简体   繁体   English

我如何做到这一点,以便用户可以连续输入,但可以使用符号停止输入?

[英]How do I make it so the user has continuous input but can stop this with the use of a symbol?

I currently have the following code: 我目前有以下代码:

    import java.io.*;
    import java.util.*;


    public class Reverser
{   

public void run() throws IOException
{
  String text;
  String reverseText = "";

  Scanner input = new Scanner(System.in);

  System.out.println("Please enter text");
  text = input.nextLine();

  int length = text.length();

  for (int i=length-1; i >= 0; i--)


  reverseText = reverseText + text.charAt(i);



  System.out.println("Your characters in reverse are: "+reverseText);

   }

   }

I now want to make it so that once the user types in something, they can then type in something else after without having to re-run the program. 现在,我要这样做,以便用户输入内容后,他们便可以在不必重新运行程序的情况下输入其他内容。 I then want to make it so that if the character '*' is entered, it ends and you can no longer enter anything. 然后,我要这样做,以便如果输入字符'*',则结束,并且您不能再输入任何内容。 Is an if statement required after the for loop? for循环之后是否需要if语句?

You need to use a loop of some sort, eg a while loop and test the input each time. 您需要使用某种循环,例如while循环,并且每次都要测试输入。

For example: 例如:

while (true) {
    System.out.println("Please enter text");
    text = input.nextLine();
    if ("*".equals(text)) {
        break;
    }

    // ...code to use 'text'...
}

The while (true) loop will run forever, but the test if ("*".equals(text)) will break the loop (using the break keyword) if an asterix is entered. while (true)循环将永远运行,但是if ("*".equals(text))输入星号,则测试if ("*".equals(text))是否会中断循环(使用break关键字)。

Is an if statement required after the for loop? for循环之后是否需要if语句?

No, I don't think so. 不,我不这么认为。

Here is one of the methods to do this 这是执行此操作的方法之一

String text = "";
String reverseText = "";

Scanner input = new Scanner(System.in);

while (true) {
    System.out.println("Please enter text");
    text = input.nextLine();

    if (text.equals("*")) {
        break;
    }

    int length = text.length();

    for (int i=length-1; i >= 0; i--)
        reverseText = reverseText + text.charAt(i);
    System.out.println("Your characters in reverse are: "+reverseText);
    reverseText = "";
}

Explanation: The code inside the while (true) block basically keeps looping and looping until input.nextLine() returns "*" . 说明: while (true)块中的代码基本上保持循环,直到input.nextLine()返回"*"为止。 That is what break does. 这就是break作用。

You can also call trim() on text so that the loop will also stop if the user enters the asterisk with some whitespace characters. 您还可以在text上调用trim() ,这样,如果用户使用一些空白字符输入星号,循环也将停止。

if (text.trim().equals("*"))

Alternatively, you can place the above condition in the parentheses of the while loop. 或者,可以将以上条件放在while循环的括号中。

String text = "";
String reverseText = "";

Scanner input = new Scanner(System.in);

while (!text.trim().equals("*")) {
    System.out.println("Please enter text");
    text = input.nextLine();

    int length = text.length();

    for (int i=length-1; i >= 0; i--)
        reverseText = reverseText + text.charAt(i);
    System.out.println("Your characters in reverse are: "+reverseText);
    reverseText = "";
}

But this will behave a little bit differently than the first one. 但这与第一个行为有些不同。 The first method will not print the reverse of * if you ever entered one because the if is placed before the for loop. 如果您曾经输入,第一种方法将不会显示*的反面,因为if放在for循环之前。 On the other hand, the second method will print the reverse of * because the check is done after each iteration of the while loop, as if an if statement is placed at the end of the loop. 另一方面,第二种方法打印*的反面,因为检查是在while循环的每次迭代之后完成的,就像if语句放在循环的末尾一样。

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

相关问题 如何获得连续的用户输入? - How do I get continuous user input? 如何使HTML和Java表单重定向到自身,以便用户可以输入其他项? - How do I make my HTML and java form redirect to itself so that the user can input another item? 如何创建一个数组,用于存储来自连续用户输入的x,y坐标? - How do I make an array for storing x, y co-ordinates from continuous user input? 如何使用户可以选择一个人的性别? - How do I make it so that the user can choose the gender of a person? 如何检查符号是否在用户输入上? - How can I check if the symbol is on the user input? 如何使用类似(input =='\\ n')的东西来确定何时停止接受用户输入? - How can I use something like (input == '\n') to determine when to stop accepting user input? 我该如何做一个do while循环并连续捕获异常? - How can I make a do while loop with an exception catch, continuous? 我如何为我的代码创建一个循环,直到用户输入正确的代码为止? - How can i make a loop for my code so it runs until user gives correct input? 如何让用户输入双倍? - how do i make the user input a double? 如何使我的代码检查用户是否输入了字母字符? [Java] - How do I make my code check that the user has input a alphabetical character? [Java]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM