Java:扫描仪输出错误

时间:2013-10-21 16:34:16

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

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

循环的顶部调用nextLine(),而处理无效输入的else块(例如我输入的字符串)也调用nextLine()。但仍然不知怎的,我的输出结果不好

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

输入左侧值: 2

输入运营商: -

输入右侧值: t

输入无效

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

无效的运营商

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

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

以下是代码片段,其中包含错误代码的重要注释。我本来只发布了问题所在的while块,但由于while块是大部分程序而且整个程序只比该部分大一点,我认为发布所有内容会更好。

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;
        }



    }
}
}

1 个答案:

答案 0 :(得分:1)

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();,然后检查其他并输出无效输入。

输入仍然具有值“t”,因此它会再次启动第二个循环并且

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

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

检查已经有“t”的 input.hasNext(),所以取“t”并继续。

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