关于java.util.InputMismatchException的错误
源代码:importjava.util.*;publicclassSwitchSelect{publicstaticvoidmain(Stringargs[]){@Sup...
源代码:
import java.util.*;
public class SwitchSelect {
public static void main(String args[]){
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("What is your score of math?");
double x = scan.nextDouble();
int y = (int) x;
int ySize = y/10;
switch(ySize){
case 10:
System.out.println("perfect!");
break;
case 9:case 8:
System.out.println("excellent!");
break;
case 7:
System.out.println("favorable!");
break;
case 6:
System.out.println("pass!");
break;
default:
System.out.println("not pass!");
}
}
}
运行结果:
What is your score of math?
89.98
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at java_basic.SwitchSelect.main(SwitchSelect.java:9)
想请问一下,scanner我定义的输入格式是double,为什么输个小数进去还会报错,但是输个整数进去就没有问题?! 展开
import java.util.*;
public class SwitchSelect {
public static void main(String args[]){
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("What is your score of math?");
double x = scan.nextDouble();
int y = (int) x;
int ySize = y/10;
switch(ySize){
case 10:
System.out.println("perfect!");
break;
case 9:case 8:
System.out.println("excellent!");
break;
case 7:
System.out.println("favorable!");
break;
case 6:
System.out.println("pass!");
break;
default:
System.out.println("not pass!");
}
}
}
运行结果:
What is your score of math?
89.98
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at java_basic.SwitchSelect.main(SwitchSelect.java:9)
想请问一下,scanner我定义的输入格式是double,为什么输个小数进去还会报错,但是输个整数进去就没有问题?! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询