这是我用于计算器程序的代码。
package javaapp;
import java.util.Scanner;
public class JavaApp {
public static void main(String args[]){
Scanner matt = new Scanner("System.in");
double fnum;
double snum;
double answer;
System.out.println("Enter first number: ");
fnum = matt.nextDouble();
System.out.println("Enter the Second number: ");
snum = matt.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}
这是错误:
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 javaapp.JavaApp.main(JavaApp.java:10)
Java Result: 1
答案 0 :(得分:8)
从Scanner
的构造函数中删除引号,以便它不会尝试从String
源读取
Scanner matt = new Scanner(System.in);