简体   繁体   中英

How to end a do while loop with a user inputted string?

public static void main (String[] args)
    {
        do {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter a string: ");
            String sentence = keyboard.nextLine();

            System.out.print("Enter a letter: ");
            String fullLetter = keyboard.nextLine();
            char letter = fullLetter.charAt(0);
            keyboard.nextLine();

            int amount = 0;
            for (int i = 0; i < sentence.length(); i++) {
                char ch = sentence.charAt(i);
                if (ch == letter) {
                    amount++;
                }
            }

            System.out.println(letter + " appears " + amount + " times in " + sentence);

            System.out.print("Continue? ");
            String decide = keyboard.nextLine();
        } while (decide.equals("yes"));
    }

}

I want the user to input either "yes" or "no" at the end of the loop, then I want that input to determine whether or not the program will loop again. As it stands right now, the the last line of my code isn't working. I've looked around and I'm not sure what I should do to fix this.

You need to declare your variable decide outside the loop and initialize inside:

String decide;
do {
    //do something ...
    decide = keyboard.nextLine();
} while (decide.equals("yes"));

You should use keyboard.next() to read a String instead of keyboard.nextLine()

next() only reads a word, nextLine() reads the whole line including Enter so it will never be equal to "yes"

You must declare declare the string describe outside of the do/while loop, otherwise it is a local variable of the do/while loop, and cannot be accessed by the do testing portion. Simply using

public static void main(String[] args) {
String decide;
        do {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter a string: ");
            String sentence = keyboard.nextLine();

            System.out.print("Enter a letter: ");
            String fullLetter = keyboard.nextLine();
            char letter = fullLetter.charAt(0);
            keyboard.nextLine();

            int amount = 0;
            for (int i = 0; i < sentence.length(); i++) {
                char ch = sentence.charAt(i);
                if (ch == letter) {
                    amount++;
                }
            }

            System.out.println(letter + " appears " + amount + " times in "
                    + sentence);

            System.out.print("Continue? ");
            decide = keyboard.nextLine();
        } while (decide.equals("yes"));
    }

will solve your problem.

You has to define your variable decide outside of the loop:

    String decide = null
    do {
      ....
      decide = keyboard.nextLine();
    } while (decide.equals("yes"));

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