gpt4 book ai didi

java - 猜测次数的问题不会针对新的尝试进行更新,并且在输入数字 1005,1006 时代码会导致错误?

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

嘿伙计们,感谢之前的帮助在我尝试解释我的问题之前,你们需要知道代码的含义。它几乎是“编写一个游戏,让用户猜测 1 到 1000 之间的随机数字。程序应该从键盘读取一个数字,并打印猜测是否过高、过低或正确。当用户猜对了,程序会打印出猜测的次数、时间和玩家姓名。当游戏开始时,程序必须打印整个高分列表,按猜测的次数升序排列”

一切正常,但问题是当我尝试输入 1005 和 1006 的代码时,它会导致错误。并且对于新的尝试,猜测的数量不会更新!对于猜测的数量,我尝试添加一个 boolean 语句,但这似乎不起作用,对于数字,我找不到它导致错误的原因。所以基本上我很困惑,我希望你们能给我一些指示或某种帮助,这样我就可以解决问题,,,,,任何帮助表示赞赏......顺便说一句,这是我的第一个程序,基本上仍处于学习阶段,所以请轻松评论。代码如下:

import java.util.*;
import java.util.Scanner.*;
import java.util.ArrayList.*;
import java.util.Collections.*;


public class Game {
private static void start() {


int tries = 0 ;

int guess = -1;
String name ;
String quit = "quit";
String y = "yes";
String n = "no";
String currentGuess;


String another = ("y") ;
Scanner input = new Scanner (System.in);

ArrayList<Integer> score = new ArrayList<Integer>();
ArrayList<Long> tg = new ArrayList<Long>();
ArrayList<String> playern = new ArrayList<String>();



boolean a=false;

do {
a=false;
int answer = (int) (Math.random() * 1000 + 1) ;
System.out.println( " Welcome to Guessing Game " ) ;
System.out.print("Please enter a number between 1 and 1000 : ");
currentGuess = input.nextLine();
long startTime = System.currentTimeMillis();





do
{


if (currentGuess.equalsIgnoreCase(quit))
{
System.out.println("Leaving Us So Soon?");
System.exit(0);
}

try {
guess = Integer.parseInt(currentGuess);
} catch (NumberFormatException nfe)
{
System.out.println(" Dude Can You Read, Only Digits ");
currentGuess = input.nextLine();


}

if (guess < 1 || guess > 1000)
{
System.out.println("Stupid Guess I Wont Count That.");
currentGuess = input.nextLine();

}
if (guess < answer )
{
System.out.println("too low "+answer);
currentGuess = input.nextLine();
tries++;
}


else if(guess > answer )
{
System.out.println("too high "+answer);
currentGuess = input.nextLine();
tries++;
}


else if (guess == answer)
{
//stop stop watch
long endTime = System.currentTimeMillis();
//calculate game time
long gameTime = endTime - startTime;
gameTime = (gameTime/1000);
System.out.println("You Rock Dude, Good Job!");


System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");
System.out.println("Please enter your name.");
name = input.nextLine();

score.add(tries) ;
playern.add(name);
tg.add(gameTime);

for ( int g=0; g < score.size()-1; g++){
for ( int b=g+1; b < score.size(); b++){
if (score.size()>1){
if (score.get (g) > score.get (b)){
Collections.swap(score, g, b);
Collections.swap(playern, g, b);
Collections.swap(tg, g, b);

}
}
if (score.get (g)==score.get(b) && tg.get (g) > tg.get(b))
{
Collections.swap(score, g, b);
Collections.swap(playern, g, b);
Collections.swap(tg, g, b);
}
}

}



boolean s = false ;
while (s==false)
{


System.out.print("Want to go again?(y/n).....");
currentGuess = input.nextLine();
if (currentGuess.matches("y"))
{

System.out.println("HighScore:");
System.out.println("Tries\tTimentName");
for (int j=0; j<score.size(); j++){
System.out.println(score.get(j) +"\t"+tg.get(j)+ "\t"+playern.get(j));
}


}

s=true;

}

//if user doesn't want to play again

if (currentGuess.matches("n"))
{ System.out.println("HighScore:");
System.out.println("Tries\tTime\t\tName");
for (int j=0; j<score.size(); j++){
System.out.println(score.get(j) +"\t"+tg.get(j)+ "\t"+playern.get(j));
}


System.out.println("Thanx For Playing.");
a=true;

s=true;
System.exit(0);
}
}



} while (guess != answer);

}while(a==false);

}

public static void main(String[] args) {

Game.start();
}
}

最佳答案

您需要在拒绝输入的两个地方继续

关于java - 猜测次数的问题不会针对新的尝试进行更新,并且在输入数字 1005,1006 时代码会导致错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3970248/

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