计算器程序中的用户输入错误

时间:2013-08-23 19:26:49

标签: java

这是我用于计算器程序的代码。

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

1 个答案:

答案 0 :(得分:8)

Scanner的构造函数中删除引号,以便它不会尝试从String源读取

Scanner matt = new Scanner(System.in);