简体   繁体   English

第一个用户输入尝试不起作用(Java)

[英]First user input attempt not working (Java)

I am trying to get the user input and when I put a value for the first attempt it will give me the question again "Enter the Expression (% to break):" although if the first attempt is blank it will give the correct error message "Expression cannot be blank". 我正在尝试获取用户输入,当我为第一次尝试设置值时,它将再次向我提出问题“输入表达式(%要中断):”虽然如果第一次尝试为空,它将给出正确的错误消息“表达不能为空”。
So what I need is how to get the first attempt to work. 所以我需要的是如何让第一次尝试工作。

THANKS THIS HAS BEEN SOLVED!! 谢谢,这已经解决了! I REALLY APPRECIATE IT! 我真的很感激!

package PrefixCalc;

import java.util.Scanner;

public class PrefixCalc {

    public static void main(String[] args) {
        Expressions calc;
        Scanner console = new Scanner(System.in);

        System.out.print("Enter the Expression(% to break): ");
        String line = console.nextLine();
        while (line.isEmpty()) {
            System.out.println("Expression cannot be empty!");
            System.out.print("Enter the Expression(% to break): ");
            line = console.nextLine();
        }
        while (!line.isEmpty()) {
            if (line.equals("%")) {
                break;
            } else {

                if (args.length > 0) {
                    System.out.println("Processing string " + args[0]);
                    calc = new Expressions(new Scanner(args[0]));
                } else {
                    System.out.print("Enter the Expression(% to break): ");
                    calc = new Expressions(new Scanner(console.nextLine()));   //System.in
                }

                System.out.println("\nInput as prefix expression:");
                calc.showPreFix();
                System.out.println("Result: " + calc.evaluate());
                System.out.println("infix expression:");
                calc.showInFix();
                System.out.println("...................................................");
            }


        }
    }
}

In your while loop, you are not using the input you received from the console in your if statements, you are using args , the input into the main method, which is not the value the user inputted after being prompted with "Enter the Expression(% to break): " . 在你的while循环中,你没有使用你在if语句中从控制台收到的输入,你正在使用args ,即main方法的输入,这不是用户输入"Enter the Expression(% to break): "

The problem is right here: 问题就在这里:

            if (args.length > 0) {  <--- args instead of line
                System.out.println("Processing string " + args[0]);
                calc = new Expressions(new Scanner(args[0]));
            } else {
                System.out.print("Enter the Expression(% to break): ");
                calc = new Expressions(new Scanner(console.nextLine()));   //System.in
            }

args.length is 0 because, presumably, you did not run this method with any commands. args.length0因为据推测,您没有使用任何命令运行此方法。 Because args is empty, you are going into the else statement and printing out the "Enter the Expression(% to break): " prompt a second time. 因为args是空的,所以你要进入else语句并再次打印出"Enter the Expression(% to break): "提示符。

Just use the line variable in the if-statment and it should work fine. 只需在if-statment使用line变量,它应该可以正常工作。 Like this: 像这样:

            if (line.length > 0) {
                System.out.println("Processing string " + args[0]);
                calc = new Expressions(new Scanner(args[0]));
            } else {
                System.out.print("Enter the Expression(% to break): ");
                calc = new Expressions(new Scanner(console.nextLine()));   //System.in
            }

Also, you should probably reassign line to the new value you are getting after the second prompt. 此外,您应该将line重新分配给第二个提示后获得的新值。 You need to do something with it to avoid an infinite loop. 你需要用它做一些事情以避免无限循环。 See @Kevin Bowersox's answer for an elegant solution to that problem. 请参阅@Kevin Bowersox的答案,找到解决该问题的优雅方案。

The code is creating an infinite loop. 代码正在创建一个无限循环。 The following while loop executes while line is not empty, however it will never be empty since the String line is never modified. 以下while loopline不为空while loop执行,但是它永远不会为空,因为String line永远不会被修改。 At some point line needs to be assigned the value entered by the user, so that it can break appropriately if a % is entered. 在某些点line需要为用户输入的值分配line ,以便在输入%它可以适当地中断。 Also I'm not sure why the code uses the arguments passed into the main method, instead it should be using line . 另外我不确定为什么代码使用传递给main方法的参数,而应该使用line

  //if line contains anything other than `%` it causes an infinite loop
  while (!line.isEmpty()) {
            if (line.equals("%")) {
                break;
            } else {

                if (args.length > 0) {
                    System.out.println("Processing string " + args[0]);
                    calc = new Expressions(new Scanner(args[0]));
                } else {
                    System.out.print("Enter the Expression(% to break): ");
                    calc = new Expressions(new Scanner(console.nextLine()));   //System.in
                }

                System.out.println("\nInput as prefix expression:");
                calc.showPreFix();
                System.out.println("Result: " + calc.evaluate());
                System.out.println("infix expression:");
                calc.showInFix();
                System.out.println("...................................................");
            }
        }

FIX 固定

  while (!line.isEmpty()) {
            if (line.equals("%")) {
                break;
            } 
                calc = new Expressions(new Scanner(line));
                System.out.println("\nInput as prefix expression:");
                calc.showPreFix();
                System.out.println("Result: " + calc.evaluate());
                System.out.println("infix expression:");
                calc.showInFix();
                System.out.println("...................................................");
                System.out.print("Enter the Expression(% to break): ");
                line = console.nextLine();

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM