猜谜游戏-随机#1到100,要求用户猜5次正确的数字…JAVA初学者
设计一个应用程序,从1到100随机选取一个数字,然后提示用户猜测该数字,警告用户只有5次尝试才能正确猜测。每次猜测后,向用户报告他或她是否正确,猜测是否过高或过低。如果猜对了,打印出祝贺信息。如果猜了5次都没有成功,打印一条消息说游戏结束了。游戏结束时,提示用户是否希望再次玩游戏 我特别需要结尾方面的帮助,我想代码的布局也需要帮助。。。我运行它,它不断运行,我已经尝试使用几个网站和我的教科书,但没有运气,提前谢谢你 Java,NetBeans 8.2猜谜游戏-随机#1到100,要求用户猜5次正确的数字…JAVA初学者,java,Java,设计一个应用程序,从1到100随机选取一个数字,然后提示用户猜测该数字,警告用户只有5次尝试才能正确猜测。每次猜测后,向用户报告他或她是否正确,猜测是否过高或过低。如果猜对了,打印出祝贺信息。如果猜了5次都没有成功,打印一条消息说游戏结束了。游戏结束时,提示用户是否希望再次玩游戏 我特别需要结尾方面的帮助,我想代码的布局也需要帮助。。。我运行它,它不断运行,我已经尝试使用几个网站和我的教科书,但没有运气,提前谢谢你 Java,NetBeans 8.2 import java.util.Rando
import java.util.Random;
import java.util.Scanner;
public class GuessingGame
{
public static void main (String[]args)
{
int answer, guess,attemptsNum = 0;
final int maxAttempts = 5;
String str, another = "y";
Scanner scan = new Scanner(System.in);
Random generator = new Random();
answer = generator.nextInt(101)+1;
System.out.println("Guess a number between 1 and 100");
System.out.println("Enter your guess (0 to quit):");
{
guess = scan.nextInt();
while (guess != 0)
{
if(guess==answer)
System.out.println("Right!");
else if (guess<answer)
System.out.println("Your guess was too LOW.");
else if (guess>answer)
System.out.println("Your guess was too HIGH.");
}
System.out.println("Want to Play again?(y/n)");
another = scan.next();
while (guess != answer && ++attemptsNum < maxAttempts)
if (attemptsNum == maxAttempts)
System.out.println ("The number was " + answer);
}
}
import java.util.Random;
导入java.util.Scanner;
公开课猜谜游戏
{
公共静态void main(字符串[]args)
{
int答案,猜测,尝试数=0;
最终整数最大尝试数=5;
字符串str,另一个=“y”;
扫描仪扫描=新扫描仪(System.in);
随机生成器=新随机();
答案=发电机。下一个(101)+1;
System.out.println(“猜一个介于1和100之间的数字”);
System.out.println(“输入您的猜测(0退出):”;
{
guess=scan.nextInt();
while(猜测!=0)
{
如果(猜测=答案)
System.out.println(“对!”);
else if(猜测答案)
System.out.println(“你的猜测太高了。”);
}
System.out.println(“想再次播放吗?(y/n)”);
另一个=scan.next();
while(猜测!=回答&&++attemptsNum
}
输入您的猜测(0退出):20
你的猜测太低了
输入您的猜测(0退出):35
你的猜测太低了
输入您的猜测(0退出):80
你猜得太高了
输入您的猜测(0退出):74
你猜得太高了
输入您的猜测(0退出):56
正确的!猜测:5
再次播放(y/n)?我看到外部while循环需要重新构造,一些逻辑放错了位置。试试修订版,希望对你有所帮助。如果你有任何问题,请评论我
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
int answer, guess, attemptsNum = 0;
final int maxAttempts = 5;
String str, another = "y";
Scanner scan = new Scanner(System.in);
Random generator = new Random();
answer = generator.nextInt(101) + 1;
while (another.equals("y") || another.equals("Y")) {
System.out.println("Guess a number between 1 and 100");
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
attemptsNum = 0;
while (guess != 0)
{
attemptsNum++;
if (guess == answer) {
System.out.println("Right!");
break;
} else if (guess < answer)
System.out.println("Your guess was too LOW.");
else if (guess > answer)
System.out.println("Your guess was too HIGH.");
if (attemptsNum == maxAttempts) {
System.out.println("The number was " + answer);
break;
}
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
}
System.out.println("Want to Play again?(y/n)");
another = scan.next();
}
System.out.println("Thanks for playing");
}
}
import java.util.Random;
导入java.util.Scanner;
公开课猜谜游戏{
公共静态void main(字符串[]args){
int答案,猜测,尝试数=0;
最终整数最大尝试数=5;
字符串str,另一个=“y”;
扫描仪扫描=新扫描仪(System.in);
随机生成器=新随机();
答案=发电机。下一个(101)+1;
而(另一个.equals(“y”)|另一个.equals(“y”)){
System.out.println(“猜一个介于1和100之间的数字”);
System.out.println(“输入您的猜测(0退出):”;
guess=scan.nextInt();
attemptsNum=0;
while(猜测!=0)
{
attemptsNum++;
如果(猜测=答案){
System.out.println(“对!”);
打破
}else if(猜测<回答)
System.out.println(“你的猜测太低了。”);
else if(猜测>回答)
System.out.println(“你的猜测太高了。”);
if(attemptsNum==maxAttempts){
System.out.println(“数字是”+答案);
打破
}
System.out.println(“输入您的猜测(0退出):”;
guess=scan.nextInt();
}
System.out.println(“想再次播放吗?(y/n)”);
另一个=scan.next();
}
System.out.println(“谢谢玩”);
}
}
我可以看出javapedia.net首先给出了答案,但我已经独立地给出了这个答案。为了便于阅读,我添加了括号。ATTEMPTSUM++被添加到if语句中,以防止猜测过高或过低
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
int answer, guess, attemptsNum = 0;
final int maxAttempts = 5;
String str, another = "y";
Scanner scan = new Scanner(System.in);
Random generator = new Random();
answer = generator.nextInt(101) + 1;
System.out.println("Guess a number between 1 and 100");
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
while (guess != 0) {
if (guess == answer) {
System.out.println("Right!");
guess = 0;
} else if (guess < answer) {
System.out.println("Your guess was too LOW.");
attemptsNum++;
guess = scan.nextInt();
} else {
System.out.println("Your guess was too HIGH.");
attemptsNum++;
guess = scan.nextInt();
}
if (attemptsNum >= (maxAttempts-1)) {
System.out.println("The number was " + answer);
}
}
}
}
import java.util.Random;
导入java.util.Scanner;
公开课猜谜游戏{
公共静态void main(字符串[]args){
int答案,猜测,尝试数=0;
最终整数最大尝试数=5;
字符串str,另一个=“y”;
扫描仪扫描=新扫描仪(System.in);
随机生成器=新随机();
答案=发电机。下一个(101)+1;
System.out.println(“猜一个介于1和100之间的数字”);
System.out.println(“输入您的猜测(0退出):”;
guess=scan.nextInt();
while(猜测!=0){
如果(猜测=答案){
System.out.println(“对!”);
猜测=0;
}else if(猜测<回答){
System.out.println(“你的猜测太低了。”);
attemptsNum++;
guess=scan.nextInt();
}否则{
System.out.println(“你的猜测太高了。”);
attemptsNum++;
guess=scan.nextInt();
}
如果(尝试次数>=(最大尝试次数-1)){
System.out.println(“数字是”+答案);
}
}
}
}
两条评论:
answer=generator.nextInt(100)+1代码>
publicstaticvoidmain
之外创建一个单独的方法。这是常见的做法,并且还允许您再次调用该方法import java.util.Random; import java.util.Scanner;
class Main {
public static Scanner scan = new Scanner(System.in);
public static void main (String[]args) {
runGame();
}
public static void runGame() {
String another = "";
int answer, guess,attemptsNum = 0;
final int maxAttempts = 5;
Random generator = new Random();
answer = generator.nextInt(100)+1;
System.out.println("Guess a number between 1 and 100");
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
while (guess != answer && attemptsNum < maxAttempts-1 && guess != 0 ) {
if(guess==answer)
System.out.println("Right!");
else if (guess<answer) {
System.out.println("Your guess was too LOW.");
attemptsNum++;
guess = scan.nextInt();
}
else {
System.out.println("Your guess was too HIGH.");
attemptsNum++;
guess = scan.nextInt();
}
}
System.out.println ("The number was " + answer);
System.out.println("Want to Play again?(y/n)");
another = scan.next();
System.out.println("another = " + another);
if(another.equals("y")) {
runGame();
} else {
System.out.println("Goodbye!");
}
}
}
import java.util.Random;导入java.util.Scanner;
班长{
公共静态扫描仪扫描=ne