Java随机数猜测游戏存储最低尝试次数

时间:2015-07-24 18:13:46

标签: java

我基本上已经完成了所有工作我只是不知道如何让程序记录每次会话的最佳分数而且我也不知道如何让程序将玩家的信息保存到每次"会话" .txt文件结束。 .txt文件需要采用用户名,去往次数,成功次数和最佳得分的格式。到目前为止,我的代码是

        import java.util.Random;
import java.util.Scanner;
import java.util.Writer;
import java.util.File; 

public class GuessingGame3 {

    public static void main(String[] args)
    {

        Random generator = new Random(); //This is were the computer selects the Target

        int guess;
        int count = 0;
        int Target;
        String userName;
        String playagain;
        boolean play = true;
        int session = 0;
        int sessions = 0;

        Scanner consoleIn = new Scanner(System.in); 
        Scanner name = new Scanner(System.in); 

        System.out.println("Hello! Please enter your name:\n"); //This is were the user enters his/her name
        userName= name.nextLine();

        System.out.println("Hello "+ userName + " :) Welcome to the game!\n");


        while (play = true)
        {
            session++;
            Target = generator.nextInt(100) + 1;
            System.out.println("Can you guess the number i'm thinking off? You will have 6 attempts to guess the correct number"); //This is where the computer asks the user to guess the number and how many guesses they will have

            do {
                guess = consoleIn.nextInt();
                count++;

                if (guess > Target)
                System.out.println("Sorry! Your guess was too high! Guess again :)"); //This is to help the player get to the answer 
                else 
                if (guess < Target)
                System.out.println("Sorry! Your guess was too low! Guess again :)"); //This is to help the player get to the answer 
               }        
                while(guess != Target && count <6);

                if(guess == Target) {
                System.out.println("Congratulations "+  userName + ", it took you "+ count +" attempts to guess correctly!"); //This tells the player that they got the correct answer and how many attempts it took
                    sessions++;
                        }

                else 
                {
                System.out.println("Sorry "+ userName + ", You've used up all of your guesses! The correct answer was "+ Target + "!");  //This tells the player that they failed to find the number and then tells them what the correct answer  
                }
                {
                Scanner answer = new Scanner(System.in);
                System.out.println("Would you like to play again "+ userName +"? [Y/N]");//This asks the player if they would like to play again
                playagain = answer.nextLine();
               if(playagain.equalsIgnoreCase("Y"))//This is what happens if the player opts to play again
                {
                play = true;
                count = 0;
                count++;

                } else if(playagain.equalsIgnoreCase("N"))//This is what happens if the player opts to exit the game
                {
                    play = false;
                    System.out.println("Thanks for playing "+ userName +"! :) Please come back soon!");

                    System.out.println("The number of goes you had: "+ session +"");
                    System.out.println("The number of times you guessed correctly: "+ sessions +"");
                    PrinterWriter writer = PrinterWriter("Record");
                    writer.println(userName, session, sessions);
                    break;
                }

             }
        }
    }
}       

1 个答案:

答案 0 :(得分:1)

要确定每个会话的最佳得分,您需要做的就是先从会话中保存计数并与之前的会话进行比较。

所以在你做了......之后..它看起来像

int lowestScore = 6;// or max number of guesses

...

while(play == true)
{
    ...

    do...while...
    if(count < lowestScore)
    {
         lowestScore = count;
    }

    ...
}

这将比较最低分数与用户开始新会话的当前分数。 另请注意while语句中的双等号。