The main body of the program is to enter scores and grade according to grade.
But if you use scanner's Nextint, the program crashes when you enter characters in the console, and then try to determine whether the input is a number and then evaluate it.
The procedure is as follows:
PackageCom.test.demo;ImportJava.util.Scanner; Public classTestone { Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner S=NewScanner (system.in); String Str=NULL;//define STR as a global variable so that the while loop can also be usedSYSTEM.OUT.PRINTLN ("Score Assessment:"); //The first cycle of death is used to keep the grade rating running . while(true){ BooleanCflag =true;//for the second cycle of death . while(Cflag) {//Use the dead loop to determine if the character entered is correct intCount = 0;//if count=str.length, the table name each character is a number, so you can exitSystem.out.println ("Please enter your score:"); STR= S.nextline ();//getting a string from the console Char[] CHS = Str.tochararray ();//converting a string to a character array for(inti = 0;i<chs.length;i++) {//Stupid Method//the Smart Way is: if (str.matches (reqex: "[0-9]*")) if(! (chs[i]>= ' 0 ' &&chs[i]<= ' 9 ') {System.out.println ("The format of the input is incorrect, please re-enter:"); Break; } Count++; } if(Count==str.length ()) {//It's all numbers.Cflag =false; }} Integer Grade= Integer.parseint (str);//Convert a string to an integer typeSystem.out.println (grade); if(grade>95&&grade<=100) System.out.println ("Great, your grades are: excellent"); Else if(grade>70&&grade<=95) System.out.println ("Great, your grades are: Medium"); Else if(grade>60&&grade<=70) System.out.println ("Great, your grades are: qualified"); Else if(GRADE<=60&&GRADE>40) System.out.println ("You failed the Grade!") "); Else if(grade<=40&&grade>=0) System.out.println ("I'm sorry you have to study again!") "); Else{System.out.println ("Input of the score is not the same as the law, please re-enter:"); Continue; } System.out.println ("Do you want to quit?" y/n "); Scanner Ys=NewScanner (system.in); String ex= Ys.nextline (). toUpperCase ();//Convert a string to uppercase if(Ex.equals ("Y") {System.out.println ("OK, quit success!" "); Break; } Else{ Continue; } } }}
Whether the Java string is a number-wide judgment