我不断从输出中获得error message
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 test1.TC1.main(TC1.java:11)
但在顶部它没有任何错误
package test1;
import java.util.Scanner;
public class TC1 {
public static void main(String[] args){
double name, age, answer;
Scanner IN = new Scanner (System.in);
System.out.println("Whats yor name?");
name = IN.nextDouble();
System.out.println("How old are you?");
age = IN.nextDouble();
answer = age + name;
System.out.println(answer);
}
}
答案 0 :(得分:1)
name
实际上是double类型的变量。但也许你要的是名字应该是字符串类型。
更改名称类型并使用name = IN.nextLine();
答案 1 :(得分:0)
试试这个:
将变量类型更改为String
而不是double
,并使用Scanner.next()
public class TC1 {
public static void main(String[] args){
String name, age, answer;
Scanner IN = new Scanner (System.in);
System.out.println("Whats yor name?");
name = IN.next();
System.out.println("How old are you?");
age = IN.next();
answer = age + name;
System.out.println(answer);
}
}
答案 2 :(得分:0)
当您提供非数字输入时,将抛出此异常。
在您的情况下,您已将名称声明为 double ,并且我确信您输入的是非数字值它,特拉维斯或丽塔说:)
因此要么将name声明为String并使用IN.next()或尝试输入数值:)