在Java中使用计时器的问题。计时器将不会更新
嘿,伙计们,thanx在我解释我的问题之前,你们需要知道代码是关于什么的。写一个游戏,让用户猜一个介于1到1000之间的随机数。程序应该从键盘上读取一个数字,然后打印猜测是否过高、过低或正确。当用户猜对后,程序会打印出猜对的次数、时间和玩家姓名。当游戏开始时,程序必须打印整个高分列表,并按猜对的次数升序排列 问题是在玩游戏时,当你设法得到正确的答案时,它不会显示时间,但当高分打印出来时,它会显示在那里,这是怎么回事,因为试图更改布尔语句,但没有看到效果。下面是问题的一个示例:在Java中使用计时器的问题。计时器将不会更新,java,Java,嘿,伙计们,thanx在我解释我的问题之前,你们需要知道代码是关于什么的。写一个游戏,让用户猜一个介于1到1000之间的随机数。程序应该从键盘上读取一个数字,然后打印猜测是否过高、过低或正确。当用户猜对后,程序会打印出猜对的次数、时间和玩家姓名。当游戏开始时,程序必须打印整个高分列表,并按猜对的次数升序排列 问题是在玩游戏时,当你设法得到正确的答案时,它不会显示时间,但当高分打印出来时,它会显示在那里,这是怎么回事,因为试图更改布尔语句,但没有看到效果。下面是问题的一个示例: You guess
You guessed 2 times in 0 seconds.
Please enter your name.
gert
Want to go again?(y/n).....n
HighScore:
Tries Time Name
1 35 b
2 6 gert
所以基本上我被卡住了,我希望你们能给我一些建议或帮助,这样我就可以解决这个问题,,,,,任何帮助都是非常感谢的。。。。顺便说一句,这是我的第一个程序,基本上仍处于学习阶段,所以请不要太在意这些评论。代码如下所示:
import java.util.*;
import java.util.Scanner.*;
import java.util.ArrayList.*;
import java.util.Collections.*;
public class Main {
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();
continue;
}
if (guess < 1 || guess > 1000)
{
System.out.println("Stupid Guess I Wont Count That.");
currentGuess = input.nextLine();
continue;
}
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) {
Main.start();
}
}
首先你在这里划分时间
long gameTime = endTime - startTime;
gameTime = (gameTime/1000);
然后又是这里
System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");
难怪您会得到0:。所以,不要再分了,把游戏时间
然后,当您添加到tg数组列表时,您将添加tg.addgameTime;这是正确的时间,这就是为什么当你列出高分时它会起作用。这里有几件事 首先,这里没有使用计时器。您正在根据实际时间对某些事情进行计时,但在Java中,计时器(如Java.util.Timer)是一个调度重复任务的对象 其次,您应该尝试将代码分解成更小的方法。这些长的嵌套do/while循环很难阅读,因为不滚动就看不到整个循环。尽量缩短方法,并使用多种方法 但是,为了回答您关于时间的问题,当您尝试显示时间时,您将时间除以1000两次。您的显示行是
System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");
但是你已经在上面分配了游戏时间
long gameTime = endTime - startTime;
gameTime = (gameTime/1000);
因此,在显示屏中,您实际上并没有再次修改gameTime,这就是它存储为正确值的原因,但您显示的是gameTime/1000,即使它已经被1000除以一次。对于初学者来说,遵循您希望学习的简单缩进规则也是一件好事 你也不应该写像whilea==false这样的东西;如果a是布尔值,只需写whilea
在这种情况下,您也不必使用do-while循环。while循环更合适。是否有一个恼人标题的结束选项?你能haz debugz my codez吗?投票否决丑陋的标题。为初学者竖起大拇指。根据经验,调试您自己的代码。你可以免费得到这个调试。Dudez希望我的编译器对programmingProject2.0说“是”。谢谢你。