0

我最近发布了一个关于扫描仪没有给出预期结果的问题,并了解到我的问题是我没有用.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;
        }



    }
}
}
4

1 回答 1

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();它是否最终到达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”。

于 2013-10-21T16:50:09.667 回答