Java:扫描仪输出错误

标签 java debugging console-application java.util.scanner

我最近发布了一个有关扫描仪未给出预期结果的问题,并了解到我的问题是我没有使用 .nextLine() 刷新扫描仪。我对我正在开发的程序感到困惑,因为我正在正确地刷新扫描仪,但是当我测试我的程序时,如果 - 当提示输入数字时 - 我输入一个字符串,我会得到错误的输出。它重复相同的循环两次。

循环的顶部调用了 nextLine(),处理无效输入(例如我正在输入的字符串)的 else block 也调用了 nextLine()。但不知怎的,我的输出仍然很差

具体来说,这是一个错误输出的示例,用户输入为粗体,有问题的输出为斜体

输入左侧值:2

输入运算符:-

输入右侧值:t

输入无效

输入运算符(+ - * 或/:

无效运算符

输入运算符(+ - * 或/:

上面四行会自动输出到控制台。

这是代码片段,在错误代码所在的地方有一个大注释。我本想只发布问题所在的 while block ,但由于 while block 是程序的大部分,并且整个程序只比该部分大一点,所以我认为最好发布全部内容。

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Calculator{

public static void main(String[] args){

    double leftHandVal = 0.0;

    //Output Title & Instructions
    System.out.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
    System.out.print("!\t\t\t\t!\n");
    System.out.print( "!\t   INSTRUCTIONS\t\t!\n");
    System.out.print("!\t\t\t\t!\n");
    System.out.print("!   INPUT\t\tOUTPUT\t\t!\n");
    System.out.print("!  *******\t         *********\t\t!\n");
    System.out.print("!   c or C\t\tClear\t\t!\n");
    System.out.print("!   q or Q\t\tQuit\t\t!\n");
    System.out.print("!     +\t\tAddition\t\t!\n");
    System.out.print("!     -\t\tSubtraction\t!\n");
    System.out.print("!     *\t\tMultiplication\t!\n");
    System.out.print("!     /\t\tDivision\t\t!\n");
    System.out.print("!\t\t\t\t!\n");
    System.out.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");

    while(true){

        Scanner input = new Scanner(System.in);
        char op = '\n';//(+, -, *, or /) will use in switch statement for their ascii decimal values

        System.out.print("Enter the left-hand value: ");

        //these blocks allow the code at the very bottom to not erroneously ask the user for extra input with hasNext() calls 
        if(input.hasNext("c") || input.hasNext("C")){//even though its unlikely for a user to clear so early...just in case
            leftHandVal = 0.0;
        }
        else if(input.hasNext("q") || input.hasNext("Q")){//even though its unlikely for a user to quit so early...just in case
            op = 113;//assign q for quit code
        }  
        else if(input.hasNextDouble()){
            leftHandVal = input.nextDouble(); 


            /*
             *
             *BAD CODE INSIDE WHILE BELOW
             *BAD CODE INSIDE WHILE BELOW
             *BAD CODE INSIDE WHILE BELOW
             *BAD CODE INSIDE WHILE BELOW
             *
             */

            while(true){

                input.nextLine();
                double rightHandVal = 0.0;

                System.out.print("\nEnter operator (+ - * or / : ");

                if(input.hasNext()){
                    op = input.next().charAt(0);
                }

                //if user wishes to cancel or quit on operator prompt, break out of inner while to access the clear and quit code
                if(op == 99 || op == 67){
                    op = 99;
                    break;
                }
                else if(op == 113 || op == 81){
                    op = 113;
                    break;
                }
                else if((op != 43) && (op != 45) && (op != 42) && (op != 47)){//if invalid operator, restart inner while
                    System.out.print("Invalid Operator");

                    continue;
                }

                System.out.print("Enter the right-hand value: ");

                if(input.hasNextDouble()){
                    rightHandVal = input.nextDouble();

                    switch(op){
                        case 43:
                            System.out.printf("%.3f + %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal + rightHandVal));
                            leftHandVal += rightHandVal;
                            break;
                        case 45: 
                            System.out.printf("%.3f - %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal - rightHandVal));
                            leftHandVal -= rightHandVal;
                            break;
                        case 42:
                            System.out.printf("%.3f * %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal * rightHandVal));
                            leftHandVal *= rightHandVal;
                            break;
                        case 47:
                            System.out.printf("%.3f / %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal / rightHandVal));
                            leftHandVal /= rightHandVal;
                            break;

                    }
                }

                //if clear or quit requested from prompt for right-hand value, break to reach the clear and quit code
                else if(input.hasNext("c") || input.hasNext("C")){
                    op = 99;
                    break;
                }
                else if(input.hasNext("q") || input.hasNext("Q")){
                    op = 113;
                    break;
                }
                else{
                    System.out.print("Invalid Input");

                }

            } 
        }

        //if c || C reset op to null and restart outer while
        if(op == 99 || op == 67){
            op = '\n';
            leftHandVal = 0.0;
            continue;
        }
        //else if q || Q, prompt user with a popup to confirm.
        if(op == 113 || op == 81){
            int response = JOptionPane.showConfirmDialog(null, "QUIT CALCULATOR?", null, JOptionPane.YES_NO_OPTION);
            if(response == 0){
                System.exit(0);
            }
            continue;
        }



    }
}
}

最佳答案

else if(input.hasNext("c") || input.hasNext("C"))
{
  op = 99;
  break;
}
else if(input.hasNext("q") || input.hasNext("Q")){
  op = 113;
  break;
}
else{
  System.out.print("Invalid Input");

}

在此代码中,当您输入“t”作为右手值时,您只需检查 hasNext(); 最后它会出现 else 并打印 Invalid Input。

但是输入仍然具有值“t”,因此它会再次开始第二个while循环,并且

                System.out.print("\nEnter operator (+ - * or / : ");

                if(input.hasNext()){
                    op = input.next().charAt(0);
                }

检查已经有“t”的input.hasNext(),因此采用“t”并继续。

解决方案是在进入 while 循环之前刷新“t”。

关于Java:扫描仪输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19500001/

相关文章:

java - 写入太多日志会导致 OutOfMemory 错误吗?

ios - 升级到 Safari 7.0.4 后无法在本地和远程设置 Web 检查器断点

java - 有没有一个很好的独立的、轻量级的、基于 GUI 的 mac Java 调试工具?

java - 控制台应用程序中基于选项卡的自动完成

java - 基于主题/ token 和 token 组的完整FCM Java/Spring集成

java - Intellij IDEA 检查样式

java - 当程序在 Debug模式下运行时,有什么方法可以查看 java 程序中数组的内容吗?

c++ - fstream 测试程序由于某种原因崩溃

c# - 该流不支持并发IO读写操作

java - 可以为加载类定义新字段