Char使用扫描仪

时间:2011-11-26 16:42:25

标签: java.util.scanner

我是Java的新手。我希望能够在扫描仪中输入操作字符。

我的代码应该生成以下内容,

  1. 输入两个数字:13 6
  2. 什么操作? *
  3. 13 * 6是什么? 78
  4. 正确!
  5. 我正在使用Switch语句来存储用户可用的操作。 它们是:+, - ,*和/.

    每次在输出后写操作时都会收到运行时错误。

    import java.util.Scanner;
    
    public class Användardialog4 {
        public static void main(String[]args){
            Scanner sc = new Scanner(System.in);
    
        int svar;
        String op;
        char operator;
    
        System.out.println("Enter two numbers: ");
        int tal = sc.nextInt();
        int tal2 = sc.nextInt();
    
        System.out.println("Operation?");
        op = sc.nextLine();
        operator = op.charAt(0);
    
        switch(operator){
        case '+':
            System.out.println("What is "+tal+" + "+tal2);
            svar = sc.nextInt();
            if (svar == tal+tal2)
                System.out.println("Correct!");
            else
                System.out.println("Wrong - The right answer is: "+tal+tal2);
            break;
        case '*':
            System.out.println("What is "+tal+" * "+tal2);
            svar = sc.nextInt();
            if (svar ==tal*tal2)
                System.out.println("Correct!");
            else
                System.out.println("Wrong - The right answer is: "+tal*tal2);
            break;
        case '-':
            System.out.println("What is "+tal+" - "+tal2);
            svar = sc.nextInt();
            if (svar == tal-tal2)
                System.out.println("Correct!");
            else
                System.out.println("Wrong - The right answer is: "+(tal-tal2));
            break;
        case '/':
            System.out.println("What is "+tal+" / "+tal2);
            svar = sc.nextInt();
            if (svar == tal/tal2)
                System.out.println("Correct!");
            else
                System.out.println("Wrong - The right answer is: "+tal/tal2);
            break;
        }
    }
    }
    

    我得到的运行时错误是:

    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
    Användardialog4.main(Dialog4.java:16)
    

    所以我的问题是,如何在Scanner类中使用字符? 是不是有nextChar();或类似Java的东西?

2 个答案:

答案 0 :(得分:1)

op = sc.nextLine();更改为op = sc.next();

public String next();查找并返回此扫描程序中的下一个完整令牌。由于您的操作只有一个令牌/字符,因此这是可行的方法。

public String nextLine();使此扫描程序超过当前行并返回跳过的输入。由于您的流中没有endline-character,因此失败。

答案 1 :(得分:0)

int ans;
switch(operator){
    case '+':
        ans = tal + tal2;
        break;
    case '*':
        ans = tal * tal2;
        break;
    case '-':
        ans = tal - tal2;
        break;
    case '/':
        ans = tal / tal2;
        break;
    }
    System.out.println("What is "+tal+operator+tal2+"?");
    svar = sc.nextInt();
    if (svar == ans)
        System.out.println("Correct!");
    else
        System.out.println("Wrong - The right answer is: "+ans);

代码质量改进的一个小改动。删除了冗余代码。