我正在做一個代碼,可以從用戶那裏得到5,10或20個猜測。 根據用戶選擇的猜測數量,我的程序應該製作一個循環,並多次詢問用戶是否可以猜測隨機數。只要用戶沒有猜到數字並且沒有達到猜測的極限,該循環就會迭代。以下是輸出結果的例子。循環要求猜測一定的時間?
0
A
回答
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*/
}
}
}
相關問題
- 1. 彩色猜測game.How猜測後停止循環是正確
- 2. 帶有While循環的猜測遊戲
- 3. 需要解決不正確的猜測循環
- 4. Java猜測遊戲無限循環
- 5. 果凍豆猜測while循環?
- 6. JavaScript While循環|猜測遊戲
- 7. 添加一個猜測循環到我的Javascript猜數字遊戲
- 8. 斯卡拉循環一定的時間
- 9. 穩定的循環時間
- 10. 在Javascript中創建一個數字猜測遊戲 - 需要幫助循環
- 11. PHP循環捲曲的要求逐一
- 12. 如何在數字猜測遊戲循環時重新啓動
- 13. 猜猜時間格式?
- 14. 用while循環猜測遊戲中的while循環和if語句調試
- 15. JavaScript:循環一段時間
- 16. while循環一段時間
- 17. 從特定時間到另一個時間循環幾小時
- 18. 循環功能要求
- 19. 猜測環卡滯Java
- 20. 在java中循環一定時間和一定的任務
- 21. 猜測數字在時間限制內
- 22. 在while循環期間更新範圍,所以沒有愚蠢的猜測
- 23. 執行循環需要多長時間?
- 24. 循環一段Javascript的一段時間
- 25. 在Python循環中猜測遊戲的隨機數錯誤
- 26. 針對Java的猜測遊戲(打印不同語句循環)
- 27. 定時循環,2個循環之間的X秒的JavaScript
- 28. 循環掃描,同時要求一個字符串與空格
- 29. C++中的固定時間循環
- 30. 執行while循環規定的時間
歡迎StackExchange。這不是一項家庭作業寫作服務。你必須至少嘗試解決問題,並顯示你的嘗試,並詢問你出錯的地方。有關如何提出更好問題的示例,請參見[mcve]。 – AJNeufeld