Java猜谜游戏:需要改进
我是编程新手,我需要创建一个1-100随机数猜测游戏。计算机将选择一个数字,我们有5次尝试猜测它。它会提供一些提示,例如过高或过低。我已经设法得到了这个结果,但是 我还需要设置为按1显示新游戏,其中2为指令,3为退出。我还想知道如何添加5次尝试。非常感谢任何有用的网站或帮助。多谢各位Java猜谜游戏:需要改进,java,Java,我是编程新手,我需要创建一个1-100随机数猜测游戏。计算机将选择一个数字,我们有5次尝试猜测它。它会提供一些提示,例如过高或过低。我已经设法得到了这个结果,但是 我还需要设置为按1显示新游戏,其中2为指令,3为退出。我还想知道如何添加5次尝试。非常感谢任何有用的网站或帮助。多谢各位 import java.util.Scanner; public class NumberGuessingGame { public static void main(String[] args)
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 99 + 1);
Scanner keyboard = new Scanner(System.in);
int guess;
do {
System.out.print("Enter a guess (1-100): ");
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("Your guess is correct. Congratulations!");
else if (guess < secretNumber)
System.out
.println("Your guess is smaller than the secret number.");
else if (guess > secretNumber)
System.out
.println("Your guess is greater than the secret number.");
} while (guess != secretNumber);
}
}
import java.util.Scanner;
公开课号码游戏{
公共静态void main(字符串[]args){
int secretNumber;
secretNumber=(int)(Math.random()*99+1);
扫描仪键盘=新扫描仪(System.in);
智力猜测;
做{
系统输出打印(“输入猜测(1-100):”;
guess=keyboard.nextInt();
如果(猜测==秘密数字)
System.out.println(“您的猜测是正确的,祝贺您!”);
else if(猜测<秘密数字)
系统输出
.println(“你的猜测比秘密号码小。”);
else if(猜测>秘密编号)
系统输出
.println(“您的猜测大于秘密号码”);
}while(猜测!=秘密数字);
}
}
我建议您使用以下代码:
1.-对于菜单或选项,您必须创建一个单独的方法,在其中打印选项,并创建另一个用于说明的方法
2.-在创建这些方法之后,您应该使用case语句循环,这样您就可以选择1、2或3并使事情发生
3.-为了使您的程序只进行5次尝试,我建议您使用“count=0”之类的计数器,然后在每次尝试猜测do while循环结束时添加1,如“count+=1;或count=count+1;”
至于网站。我建议你要么看看Java官方文档,因为它们将证明是你职业生涯中最好的朋友。他们有很多从初学者到高级水平的例子。
如何让1显示新游戏,2显示指令,3显示退出 如果您想让这些输入中断程序,通常通过一个循环来检查输入,即
while(输入!=“1”&&input!=“2”…
您可以使用中断
条件退出循环并处理中断
如果你不需要中断,那么你可以把代码放在猜测之前,就像下面我做的那样
如何添加5次尝试
循环5次迭代,并相应地调整循环条件
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 99 + 1);
Scanner keyboard = new Scanner(System.in);
System.out.print("(1) displays new game\n (2) instructions\n (3) being exit");
int guess;
guess = keyboard.nextInt();
if (guess == 3) {
int nOfAttempts = 0;
do {
System.out.print("Enter a guess (1-100): ");
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("Your guess is correct. Congratulations!");
else if (guess < secretNumber)
System.out
.println("Your guess is smaller than the secret number.");
else if (guess > secretNumber)
System.out
.println("Your guess is greater than the secret number.");
nOfAttempts++;
} while (guess != secretNumber && nOfAttempts < 5);
}
}
}
import java.util.Scanner;
公开课号码游戏{
公共静态void main(字符串[]args){
int secretNumber;
secretNumber=(int)(Math.random()*99+1);
扫描仪键盘=新扫描仪(System.in);
System.out.print(“(1)显示新游戏\n(2)指令\n(3)正在退出”);
智力猜测;
guess=keyboard.nextInt();
如果(猜测=3){
int nOfAttempts=0;
做{
系统输出打印(“输入猜测(1-100):”;
guess=keyboard.nextInt();
如果(猜测==秘密数字)
System.out.println(“您的猜测是正确的,祝贺您!”);
else if(猜测<秘密数字)
系统输出
.println(“你的猜测比秘密号码小。”);
else if(猜测>秘密编号)
系统输出
.println(“您的猜测大于秘密号码”);
nOfAttempts++;
}while(guess!=secretNumber&&nOfAttempts<5);
}
}
}
“任何有用的网站[…]”-网站外内容与StackOverflow无关。--为了帮助您,我们需要知道您正在努力解决的概念。我们拥有的信息越多,我们可以提供的帮助就越好。--关于您的代码的一些备注:请减少代码中的空行数。-即使可能,您也不应忽略前后括号d如果
,否则
,…您自己的尝试出了什么问题?