猜猜游戏 - 它不会循环

时间:2014-04-03 08:59:35

标签: java loops jcreator

作为我们课程作业的一部分,我和我班上的另一个人必须制作一个猜测游戏,其中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();

    }
}

2 个答案:

答案 0 :(得分:0)

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()方法的回车缓冲区。

答案 1 :(得分:0)

您需要添加另一个循环。从风格上讲,我会避免使用do ... 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(); 


}

如果用户输入'no',则需要将anotherFlag设置为false。这应该是一个相对简单的练习,但上面的内容会让程序反复循环。