gpt4 book ai didi

java - 简单猜谜游戏中的记分牌

转载 作者:行者123 更新时间:2023-12-01 05:06:05 25 4
gpt4 key购买 nike

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

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

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

如果两个玩家的猜测数量相同,则会按谁的时间更快进行排序。我尝试过创建一个数组列表,但没有任何运气。我试图让索引在每次游戏重置时增加 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();
}
}

最佳答案

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

此外,您不需要索引。只需 score.add(count) 就会在所有已有分数之后添加一个新分数。

关于java - 简单猜谜游戏中的记分牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12640398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com