简体   繁体   English

Java随机数猜测游戏商店的最低尝试次数

[英]Java random number guessing game store lowest amount of attempts

I basically have everything done I just don't know how to get the program to record the best score per session and I also don't understand how to get the program to save the information of the player to a .txt file everytime the "session" ends. 我基本上已经完成了所有工作,只是我不知道如何使该程序记录每个会话的最佳成绩,而且我也不知道如何使该程序将播放器的信息每次都保存到.txt文件中。会话”结束。 The .txt file needs to be in the format username, number of goes, number of successful goes and best score. .txt文件的格式应为用户名,运行次数,成功运行次数和最佳成绩。 My code so far is 到目前为止,我的代码是

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

             }
        }
    }
}       

To determine the best score per session, all you need to do is first save the count from the session and compare with the previous session. 要确定每个会话的最佳分数,您需要做的就是首先保存会话中的计数并与上一个会话进行比较。

So after your do..while.. it would look something like 所以在你做..while ..之后,它看起来像

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

...

while(play == true)
{
    ...

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

    ...
}

This will compare the lowestScore with the current score evertime the user starts a new session. 每当用户开始新的会话时,这会将最低分数与当前分数进行比较。 Also, note the double equal sign in the while statement. 另外,请注意while语句中的双等号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM