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

允我心安 提交于 2020-01-09 12:00:12

问题


import java.util.Scanner;

public class MyFirstGame {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Please Enter A Number: ");
        double s = scanner.nextDouble();
        double randomNumber = Math.random();
        double realNumber = randomNumber*10;
        double realerNumber = Math.round(realNumber);
        System.out.println(realerNumber);

        if(s==realerNumber) {
            System.out.println("You Win!");
        } else {
            System.out.println("Try Again...");
        }
    }
}

So what I am trying to do is make a "game" for my Java class. I have generate a random number between 1 and 10 and the user has to input a number and if the input and the random number are the same, they "win." If they lose, they try again...? First, I did all the necessary scanner stuff that I don't even fully understand. I just copied the professor. So the program says to enter a number and the program generates a number between 0.0 and 1.0. I multiply that number by 10 to make it between 1 and 10. Then I round the number to the nearest integer. If the input matches this number, the program says you win. If not, it'll say try again.

The problem is how do I make the program repeat itself without the user having to reboot the program with the cmd? I need to repeat the input, random number generator, and then the result. What do I need to do? Also, how is my program? My second big one...yeah right...big. But seriously, how can I make it less complex or anything to improve it. Thanks.


回答1:


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)



回答2:


just put your code inside the do-while loop

Scanner scanner = new Scanner(System.in);

do
{
    System.out.println("Please Enter A Number: ");
    double s = scanner.nextDouble();

    double realerNumber = Math.round( Math.random() * 10 );

    System.out.println(realerNumber);

    if(s==realerNumber) {
        System.out.println("You Win!");
    } else {
        System.out.println("Try Again...");
    }
}
while(someCondition);

the someCondition can be for example a counter (if you want to play n times just set counter to n and decrease it every loop iteration then check if it is 0 in while) or some function checking if a key is pressed (like escape)

int n = 5;

do
{
    n--;
    ...
}
while(n > 0);



回答3:


This will run forever, but it's the idea mentioned in the first comment

    ...
    Scanner scanner = new Scanner(System.in);
    while(true){ // add this after Scanner ... declaration
       ...
        } // end of existing else block
    } // end of while loop, so add this single brace
    ...


来源:https://stackoverflow.com/questions/32981746/how-do-i-make-my-program-repeat-according-to-certain-circumstances

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!