簡體   English   中英

在數字猜謎游戲中用FOR循環實現/替換WHILE循環

[英]Implement/Substitute WHILE Loop with FOR Loop in number guessing game

所以對於我的Java編程類,其中一個評估是以下(經典數字猜謎游戲):

編寫一個用數字播放HiLo猜謎游戲的程序。 程序應該選擇11(包括)和88(不包括)之間的隨機數,然后反復提示用戶猜測數字。 在每次猜測時,向用戶報告他或她是正確的或猜測是高還是低。 繼續接受猜測,直到用戶猜對或選擇退出。 使用sentinel值來確定用戶是否要退出。 計算猜測次數並在用戶正確猜測時報告該值。 在每個游戲結束時(通過退出或正確的猜測),提示確定用戶是否想再次玩游戲。 繼續玩游戲,直到用戶選擇停止。 您需要至少使用while循環和for循環。

到目前為止,游戲完全正常,使用WHILE和IF功能。 但是為了在我的解決方案上獲得滿分,它需要我至少使用一個FOR循環,但我很難做到這一點。

import java.util.*;
public class Guessing {
    public static void main (String[] args)
 {
 //Setting up the variables
 final int MAX = 88;
 final int MIN = 11;
 int answer, guess = 1;
 String another="Y";

 //Intializing scanner and random
 Scanner scan = new Scanner (System.in);
 Random generator = new Random();
 //play again loop
 while (another.equalsIgnoreCase("Y"))
 {
    //Generate a random number between 11 and 88
    answer = generator.nextInt(MAX-MIN)+11;

    System.out.print ("Guess the number I picked between "+MIN+" and "
               + MAX + "!\n");

    while(guess!=answer)
    {
       System.out.println("Enter your guess: ");
       guess = scan.nextInt();
       System.out.println(answer);

       if (guess<answer && guess != 0)
           System.out.println("Your guess was too low! (0 to exit) ");
       else if (guess>answer)
           System.out.println("Your guess was too high!(0 to exit) "); 
       else if (guess==0){
           System.out.println("You excited the current round.");
           break;}
       else{ 
           System.out.println("Your guess was correct!");
           break;}
       }
    }
    //Asking player to play another game
    System.out.println("Do you want to play another game?(Y|N)");
    another = scan.next();
    if (another.equalsIgnoreCase("N"))
        System.out.println("Goodbye, thank you for playing");
 }
}
}

到目前為止,該計划有效。 它正確地給出了更高/更低的建議,當輸入0作為猜測時,當前一輪停止,你可以用Y / N開始另一輪。 但我努力用FOR循環替換其中一個函數/循環。

您可以使用for循環替換中心while循環,您也可以使用它來計算迭代次數

for(int i=0;;i++)
{
   System.out.println("Enter your guess: ");
   guess = scan.nextInt();
   System.out.println(answer);

   if (guess<answer && guess != 0)
       System.out.println("Your guess was too low! (0 to exit) ");
   else if (guess>answer)
       System.out.println("Your guess was too high!(0 to exit) "); 
   else if (guess==0){
       System.out.println("You excited the current round.");
       break;}
   else{ 
       System.out.println("Your guess was correct!\n");
       System.out.println("It took "+i+" guesses to get the answer");
       break;}
   }
}

這個for循環是一個無限循環,因為它沒有第二個參數。 但是,當給出正確的答案時,你的程序將退出for循環,因為最后的其他內容中斷了。

隨着猜測的數量向上計數,可以使用for循環。 通常會寫一個for (int i = 0; i < n; ++i) {但是在這里我們想知道for循環之后的循環計數器並且必須在之前聲明它:

int numberOfGuesses = 0;
for (; guess != 0 && guess != answer; numberOfGuesses++) {
}
... numberOfGuesses

除了找到答案或退出之外沒有上限。

所有三個部分for (PARTA; PARTB; PARTC)都是可選的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM