JDK:1.8.0_192
系统:centOS 7
怎么对键入的成绩进行判断,如果不是数字或不满足条件则重新输入
丑陋的源代码如下:
class hello{
public static void main(String[] args){
Scanner inputTwo = new Scanner(System.in);
Scanner inputThree = new Scanner(System.in);
//键入班级数量
System.out.print("Plz input class number:");
int classNum = inputTwo.nextInt();
double sum=0;
double avg=0;
Scanner input = new Scanner(System.in);
//循环键入学生成绩
for(int i=1;i<=classNum;i++){
sum = 0;
//键入当前班级的学生数量
System.out.print("plz input student number in class No."+i+":");
int stuNum = inputThree.nextInt();
for(int j=1;j<=stuNum;j++){
System.out.println("*****Plz input score of class No."+i+"*****");
System.out.print("Plz input score of student No."+j+":");
double score = input.nextDouble();
//判断键入是否符合条件,不符合则要求重输
if(score<=100&&score>=0){
sum=sum+score;
}else{
while(score!=score+1){
System.out.print("Wrong input,plz input again:");
score=input.nextDouble();
if(score<=100&&score>=0){
break;
}
}
}
}
avg=sum/stuNum;
System.out.println("Class No."+i+"of average score is:"+String.format("%.2f",avg));
}
}
当我键入“y”时,整个程序直接断了:
先谢过各位大佬嘞!!!
instanceof怎么用呢?
可以用next()或者nextLine() 获得输入字符串,然后用obj instanceof Integer 判断是否是int型
String end=“xxxx”;
for (int i = end.length(); --i >= 0;) {// 验证是否是int型
if (!Character.isDigit(end.charAt(i))) {
// msg("结束行数据不合法");
return false;
}
}