[英]Guessing Game - it won't loop
作为我们课程的一部分,我本人和班上的另一个人必须做一个猜游戏,在该游戏中会生成一个1到100之间的随机数,并且用户最多可以猜测6个猜测和猜数字。 我还必须创建一个“会话”,用户可以在其中输入其名称,并将其结果存储到文本文件中。 我的工作已经到他们可以玩游戏并成功输入他们的名字的地步,但是如果您选择要再次玩的选项,它会显示“处理完成”并退出程序。 非常感谢您的帮助,这是我到目前为止所拥有的;
码:
import java.util.Random; //importing the Random class
import java.util.Scanner; //importing the Scanner class
/* Author Laura Brown 28/02/2014 */
public class TheGuessingGame
{
public static void main(String[] args)
{
Random generator = new Random(); //creates a random number between 1-100
int TARGET = generator.nextInt(100) + 1; //establishes the target number
int guess;
int count = 0;
String userName;
String another = "y";
Boolean flag = false;
Boolean anotherFlag = true;
Scanner consoleIn = new Scanner(System.in); //creating a new Scanner object
Scanner name = new Scanner(System.in); //creating a new Scanner object
System.out.print("Hello! Please enter your name:\n"); //asking for user input
userName = name.nextLine();
System.out.print("Hello "+ userName+ ", and welcome to the game!\n");
System.out.print("Can you guess what it is?\n");
do { //beginning the loop
guess = consoleIn.nextInt();
count++;
if (guess > TARGET)
System.out.print("Sorry - Your guess is too high \n");
else
if (guess < TARGET)
System.out.print("Sorry - Your guess is too low \n");
}
while(guess != TARGET && count < 6);
if(guess == TARGET) {
System.out.println("Congratulations! - You found it!");
System.out.println();
}
else {
System.out.println("Sorry - You have used all 6 guesses");
}
System.out.println();
System.out.println("Would you like to guess again? (yes/no)");
another = consoleIn.next();
}
}
public static void main(String[] args)
{
Random generator = new Random(); //creates a random number between 1-100
int guess;
int count = 0;
int Target;
String userName;
String another = "y";
Boolean flag = false;
Boolean anotherFlag = true;
Scanner consoleIn = new Scanner(System.in); //creating a new Scanner object
Scanner name = new Scanner(System.in); //creating a new Scanner object
System.out.print("Hello! Please enter your name:\n"); //asking for user input
userName = name.nextLine();
System.out.print("Hello "+ userName+ ", and welcome to the game!\n");
while (another.equalsIgnoreCase("y")) // this will check if your user input "another"
{
TARGET = generator.nextInt(100) + 1; //establishes the target number
System.out.print("Can you guess what it is?\n");
do { //beginning the loop
guess = consoleIn.nextInt();
count++;
if (guess > TARGET)
System.out.print("Sorry - Your guess is too high \n");
else
if (guess < TARGET)
System.out.print("Sorry - Your guess is too low \n");
}
while(guess != TARGET && count < 6);
if(guess == TARGET) {
System.out.println("Congratulations! - You found it!");
System.out.println();
}
else {
System.out.println("Sorry - You have used all 6 guesses");
}
System.out.println();
System.out.println("Would you like to guess again? (yes/no)");
another = consoleIn.next();
consoleIn.nextLine();
}
}
}
在另一个循环中添加以循环整个游戏过程。 并把随机生成方法放到循环内部以重新生成一个新的数字。 试试吧。 无论如何,我添加了另一行consoleIn.nextLine(); 用户输入后,清除.next()方法的回车缓冲区。
您需要添加另一个循环。 从风格上讲,我会避免使用do ... while循环,因为它们很难阅读。 我加入了一个while循环,以更传统的方式向您展示了它们是多么幸福。
while(anotherFlag)
{
TARGET = generator.nextInt(100) + 1; //establishes the target number
System.out.print("Can you guess what it is?\n");
do
{ //beginning the loop
guess = consoleIn.nextInt();
count++;
if (guess > TARGET)
System.out.print("Sorry - Your guess is too high \n");
else
if (guess < TARGET)
System.out.print("Sorry - Your guess is too low \n");
}
while(guess != TARGET && count < 6);
if(guess == TARGET) {
System.out.println("Congratulations! - You found it!");
System.out.println();
}
else
{
System.out.println("Sorry - You have used all 6 guesses");
}
System.out.println();
System.out.println("Would you like to guess again? (yes/no)");
another = consoleIn.next();
}
上面的方法无法完全正常工作,如果用户输入“否”,则需要将anotherFlag设置为false。 那应该是一个相对简单的练习,但是上面的操作会使程序反复循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.