[英]Java random number guessing game store lowest amount of attempts
我基本上已經完成了所有工作,只是我不知道如何使該程序記錄每個會話的最佳成績,而且我也不知道如何使該程序將播放器的信息每次都保存到.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;
}
}
}
}
}
要確定每個會話的最佳分數,您需要做的就是首先保存會話中的計數並與上一個會話進行比較。
所以在你做..while ..之后,它看起來像
int lowestScore = 6;// or max number of guesses
...
while(play == true)
{
...
do...while...
if(count < lowestScore)
{
lowestScore = count;
}
...
}
每當用戶開始新的會話時,這會將最低分數與當前分數進行比較。 另外,請注意while語句中的雙等號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.