2016-12-06 23 views
0

我正在做一個代碼,可以從用戶那裏得到5,10或20個猜測。 根據用戶選擇的猜測數量,我的程序應該製作一個循環,並多次詢問用戶是否可以猜測隨機數。只要用戶沒有猜到數字並且沒有達到猜測的極限,該循環就會迭代。以下是輸出結果的例子。循環要求猜測一定的時間?

+1

歡迎StackExchange。這不是一項家庭作業寫作服務。你必須至少嘗試解決問題,並顯示你的嘗試,並詢問你出錯的地方。有關如何提出更好問題的示例,請參見[mcve]。 – AJNeufeld

回答

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

} 
} 
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*/ 
      } 

    } 
}