程序跳过System.in

时间:2015-10-08 21:46:19

标签: java while-loop java.util.scanner

我正在创建一个程序来调用另一个创建RPN计算器的代码

        RPNCalculator Calculator = new RPNCalculator();
        System.out.println("Please enter a valid post-fix expression one token " +
                    "at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)");
        System.out.println("Each token must be an integer or an operator (+,-,*,/)");
        Scanner reader= new Scanner(System.in);


        System.out.println();
        System.out.println("That expression equals " + result);
        System.out.println();

    while (true)
    {    
         equation = reader.nextLine();
         result=Calculator.evaluateEquation(equation);
    }

然而,当程序运行时,它甚至没有机会输入任何内容并返回"该表达式等于0"

1 个答案:

答案 0 :(得分:0)

你的while循环位于System.out.println()的错误一边。

Scanner reader= new Scanner(System.in);
while (true)
{    
     equation = reader.nextLine();  
     //YOU ALSO need a reason to break out of this loop.
     //LIKE IF equation == "EXIT" or "0"
         //BREAK
     result=Calculator.evaluateEquation(equation);
}   
System.out.println();
System.out.println("That expression equals " + result);
System.out.println();