简体   繁体   中英

Java - get out of the do-while loop with an input of user

I am trying to write a program that allows generate random numbers inside a infinite loop. If the user press 'c' it will get out of loop. But the loop only starts when I press c, it should start before and it should stop when I press 'c'.

import java.util.Random;
import java.util.Scanner;

public class main{

    public static void main (String[] args) {

        Random rand = new Random();

        int[] randNumber = new int[100];

        Scanner scanner = new Scanner(System.in);
        char c = scanner.next().charAt(0);

        do {
            for (int i = 0; i <= 99; i++) {
                randNumber[i] = rand.nextInt(100);
            }

            for (int i = 0; i < 99; i++) {
                if (randNumber[i] < randNumber[i + 1]) {
                    System.out.println("Number " + randNumber[i] + " smaller than " + randNumber[i + 1]);
                } else if (randNumber[i] > randNumber[i + 1]) {
                    System.out.println("Number " + randNumber[i] + " greater than " + randNumber[i + 1]);
                } else if (randNumber[i] == randNumber[i + 1]) {
                    System.out.println("Number " + randNumber[i] + " equal to " + randNumber[i + 1]);
                } else {
                    System.out.println("Wrong!");
                }
            }
        } while (c == 'c');
    }
}

Also I would like to how can I do this program only using while loop instead of do-while loop. When I was doing with only while I started with while(1) then break the loop with if statements from input, but that didn't work neither. Inside still there is two for loops.

Many thanks

Regarding first query move the lines of codes where you are taking input inside the do-while loop, in this way after each iteration it will ask for a input and if you enter 'c', it will continue else it will break free. Regarding second query, if you use while(1) and check for user input for 'c' using if statements and breaking out if user inputs 'c', it should work.

You need to ask for user input inside of your loop; otherwise, c never changes.

        Random rand = new Random();

        int[] randNumber = new int[100];

        Scanner scanner = new Scanner(System.in);
        
        // Just set it to 'c' as default so it enters the loop at least once. IMO this is better than an infinite loop with breakout or having another default value for c and checking it, or using a do while loop
        char c = 'c';

        while (c == 'c') {
            for (int i = 0; i <= 99; i++) {
                randNumber[i] = rand.nextInt(100);
            }

            for (int i = 0; i < 99; i++) {
                if (randNumber[i] < randNumber[i + 1]) {
                    System.out.println("Number " + randNumber[i] + " smaller than " + randNumber[i + 1]);
                } else if (randNumber[i] > randNumber[i + 1]) {
                    System.out.println("Number " + randNumber[i] + " greater than " + randNumber[i + 1]);
                } else if (randNumber[i] == randNumber[i + 1]) {
                    System.out.println("Number " + randNumber[i] + " equal to " + randNumber[i + 1]);
                } else {
                    System.out.println("Wrong!");
                }
            }

            // Ask for the next input
            char c = scanner.next().charAt(0);
        }

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