Java while循环游戏

时间:2016-03-04 04:34:08

标签: java while-loop numbers

我正在用Java编写一个程序,需要让用户猜测只有10次尝试的数字,并使用while循环。这就是我目前所拥有的,任何人都可以帮助将while循环放在哪里以及我缺少什么?谢谢。

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

    public class GuessGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here



        Random generator = new Random(); 

        Scanner input = new Scanner(System.in);

        int numberToGuess = 1 + generator.nextInt(100); 
        int numberOfTries = 0;
        int limit= 10;
        int guess;



       System.out.println ("You have 10 tries to guess a number between 1 and 100");

        System.out.print("Guess number 1: ");
        guess= input.nextInt();
        numberOfTries++;


        if (guess < numberToGuess)
                 System.out.println("Your guess is too low. Try again.");

        else if (guess > 100)
                System.out.println("Guesses should be between 1 and 100.");

        else if (guess > numberToGuess)
            System.out.println("Too high. Try again.");

        else if (guess == numberToGuess)
            System.out.println("Congratulations!");


        System.out.print("Guess number 2: ");
        guess= input.nextInt();
        numberOfTries++;


         System.out.print("Guess number 3: ");
        guess= input.nextInt();
        numberOfTries++;

         System.out.print("Guess number 4: ");
        guess= input.nextInt();
        numberOfTries++;

         System.out.print("Guess number 5: ");
        guess= input.nextInt();
        numberOfTries++;

         System.out.print("Guess number 6: ");
        guess= input.nextInt();
        numberOfTries++;

         System.out.print("Guess number 7: ");
        guess= input.nextInt();
        numberOfTries++;

         System.out.print("Guess number 8: ");
        guess= input.nextInt();
        numberOfTries++;

         System.out.print("Guess number 9: ");
        guess= input.nextInt();
        numberOfTries++;

         System.out.print("Guess number 10: ");
        guess= input.nextInt();
        numberOfTries++;            

       }
}

3 个答案:

答案 0 :(得分:2)

  

用户只需10次尝试即可猜出该数字

 while(numberOfTries<10){
    System.out.print("Guess number " + (numberOfTries+1) + ": ");
    guess= input.nextInt();
    if (guess < numberToGuess)
             System.out.println("Your guess is too low. Try again.");
    else if (guess > 100)
            System.out.println("Guesses should be between 1 and 100.");
    else if (guess > numberToGuess)
        System.out.println("Too high. Try again.");
    else if (guess == numberToGuess){
        System.out.println("Congratulations!");
        break;
    }

    numberOfTries++;
    }
// If after executing the while loop the numberOfTries is equal to 10, that means the user had made all attempt.
 if(numberOfTries == 10){
    System.out.println("Sorry, you did not guess the guess the answer in 10 tries");   
    System.out.println("The number was" +numberToGuess); 
 }

这将取代您发布的所有陈述

System.out.println ("You have 10 tries to guess a number between 1 and 100");

答案 1 :(得分:2)

尝试这样的事情:

Random generator = new Random(); 
Scanner input = new Scanner(System.in);

int numberToGuess = 1 + generator.nextInt(100); 
int numberOfTries = 0;
int limit= 10;
int guess;
System.out.println ("You have 10 tries to guess a number between 1 and 100");

while(numberOfTries < 10){
    System.out.print("Guess number " + numberOfTries + ": ");
    guess= input.nextInt();
    if (guess < numberToGuess)
         System.out.println("Your guess is too low. Try again.");

    else if (guess > 100)
        System.out.println("Guesses should be between 1 and 100.");
    else if (guess > numberToGuess)
        System.out.println("Too high. Try again.");
    else if (guess == numberToGuess){
        System.out.println("Congratulations!");
        break;
        }
    numberOfTries++;
}

答案 2 :(得分:0)

不要将10个数字作为单独的语句,而是执行以下操作:

int guess = 0;
guess = input.nextInt();
while( guess != numberToGuess || numberOfTrys > 10){
    System.out.println("Enter a new guess :: ");
    guess = input.nextInt();
    numberOfTrys ++;
}

我们的条件检查我们的猜测是否正确或者试验次数是否超过10次。如果这两个条件中的任何一个为真,它将退出while循环,您可以进一步处理程序逻辑。