java数字猜测中的while循环
所以我一直在做这件事,效果很好,唯一的问题是,当你猜到一个不在1到100之间的数字后,它会进入下一个猜测,当这个尝试不应该对你不利时。我已经附上了输出应该是什么样子的图片 我需要“for”语句还是布尔变量 这是我的代码:java数字猜测中的while循环,java,loops,Java,Loops,所以我一直在做这件事,效果很好,唯一的问题是,当你猜到一个不在1到100之间的数字后,它会进入下一个猜测,当这个尝试不应该对你不利时。我已经附上了输出应该是什么样子的图片 我需要“for”语句还是布尔变量 这是我的代码: Random generator = new Random(); //import the scanner for prompt Scanner input = new Scanner(System.in); //integers for the secret numbe
Random generator = new Random();
//import the scanner for prompt
Scanner input = new Scanner(System.in);
//integers for the secret number, input guess of the secret number, tries and limit to tries
int numberToGuess = 1 + generator.nextInt(100);
int numberOfTries = 1;
int limit = 10;
int guess;
//prompt user to enter a number to guess
System.out.println("You have 10 tries to guess a number between 1 and 100");
//start of while loop
while (numberOfTries <= 10) {
System.out.print("Guess number " + numberOfTries + ": ");
guess = input.nextInt();
//if else statements (outputs)
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! You have correctly guess the number in " + numberOfTries + " tries");
System.exit(0);
} else {
System.out.println("Sorry, you did not guess the guess the answer in 10 tries");
System.out.println("The number was " + numberToGuess);
//break to end while loop
break;
}
numberOfTries++;
// If statement after executing the while loop the output if user loses and answer to secret number
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);
}
}
Random generator=new Random();
//导入扫描仪以进行提示
扫描仪输入=新扫描仪(System.in);
//密码的整数,输入密码的猜测,尝试次数和限制尝试次数
int numberToGuess=1+生成器.nextInt(100);
int numberofthries=1;
整数极限=10;
智力猜测;
//提示用户输入要猜测的数字
println(“您有10次尝试猜测1到100之间的数字”);
//while循环的开始
while(次数100)
System.out.println(“猜测应该在1到100之间”);
否则如果(猜测>数字猜测)
System.out.println(“太高,请重试”);
else if(guess==numberToGuess){
System.out.println(“祝贺您!您已经正确地猜出了”+numberofthries+“thries”中的数字”);
系统出口(0);
}否则{
System.out.println(“对不起,您没有在10次尝试中猜到答案”);
System.out.println(“数字是”+numberToGuess);
//循环时从中断到结束
打破
}
numberofthries++;
//执行while循环后的If语句,如果用户丢失,则输出并回答密码
如果(尝试次数>10){
System.out.println(“对不起,您没有在10次尝试中猜到答案”);
System.out.println(“数字是”+numberToGuess);
}
}
不要增加numberofthries++;如果他们猜到一个超出范围的数字。将增量放入if语句中尝试在主while
循环中使用嵌套的do while
循环:
//start of while loop
while(numberOfTries <= 10){
//get a valid guess
do{
boolean valid = true;
System.out.print("Guess number " + numberOfTries + ": ");
guess= input.nextInt();
//if else statements (outputs)
if (guess < numberToGuess){
System.out.println("Your guess is too low. Try again.");
valid = false;
}
else if (guess > 100){
System.out.println("Guesses should be between 1 and 100.");
valid = false;
}
while (!valid);
//handle the valid guess
}
//while循环的开始
while(次数100){
System.out.println(“猜测应该在1到100之间”);
有效=错误;
}
而(!有效);
//处理正确的猜测
}
以下是我的做法。你告诉我你喜欢哪一个:
import java.util.Random;
import java.util.Scanner;
/**
* Created by Michael
* Creation date 3/4/2016.
* @link https://stackoverflow.com/questions/35809392/while-loop-in-java-number-guess
*/
public class NumberGuess {
public static final int MIN_VALUE = 1;
public static final int MAX_VALUE = 100;
public static final int MAX_TRIES = 10;
public static void main(String[] args) {
Random generator = new Random();
Scanner input = new Scanner(System.in);
String playAgain;
do {
int answer = MIN_VALUE + generator.nextInt(MAX_VALUE);
System.out.println(String.format("You have %d tries to guess a number between %d and %d", MAX_TRIES, MIN_VALUE, MAX_VALUE));
boolean correct = playGuessingGame(input, answer);
if (!correct) {
System.out.println(String.format("The number was %d", answer));
}
System.out.print("Play again? [Y/N]: ");
playAgain = input.nextLine();
} while ("Y".equalsIgnoreCase(playAgain));
}
public static boolean playGuessingGame(Scanner input, int answer) {
boolean correct = false;
int numTries = 0;
do {
System.out.print(String.format("Guess %d: ", ++numTries));
int guess = Integer.parseInt(input.nextLine());
if (guess < MIN_VALUE) {
System.out.println(String.format("Guess %d is below minimum %d", guess, MIN_VALUE));
} else if (guess > MAX_VALUE) {
System.out.println(String.format("Guess %d is above maximum %d", guess, MAX_VALUE));
} else if (guess < answer) {
System.out.println(String.format("Guess %d is too low. Try again.", guess));
} else if (guess > answer) {
System.out.println(String.format("Guess %d is too high. Try again.", guess));
} else {
System.out.println("Woo hoo! You got it right!");
correct = true;
break;
}
} while (numTries < MAX_TRIES);
return correct;
}
}
import java.util.Random;
导入java.util.Scanner;
/**
*迈克尔创作的
*创建日期:2016年3月4日。
*@linkhttps://stackoverflow.com/questions/35809392/while-loop-in-java-number-guess
*/
公共类编号{
公共静态最终整数最小值=1;
公共静态最终int最大值=100;
公共静态最终整数最大值=10;
公共静态void main(字符串[]args){
随机生成器=新随机();
扫描仪输入=新扫描仪(System.in);
再次演奏弦乐;
做{
int answer=最小值+生成器.nextInt(最大值);
System.out.println(String.format(“您有%d次尝试猜测一个介于%d和%d之间的数字”,MAX_次尝试,MIN_值,MAX_值));
布尔正确=playGuessingGame(输入,回答);
如果(!正确){
System.out.println(String.format(“数字是%d”,答案));
}
系统输出打印(“再次播放?[Y/N]:”;
再次播放=input.nextLine();
}while(“Y”.equalsIgnoreCase(playreach));
}
公共静态布尔PlayGussingGame(扫描仪输入,整数回答){
布尔正确=错误;
整数=0;
做{
System.out.print(String.format(“猜测%d:,++numTries”);
int guess=Integer.parseInt(input.nextLine());
如果(猜测<最小值){
System.out.println(String.format(“猜测%d低于最小%d”,猜测,最小值));
}否则如果(猜测>最大值){
System.out.println(String.format(“猜测%d高于最大%d”,猜测,最大值));
}else if(猜测<回答){
System.out.println(String.format(“猜测%d太低。请重试。”,猜测));
}else if(猜测>回答){
System.out.println(String.format(“猜测%d太高,请重试。”,猜测));
}否则{
System.out.println(“呜呼!你说对了!”);
正确=正确;
打破
}
}而(数值<最大值);
返回正确;
}
}
你和你的教授意识到输掉这场比赛几乎是不可能的。对吧?
一分为二的策略是,你只要试10次就可以得到正确的答案。将该数字缩小以应对挑战。递增应该在if语句中完成。t:D必须是当今世界上的“边学习边循环”。哈哈,我正在尝试。我开始学习HTML和Ruby,进入java,这对我来说要困难得多。但愿我能先学会这一点!一句忠告:丢掉那些无用的评论。它们只会增加混乱,使代码更难阅读。它们提供的信息比可读的、干净的代码要少。我同意你的看法,尽管我的教授对评论非常敏感,他希望所有评论都能解释它的含义。别紧张,我也老了。这不是年龄的问题。我会贴些东西给你看看我的意思。由你决定。简单而优雅+1非常干净!我刚刚运行了它,唯一的问题是,一旦你猜到一个超过100的数字,就不应该再进行下一次猜测。它应该保持在同一个。不管怎样,要解决这个问题吗?我必须重新提交程序,因为他说我需要两个循环。我猜是do,whileHere是他的标题中的一个片段:“然而,我们将对如何编写这个程序施加限制。内环必须是作为do while循环的程序,外环必须作为由布尔变量控制的while循环进行编程。例如,外环可能以while(!done)开始您的程序应该只接受1到100之间的整数。如果用户试图猜测超出此范围的数字