简体   繁体   English

如何使用类似(input =='\\ n')的东西来确定何时停止接受用户输入?

[英]How can I use something like (input == '\n') to determine when to stop accepting user input?

I have to convert an infix operation to a postfix one, however the infix operation must be inputted as one character per line. 我必须将一个infix操作转换为一个postfix,但是必须将infix操作输入为每行一个字符。 So instead of inputting something like this: 3-2, you would need to input something like this: 因此,您无需输入以下内容:3-2,而是需要输入以下内容:

3
-
2

I had an idea of using =='\\n' to determine whether the inputted character is a next line function so that would determine the end of the equation, but it doesn't work. 我有一个使用=='\\ n'来确定输入的字符是否是下一行功能的想法,这样就可以确定方程式的结尾,但是它不起作用。 I tried replacing it with a different character such as =='e', and that works perfectly. 我尝试将其替换为其他字符,例如=='e',并且效果很好。 What can I do to fix this? 我该怎么做才能解决此问题?

   String string = "";
   Scanner input = new Scanner(System.in);
   boolean flag = true;
   while (flag==true)
   {
       char charIn = input.next().charAt(0);
       string = string + charIn;
       if (charIn=='e') //inputting 'e' gives me my desired result
       {
           flag = false;
       }
   }
   //code that passes string to InfixToPostfix method and prints out the answer. this part works fine

You did not specify that this was a school assignment or that you had certain restrictions, so this answer is admittedly a shot in the dark. 您没有指定这是学校作业,还是没有一定的限制,因此,这个答案无疑是在黑暗中拍摄的。

I would recommend using a StringBuilder within a loop and reading nextLine() instead of simply next() . 我建议在循环中使用StringBuilder并读取nextLine()而不是简单的next() This allows you to determine if the entry was empty (ie: the enter key was pressed without entering a character). 这使您可以确定条目是否为空(即:在不输入字符的情况下按下了回车键)。

Also, we should allow the user to enter more than one character anyway (what happens when they try to enter 22 as a number). 另外,我们仍然应该允许用户输入多个字符(尝试输入22作为数字时会发生什么情况)。 Abandoning the char type allows for this. 放弃char类型可以实现这一点。

public static void main(String[] args) {
    StringBuilder string = new StringBuilder();
    Scanner input = new Scanner(System.in);
    boolean flag = true;
    while (flag) {

        // Capture all characters entered, including numbers with multiple digits
        String in = input.nextLine();

        // If no characters were entered, then the [ENTER] key was pressed
        if (in.isEmpty()) {
            // User is done adding characters; exit the loop
            flag = false;
        } else {

            // Otherwise, get the text entered and add it to our final string
            string.append(in);
        }
    }

    System.out.println("Final String: " + string);
}

Does this meet your needs? 这符合您的需求吗?

This should do what you want. 这应该做您想要的。 Reading just the first character has its limitations. 仅阅读第一个字符有其局限性。

String string = "";
Scanner input = new Scanner(System.in);
boolean flag = true;
while (flag==true)
{
     String nextLine = input.nextLine();
     char charIn;

     if(nextLine.length() > 0) {
       charIn = nextLine.charAt(0); //This is bad idea as you can only operate on single digit numbers
       System.out.println("charIn = " + charIn);;
       string = string + charIn;
     }
     else
          flag = false;
}

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

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