[英]Getting decimal number input/output for Math.abs
So my question is the following: I get an error on line 12 that I want to solve but haven't found the result for. 所以我的问题如下:我在第12行得到一个错误,我想要解决,但没有找到结果。 I use Eclipse to run and write my code. 我使用Eclipse来运行和编写我的代码。
This is what I do: 这就是我做的:
an error pops up saying 弹出一个错误说
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at apples.main(apples.java:12)
How come this does not work? 为什么这不起作用? Also, I tried running it in CMD outside of Eclipse aswell, without sucess. 此外,我尝试在Eclipse之外的CMD中运行它,没有成功。
import java.util.Scanner;
class apples
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Select one of the following(absolute,ceil,floor,max,min,power,squareroot): ");
String code = scan.nextLine();
if (code.contains("absolute"))
{
System.out.println("Enter a number to get absolute value: ");
Scanner num1 = new Scanner(System.in);
double numberone;
double numberone1 = num1.nextDouble();
System.out.println(Math.abs(numberone1));
}
}
}
InputMismatchException , this exception thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type. InputMismatchException ,此扫描程序抛出此异常,指示检索到的标记与预期类型的模式不匹配,或者标记超出预期类型的范围。
num1.nextDouble()
-> Here your passing value does not match the double regular expression, or is out of range. num1.nextDouble()
- >这里传递的值与双正则表达式不匹配,或者超出范围。
Your code is working for me if i put valid input input for your program . 如果我为您的程序输入有效的输入输入,那么您的代码对我有用。
If you are getting InputMismatchException means you are not providing expected input to Scanner. 如果您收到InputMismatchException意味着您没有向Scanner提供预期的输入。
double numberone1 = num1.nextDouble();
For this from your command you should give Double value only otherwise it will throw InputMismatchException
对于你的命令,你应该只给Double值,否则它将抛出InputMismatchException
import java.util.Scanner; import java.util.Scanner;
public class Test{
private static Scanner scan;
private static Scanner num1;
public static void main(String[] args){
scan = new Scanner(System.in);
String code;
do{
System.out.println("Select one of the following(absolute,ceil,floor,max,min,power,squareroot): ");
code = scan.nextLine();
if (code.contains("absolute"))
{
System.out.println("Enter a number to get absolute value: ");
num1 = new Scanner(System.in);
//double numberone;
double numberone1 = num1.nextDouble();
System.out.println(Math.abs(numberone1));
}
}while(!code.contains("absolute"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.