我需要使用floor函数来修复用户输入的值,而不是让它抛出异常并停止。有可能吗?如果用户输入8.3,它应该变成8,并且不会抛出异常。你知道怎么做吗?
我写的部分代码是这样的:
try{
int y=0;
Scanner inputY = new Scanner(System.in);
System.out.println("Enter an Integer between 1-9");
y = inputY.nextInt();
catch(InputMismatchException e){
return 0;
}
finally{
System.out.println("The input number was not an integer between 1-9");
System.exit(0);
}发布于 2017-02-04 04:05:58
不要使用nextInt(),而要使用nextDouble(),并进行从double到int的转换。
double y = inputY.nextDouble();
int x = (int) y;https://stackoverflow.com/questions/42032171
复制相似问题