Java 猜谜游戏-它赢了';t形环

Java 猜谜游戏-它赢了';t形环,java,loops,jcreator,Java,Loops,Jcreator,作为我们课程的一部分,我和我班上的另一个人必须做一个猜测游戏,其中随机数在1到100之间生成,用户最多有6次猜测来尝试猜测数字。我还必须创建一个“会话”,用户可以在其中输入自己的姓名,并将结果存储到文本文件中。我已经让它工作到一个点,他们可以玩游戏,并成功地输入自己的名字,但如果你选择的选项,你想玩一次,它会说'进程完成'并退出程序。非常感谢您的帮助,以下是我迄今为止所做的 代码: import java.util.Random//导入随机类 导入java.util.Scanner//导入Sca

作为我们课程的一部分,我和我班上的另一个人必须做一个猜测游戏,其中随机数在1到100之间生成,用户最多有6次猜测来尝试猜测数字。我还必须创建一个“会话”,用户可以在其中输入自己的姓名,并将结果存储到文本文件中。我已经让它工作到一个点,他们可以玩游戏,并成功地输入自己的名字,但如果你选择的选项,你想玩一次,它会说'进程完成'并退出程序。非常感谢您的帮助,以下是我迄今为止所做的

代码:

import java.util.Random//导入随机类
导入java.util.Scanner//导入Scanner类
/*作者劳拉·布朗2014年2月28日*/
公开课猜谜游戏
{ 
公共静态void main(字符串[]args)
{ 
Random generator=new Random();//创建一个介于1-100之间的随机数
int TARGET=generator.nextInt(100)+1;//建立目标编号
智力猜测;
整数计数=0;
字符串用户名;
字符串另一个=“y”;
布尔标志=假;
布尔值anotherFlag=true;
Scanner consoleIn=new Scanner(System.in);//创建新的Scanner对象
Scanner name=new Scanner(System.in);//创建新的Scanner对象
System.out.print(“您好!请输入您的姓名:\n”);//请求用户输入
userName=name.nextLine();
System.out.print(“你好”+用户名+”,欢迎来到游戏!\n);
System.out.print(“您能猜出它是什么吗?\n”);
执行{//开始循环
guess=consoleIn.nextInt();
计数++;
如果(猜测>目标)
System.out.print(“抱歉-您的猜测太高了\n”);
其他的
如果(猜测<目标)
System.out.print(“对不起,您的猜测太低了\n”);
}
while(猜测!=目标和计数<6);
如果(猜测==目标){
System.out.println(“恭喜!-你找到了!”);
System.out.println();
}
否则{
System.out.println(“对不起,您已经使用了所有6个猜测”);
}
System.out.println();
System.out.println(“您想再猜一次吗?(是/否)”;
另一个=consoleIn.next();
}
}
公共静态void main(字符串[]args)
{ 
Random generator=new Random();//创建一个介于1-100之间的随机数
智力猜测;
整数计数=0;
int目标;
字符串用户名;
字符串另一个=“y”;
布尔标志=假;
布尔值anotherFlag=true;
Scanner consoleIn=new Scanner(System.in);//创建新的Scanner对象
Scanner name=new Scanner(System.in);//创建新的Scanner对象
System.out.print(“您好!请输入您的姓名:\n”);//请求用户输入
userName=name.nextLine();
System.out.print(“你好”+用户名+”,欢迎来到游戏!\n);
while(other.equalsIgnoreCase(“y”)//这将检查用户是否输入“other”
{
TARGET=generator.nextInt(100)+1;//建立目标编号
System.out.print(“您能猜出它是什么吗?\n”);
执行{//开始循环
guess=consoleIn.nextInt();
计数++;
如果(猜测>目标)
System.out.print(“抱歉-您的猜测太高了\n”);
其他的
如果(猜测<目标)
System.out.print(“对不起,您的猜测太低了\n”);
}
while(猜测!=目标和计数<6);
如果(猜测==目标){
System.out.println(“恭喜!-你找到了!”);
System.out.println();
}
否则{
System.out.println(“对不起,您已经使用了所有6个猜测”);
}
System.out.println();
System.out.println(“您想再猜一次吗?(是/否)”;
另一个=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(); 


}
while(另一个标志)
{
TARGET=generator.nextInt(100)+1;//建立目标编号
System.out.print(“您能猜出它是什么吗?\n”);
做
{//开始循环
guess=consoleIn.nextInt();
计数++;
如果(猜测>目标)
System.out.print(“抱歉-您的猜测太高了\n”);
其他的
如果(猜测<目标)
System.out.print(“对不起,您的猜测太低了\n”);
}
while(猜测!=目标和计数<6);
如果(猜测==目标){
System.out.println(“恭喜!-你找到了!”);
System.out.println();
}
其他的
{
System.out.println(“对不起,您已经使用了所有6个猜测”);
}
System.out.println();
System.out.println(“您想再猜一次吗?(是/否)”;
另一个=consoleIn.next();
}

如果用户输入“否”,则需要将另一个标志设置为false。这应该是一个相对简单的练习,但是上面的练习会让程序反复循环

您的
main()
方法不应在游戏结束时结束。我建议编写一个包含游戏逻辑的
playGame
方法(这基本上就是
main()
现在正在做的),然后在用户输入他想玩另一个游戏时从
main()
方法调用它。您还可以从那里实现“会话”管理。@XaviLópez这完全取决于游戏的要求。您需要第二个do while循环,其中while条件检查游戏是否应该重复。我不会再多说了,因为上面的代码表明您已经知道如何编写do-while循环。请不要在任何上下文中编写
==true
。它什么也不做。
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();

    }
 }
}
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(); 


}