记分牌在一个简单的猜谜游戏

时间:2012-09-28 12:51:50

标签: java

以下是猜谜游戏的代码。

我正在尝试实现一个记分牌,该记分牌在玩家输入他们的名字后显示。只要应用程序正在运行,记分板就应该保存变量playernamecounttotalTime

它也应该按猜测量排序(较低的猜测量=在记分牌上更高)。

如果两个玩家有相同数量的猜测,那么它的排序时间会更快。我试过没有运气就做了一个arraylist。每次游戏重置时,我都试图让索引增加1,所以变量不会被新的覆盖。

我也无法让arraylist接受长字符串。

import java.util. *;

class Game {        

public void start() {

    int randomNumber = (int) (Math.random() * 1000);
    int number = -1, index, count;  
    String decision, guess, playername;
    long currentTime = 0, newTime, totalTime;
    boolean quitting = false;
    count = 0;
    System.out.println("Welcome to the Guessing Game");
    System.out.println("Type \"quit\" to quit at anytime.");
    Scanner scan  = new Scanner(System.in);
    System.out.println("Try to guess the number. Range is from 1 - 1000: ");
    guess = scan.nextLine();
    if ("quit".equals(guess)) {
        System.out.println("Quitting...");
        quitting = true;
    } else {
        number = Integer.parseInt(guess);
    }

    while (number != randomNumber && number > -1){
        if (number >= 1001 || number <= 1) {
            System.out.println("Number is not between 1 - 1000. Enter another number");
        }

        if (number != randomNumber && number > randomNumber && number <= 1000 && number >= 1) {
            System.out.println("Number is too high. Try again!");
        }

        if (number != randomNumber && number < randomNumber && number < 1000 && number > 1) {
            System.out.println("Number is too low. Try again!");
        }

        if (number != randomNumber) {
            count = count + 1;
        }

        if (number != randomNumber) {
            Scanner scannew  = new Scanner(System.in);
            System.out.println("Enter a different number: ");
            guess = scannew.nextLine();
            if ("quit".equals(guess)) {
                System.out.println("Quitting...");
                quitting = true;
            } else {
                number = Integer.parseInt(guess);
            }
        }

        if (count == 1) {
            long lDateTime = new Date().getTime();
            currentTime = lDateTime / 1000;
        }

        if (quitting) {
            break;
        }
    }

    if (!quitting) {
        long DateTime = new Date().getTime();
        newTime = DateTime / 1000;
        totalTime = newTime - currentTime;

        System.out.println("You win! Your total amount of guesses was: " + count + " Total time (seconds): " + (totalTime));

        Scanner name  = new Scanner(System.in);
        System.out.println("Enter your name: ");
        playername = scan.nextLine();


        //ArrayList<Integer> score = new ArrayList<Integer>();
        //score.add(index, count);
        //System.out.println(score);
        //index++;

        Scanner decision1 = new Scanner(System.in);
        System.out.println("Do you want to play again? y/n");
        decision = decision1.nextLine();

        if ("y".equals(decision)) {
            Game one = new Game();
            one.start();
        }

        if ("n".equals(decision)) {
            System.out.println("Quitting...");
        }
    }
    scan.close();
}
}

1 个答案:

答案 0 :(得分:0)

每次向其添加分数时,您都会重新初始化ArrayList,因此它永远不会包含多个值。只需初始化一次,以及randomNumber等。

此外,您不需要index。只有score.add(count)会在已经存在的所有内容之后添加新的分数。