Java扫描程序无法正常工作 [英] Java- Scanner not working

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

问题描述

很抱歉如果这听起来很愚蠢,但是我刚接触Java并且在我遇到卡时正在制作计算器程序。

我在程序之前已经使用扫描仪输入但是当我尝试稍后输入,程序不接受输入并继续。

这是代码 -

Sorry If this sounds stupid, but I'm new to Java and was making a calculator program when I got stuck.
I have already taken input with scanner before in the program but when I try to take input later, The program doesn't take input and continues.
Here is the code-

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("\nGoodbye!\n");
               break;
            }
        }
    }
    public static void calculate() {
        System.out.println("\nWelcome to THE CALCULATOR by PRATINAV BAGLA");
        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.");
        }
    }
}

输出为 -

Welcome to THE CALCULATOR by PRATINAV BAGLA

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:
Goodbye!

正如你所看到的那样,最后再次运行的输入被跳过,程序似乎认为它是 或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. -

Welcome to THE CALCULATOR by PRATINAV BAGLA

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

Goodbye!

任何解决方案?

推荐答案

尝试在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 :)

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

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