1

下面是我猜谜游戏的代码。

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

它也应该按猜测的数量排序(猜测的数量越少 = 记分板上的越高)。

如果两个玩家的猜测数量相同,那么它会按照谁的时间更快来排序。我试过制作一个数组列表,但没有任何运气。每次游戏重置时,我都试图让索引增加 1,因此变量不会被新变量覆盖。

我也无法让数组列表接受长字符串。

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();
}
}
4

1 回答 1

0

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

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

于 2012-09-28T21:01:36.150 回答