简体   繁体   中英

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?

You need to use a loop of some sort, eg a while loop and test the input each time.

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.

Is an if statement required after the for loop?

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 "*" . That is what break does.

You can also call trim() on text so that the loop will also stop if the user enters the asterisk with some whitespace characters.

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

Alternatively, you can place the above condition in the parentheses of the while loop.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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