1

`我刚刚在java中完成了猜谜游戏,但现在我需要为这个游戏创建一个highs核心,即使在代码停止后(我使用eclipse)也会存储名称、时间和猜测数。谁能帮我弄一张高分表?这是游戏:

import java.util.*;

public class Game {

public static void main (String[]args){
    Game guessing = new Game();
    guessing.start();

}
public void start() {

    System.out.println("Welcome to guessing game!"); 
    System.out.println("Please enter the number between 1 and 1000");

    Scanner input = new Scanner(System.in);
    String y = "yes";
    String n = "no";    
    String playerName;
    String currentGuess;
     String quit = "quit";
     int tries = 0;  //number of times player guessed
     int guess = 0;  //number that player inputs
     long startTime = System.currentTimeMillis();  //start timer after first guess
     int randomNumber = (int) (Math.random() * 999 + 1); // generating random number
     System.out.println(randomNumber);     // to be deleted after game is finished
     currentGuess = input.nextLine();


do{


    if (currentGuess.matches("[0-9]+")) {  //if input is non-numeric value, show message
        int numericInput = Integer.parseInt(currentGuess);
    }
    else {
    System.out.println("You have enetered non-numeric value,please try again");
    currentGuess = input.nextLine();
    continue; //allows to enter non-numeric values multiple times without crash
    }

    guess = Integer.parseInt(currentGuess);


    if(guess<1 || guess>1000 ){ //if the input number is out of range
        System.out.println("The number is out of range! Please try again");
        currentGuess = input.nextLine();
        continue;
    }

    if (guess>randomNumber){
        System.out.println("Oops,too high!");
        currentGuess = input.nextLine();
        tries++;
    }
    else if (guess<randomNumber){
        System.out.println("Sorry, to low!");
        currentGuess = input.nextLine();
        tries++;
    }



    if(currentGuess.equalsIgnoreCase(quit)){
        System.out.println("*****Thanx for playing! See you next time!*****");
    }

    if (guess==randomNumber){
        tries++;
        long endTime = System.currentTimeMillis();//stop the timer after number is guessed         correctly
        long gameTime = endTime - startTime; //display game time after input number==random   number
        System.out.println("Well done! You won the game in " + tries + " guesses " + "and " +    gameTime/1000 +  " seconds!");
        System.out.println("Please enter your name: ");
        playerName = input.nextLine();    //input for player name
        System.out.println("Would you like to play again? y/n..."); 
                currentGuess = input.nextLine();
                }                              /*input for "yes" or "no".. 
                                                  *MUST type "yes" or "no"! instead of "y" or   "n"!!*/

    if (currentGuess.equalsIgnoreCase(n)){
        System.out.println("******See you next time");

    }

    if (currentGuess.equalsIgnoreCase(y)) {
        start();

    }
    }

while(guess!=randomNumber && !currentGuess.equalsIgnoreCase(quit));  /*keep running the loop    until input
                                                                 *equals random number, or until   input equals
                                                                 * "quit" */


}
}
4

1 回答 1

1

您可以将结果保存到用户目录中的 txt 文件中。当您的程序启动时,您可以在初始化程序时阅读它。编写文本文件的示例

于 2012-10-03T13:56:49.053 回答