要求猜测一定时间的循环?

时间:2016-12-06 04:34:04

标签: java

我正在制作一个代码,可以从用户那里获得5,10或20个猜测。 根据用户选择的猜测次数,我的程序应该循环并多次询问用户是否可以猜出随机数。只要用户没有猜到数字并且没有达到猜测的极限,这个循环就会迭代。以下是输出应该是什么样子的示例。

3 个答案:

答案 0 :(得分:0)

尝试:

for(int i = 1; i <= numberOfGuesses; i++){
   System.out.print("Enter guess #"+i+": ");
   guess = scan.nextInt();
   if (guess > randomNumber && guess <= 100)
      System.out.println("Your guess, "+guess+", is less than the magic number.");
   else if (guess < randomNumber && guess > 0)
      System.out.println("Your guess, "+guess+" is greater than the magic number.");
   else if (guess == randomNumber){
      System.out.println("Congratulations, "+name+"! You guessed the magic number in "+i+" guesses");
      break;
   }
   else
      i--;
}

答案 1 :(得分:0)

通过以下代码,我提供了有关逻辑的内联注释。如果您有任何难以理解的观点,请对答案进行评论。

package src.main;

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

public class NewClass {
public static void main(String[] args) {

    int guess;

    Scanner scan = new Scanner(System.in);

    System.out.print(" Please enter your name: ");
    String name = scan.next();

    System.out.print(" Would you like to try to guess a number? (Yes or No):");
    String answer = scan.next();

    if (answer.length() == 3)
        System.out.print("How many guesses would you like? (5, 10, 20)");
    int numberOfGuesses = scan.nextInt();

    Random random = new Random();

    int randomNumber = random.nextInt(100 - 0 + 1) + 0;
    int actualGuessCount = 0;

    // Check whether the number of Guess attempts not exceeded
    while (actualGuessCount <= numberOfGuesses) {
        // Increase the number of Guess attempts by one
        actualGuessCount++;

        System.out.print("Enter guess #" + actualGuessCount + ":");
        guess = scan.nextInt();

        // Check the Guess value and random value same
        if (guess == randomNumber) {
            System.out.println("Congratulations, " + name + "! You guessed the magic number in " + actualGuessCount
                    + " guesses.");
            System.exit(0);
        } else if (guess < randomNumber) {// Check guess value less than the
                                            // random value
            System.out.println("Your guess, " + guess + ", is less than the magic number");
        } else {
            System.out.println("Your guess, " + guess + ", is greater than the magic number");
        }

        // If the number of attempts equals to requested attempts program
        // will exit
        if (actualGuessCount == numberOfGuesses) {
            System.out.println("You consume all the guesses.");
            System.exit(0);
        }
    }

}
}

答案 2 :(得分:0)

在这里

    import java.util.*;

    public class peasy {
    public static void main(String[] args)
        {

            String name,guess;
            int choice,i,Userguess,magic;
            Scanner a = new Scanner(System.in);
            System.out.print("Enter your name:");
            name = a.next();
            System.out.print("Would you like to try a guess number? (Yes or No):");
            guess = a.next();
            if (guess.equals("No")){
                System.exit(0); /* we do not need to write for Yes as the program will as default */
            }


            System.out.print("How many guess would you like? (5 ,10 ,15): ");
            choice= a.nextInt(); /*CHoice stores the total guess user wishes to make*/

            Random number = new Random(); 
            magic =1+ number.nextInt(100); /* Creates a random number from the range 1 to 100 */

            for(i=0;i<choice;i++)
            {
                System.out.printf("Enter you guess #%d :",(i+1));
                Userguess=a.nextInt(); /* stores the guess of user*/
                if(Userguess == magic)
                {
                    System.out.printf("Congrulations %s ! You guessed the magic number in %d guesses \n",name,Userguess ); /* displays the messsgae if the guess is correct*/
                    System.exit(0);
                }
                if(Userguess < magic)
                {

                    System.out.printf("Your guess %d is less than the magic number",Userguess ); /*displays a message by checking if the number entered is less than the magic number*/
                }
                if(Userguess > magic)
                {

                    System.out.printf("Your guess %d is more than the magic number",Userguess ); /*displays a message by checking if the number entered is more than the magic number*/
                } /*closing parenthisis*/
            }

    }
}