Java 在循环中尝试一定次数后打印打印语句
我不熟悉编码。我创建了一个猜谜游戏,它运行得很好,但是,我想知道如何制作它,以便在用户尝试猜3次数字后,他们得到我在最后一行给出的提示,但它当前无法访问,我不知道如何使语句可访问,并且在Java 在循环中尝试一定次数后打印打印语句,java,nested-loops,do-while,Java,Nested Loops,Do While,我不熟悉编码。我创建了一个猜谜游戏,它运行得很好,但是,我想知道如何制作它,以便在用户尝试猜3次数字后,他们得到我在最后一行给出的提示,但它当前无法访问,我不知道如何使语句可访问,并且在dowhile循环中。我现在被卡住了。多谢各位 import java.util.Scanner; public class guessing_game { public static void main (String[] args) { Scanner kb = new Scanner(S
do
while
循环中。我现在被卡住了。多谢各位
import java.util.Scanner;
public class guessing_game {
public static void main (String[] args) {
Scanner kb = new Scanner(System.in);
desc();
run(kb);
//int nun = 0;
//for (int i = 0; i < nun; nun ++)
}
public static void desc() {
System.out.println("This is a guessing game.");
System.out.println();
System.out.println("Let's see how many tries it takes you to guess the right number!");
System.out.println();
System.out.println();
System.out.println();
}
public static int run(Scanner kb) {
System.out.println("Please enter a number between 1-100");
int guess = kb.nextInt();
int num = 44;
int tries = 0;
do {
if (guess < num) {
System.out.println("Oooh. Your guess is too low. Try again.");
System.out.println();
run(kb);
}
else if ((guess > 100) || (guess < 0)) {
System.out.println("That isn't between 1-100 is it?");
System.out.println();
run(kb);
}
else if (guess > num) {
System.out.println("Aaah. Your guess is too high. Try again.");
System.out.println();
run(kb);
}
else if(guess == num) {
System.out.println("Bingo!!! Nice guess bud.");
System.out.println("Tell a friend to play! Wanna try again? (y or n)");
String choice = kb.next();
if (choice.equalsIgnoreCase("y")) {
run(kb);
}
else if (choice.equalsIgnoreCase("n")) {
System.exit(0);
}
}
tries++;
}while(tries < 3);
{
System.out.print("Here's a hint the lucky number is 4");
}
return guess;
}
}
import java.util.Scanner;
公开课猜谜游戏{
公共静态void main(字符串[]args){
扫描仪kb=新扫描仪(System.in);
desc();
运行(kb);
//int nun=0;
//for(int i=0;i100)| |(猜测<0)){
System.out.println(“这不是在1-100之间,是吗?”);
System.out.println();
运行(kb);
}
else if(猜测>数值){
System.out.println(“啊,你的猜测太高了,再试一次。”);
System.out.println();
运行(kb);
}
else if(猜测==num){
System.out.println(“宾果!!!猜得好,巴德”);
System.out.println(“告诉朋友玩!想再试一次吗?(y或n)”;
字符串选择=kb.next();
if(选择相等信号情况(“y”)){
运行(kb);
}
else if(选择.相等信号情况(“n”)){
系统出口(0);
}
}
尝试++;
}而(t<3);
{
System.out.print(“这里有一个提示,幸运数字是4”);
}
返回猜测;
}
}
您的程序存在一些流问题,但这里有一个简单的解决方法
首先,当您从run()
方法返回guess
值时,实际上并没有使用该值,因此可以删除该值
此外,在这种情况下,您不希望使用do/while
循环,而只希望使用while
。您希望不断重复提示,直到用户猜对为止。因此,添加一个布尔值
,以允许您检查他们是否获胜:
boolean correct=false代码>-我们将开始设置为false,因为他们还没有开始
现在,不要在每次猜测后再次调用run()
(这会重置trys
每次计数),只需让while
循环完成它的工作并重复它自己。因此,我们需要将提示输入移到while
循环中
以下是更改的完整代码列表:
import java.util.Scanner;
public class guessing_game {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
desc();
run(kb);
//int nun = 0;
//for (int i = 0; i < nun; nun ++)
}
public static void desc() {
System.out.println("This is a guessing game.");
System.out.println();
System.out.println("Let's see how many tries it takes you to guess the right number!");
System.out.println();
System.out.println();
System.out.println();
}
// Change the return type to void as you never use the value returned
public static void run(Scanner kb) {
int num = 44;
// Add a boolean to determine if the game is won
boolean correct = false;
int tries = 0;
while (!correct) {
System.out.println("Please enter a number between 1-100");
int guess = kb.nextInt();
if (guess < num) {
System.out.println("Oooh. Your guess is too low. Try again.");
System.out.println();
} else if ((guess > 100) || (guess < 0)) {
System.out.println("That isn't between 1-100 is it?");
System.out.println();
} else if (guess > num) {
System.out.println("Aaah. Your guess is too high. Try again.");
System.out.println();
} else if (guess == num) {
// Flag the guess as correct; this will exit the loop after this run
correct = true;
System.out.println("Bingo!!! Nice guess bud.");
System.out.println("Tell a friend to play! Wanna try again? (y or n)");
String choice = kb.next();
if (choice.equalsIgnoreCase("y")) {
run(kb);
} else if (choice.equalsIgnoreCase("n")) {
System.exit(0);
}
}
tries++;
}
}
}
import java.util.Scanner;
公开课猜谜游戏{
公共静态void main(字符串[]args){
扫描仪kb=新扫描仪(System.in);
desc();
运行(kb);
//int nun=0;
//for(int i=0;i100)| |(猜测<0)){
System.out.println(“这不是在1-100之间,是吗?”);
System.out.println();
}else if(猜测>数值){
System.out.println(“啊,你的猜测太高了,再试一次。”);
System.out.println();
}else if(猜测==num){
//将猜测标记为正确;这将在此运行后退出循环
正确=正确;
System.out.println(“宾果!!!猜得好,巴德”);
System.out.println(“告诉朋友玩!想再试一次吗?(y或n)”;
字符串选择=kb.next();
if(选择相等信号情况(“y”)){
运行(kb);
}else if(选择.相等信号情况(“n”)){
系统出口(0);
}
}
尝试++;
}
}
}
1.您正在递归调用run()方法,每次调用此方法时,将创建一个新变量try并将其初始化为零。
2.递归调用在条件检查之前,由于同样的原因,逻辑可能永远不会到达条件检查
为了使这项工作的改动最小,您可以使用以下代码。但这不是最好的,因为它不能解决上述缺点
import java.util.Scanner;
公开课猜谜游戏{
static int tries = 0;
public static void main (String[] args)
{
Scanner kb = new Scanner(System.in);
desc();
run(kb);
//int nun = 0;
//for (int i = 0; i < nun; nun ++)
}
public static void desc()
{
System.out.println("This is a guessing game.");
System.out.println();
System.out.println("Let's see how many tries it takes you to guess the right number!");
System.out.println();
System.out.println();
System.out.println();
}
public static int run(Scanner kb)
{
System.out.println("Please enter a number between 1-100");
int guess = kb.nextInt();
int num = 44;
do{
tries++;
if(tries>=3) break;
if (guess < num)
{
System.out.println("Oooh. Your guess is too low. Try again.");
System.out.println();
run(kb);
}
else if ((guess > 100) || (guess < 0))
{
System.out.println("That isn't between 1-100 is it?");
System.out.println();
run(kb);
}
else if (guess > num)
{
System.out.println("Aaah. Your guess is too high. Try again.");
System.out.println();
run(kb);
}
else if(guess == num)
{
System.out.println("Bingo!!! Nice guess bud.");
System.out.println("Tell a friend to play! Wanna try again? (y or n)");
String choice = kb.next();
if (choice.equalsIgnoreCase("y"))
{
run(kb);
}
else if (choice.equalsIgnoreCase("n"))
{
System.exit(0);
}
}
}while( tries < 3);
{
System.out.print("Here's a hint the lucky number is 4");
}
return guess;
}
}
static int trys=0;
公共静态void main(字符串[]args)
{
扫描仪kb=新扫描仪(System.in);
desc();
运行(kb);
//int nun=0;
//for(int i=0;i