0

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

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
4

1 回答 1

8

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

Scanner matt = new Scanner(System.in);
于 2013-08-23T19:27:56.120 回答