java控制台程序出错

时间:2015-08-06 04:57:58

标签: java

import java.util.Scanner;

public class Example {
    public static void main(String Args[]) {
        Scanner sc = new Scanner("System.in");
        System.out.println("Yntreq (1/2):");
        int y = sc.nextInt();
        switch (y) {
            case 1:
                System.out.println("Duq yntrel eq 1-y");
                break;
            case 2:
                System.out.println("Duq yntrel eq 2-y");
                break;
            default:
                break;
        }

    }
}

当它运行时,eclipse显示此错误

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.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Example.main(Example.java:7)

4 个答案:

答案 0 :(得分:3)

System.in不是字符串!

变化:

Scanner sc = new Scanner("System.in");

为:

Scanner sc = new Scanner(System.in);

答案 1 :(得分:1)

删除双引号" "来自" System.in"

Scanner sc = new Scanner(System.in);
instead of
Scanner sc = new Scanner("System.in");

答案 2 :(得分:1)

""移除Scanner sc = new Scanner("System.in");

Scanner sc = new Scanner(System.in);

答案 3 :(得分:0)

你在构造函数中传递了无效参数, 输入流的构造函数语法是 -

public Scanner(InputStream source)

构造一个新的Scanner,它生成从指定输入流扫描的值。使用底层平台的默认字符集将流中的字节转换为字符。 参数: source - 要扫描的输入流

示例:Scanner sc = new Scanner(System.in);