How do I make my program repeat according to certain circumstances?

后端 未结 3 1556
说谎
说谎 2020-12-04 03:46
import java.util.Scanner;

public class MyFirstGame {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        Syste         


        
3条回答
  •  没有蜡笔的小新
    2020-12-04 04:23

    Use a while loop:

    long realerNumber = Math.round(realNumber);
    // first guess
    long guess = scanner.nextLong();
    while (guess != realerNumber) {
        System.out.println("Try Again...");
        // guess again
        guess = scanner.nextInt();
    }
    
    System.out.println("You Win!");
    

    There is already a class to generate random numbers, you could use it:

    // TODO: move to constant
    int MAX = 10;
    // nextInt(int n) generates a number in the range [0, n)
    int randomNumber = new Random().nextInt(MAX + 1)
    

提交回复
热议问题