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

后端 未结 3 1555
说谎
说谎 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:13

    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);
    

提交回复
热议问题