读取两次后扫描仪无法正常工作 [英] Scanner doesn't work properly after reading double

查看:54
本文介绍了读取两次后扫描仪无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扫描器在调用 nextDouble()后不等待输入,但是在 nextLine()之后工作正常.

Scanner doesn't wait for input after calling nextDouble() but it works fine after nextLine().

import java.util.*;

public class calc {
    public static final Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        while(true) {
            calculate();
            System.out.print("Would you like to run again? Y/N: ");
            String repeat = input.nextLine().toLowerCase();
            if (!(repeat.equals("y") || repeat.equals("yes"))) {
               System.out.println("\nBye!\n");
               break;
            }
        }
    }
    public static void calculate() {
        System.out.print("\nAdd, Subtract, Multiply, Divide or Modulus? : ");
        double fsnum, scnum, ans;
        String choice = input.nextLine().toLowerCase();
        if(choice.equals("add") || choice.equals("subtract") || choice.equals("multiply") || choice.equals("divide") || choice.equals("modulus")) {
            System.out.print("Enter the first number: ");
            fsnum = input.nextDouble();
            System.out.print("Enter the second number: ");
            scnum = input.nextDouble();
            switch(choice) {
                case "add":
                    ans = fsnum+scnum;
                    break;
                case "subtract":
                    ans = fsnum-scnum;
                    break;
                case "multiply":
                    ans = fsnum*scnum;
                    break;
                case "divide":
                    ans = fsnum/scnum;
                    break;
                case "modulus":
                    ans = fsnum%scnum;
                    break;
                default:
                    ans = 0;
                    break;
            }
            System.out.println("The answer is: " + ans);
        } else {
            System.out.println("ERROR: Please select a valid operation.");
        }
    }
}

输出

Add, Subtract, Multiply, Divide or Modulus? : add
Enter the first number: 3
Enter the second number: 4
The answer is: 7.0
Would you like to run again? Y/N:
Bye!

您可以看到跳过了最后一次要再次运行的输入,程序似乎将其假定为"或null.
如果选择了无效的操作,这似乎不会发生.-

As you can see the last input to run again is skipped and the program seems to assume it as "" or null.
This doesn't seem to happen if an invalid operation is chosen. -

Add, Subtract, Multiply, Divide or Modulus? : yaya
ERROR: Please select a valid operation.
Would you like to run again? Y/N: n

Bye!

有解决方案吗?

推荐答案

尝试在scnum = input.nextDouble();之后立即放置input.nextLine();我认为您需要摆脱换行符(nextDouble()不会读取).有关更好的说明,请参见使用scanner.nextLine():)

Try putting input.nextLine() immediately after scnum = input.nextDouble(); I think you need to get rid of the newline character (which is not read by nextDouble()). See Using scanner.nextLine() for a better explanation :)

这篇关于读取两次后扫描仪无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆