有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用计时器的问题。计时器将不会更新

嘿,伙计们,thanx在我解释我的问题之前,你们需要知道代码是关于什么的。差不多编写一个游戏,其中用户要猜测1到1000之间的一个随机数。程序应从键盘上读取一个数字,并打印猜测是否过高、过低或正确。当用户猜对后,程序将打印猜测次数、时间和玩家姓名。当游戏启动时,程序必须按“升序”中的猜测次数打印整个高分列表

问题是在玩游戏时,当你设法得到正确的答案时,它不会显示时间,但当高分打印出来时,它会显示在那里,这是怎么回事,因为试图更改布尔语句,但没有看到效果。下面是问题的一个示例:

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

共 (3) 个答案

  1. # 1 楼答案

    对于初学者来说,遵循你(希望)学到的简单缩进规则也是一件好事

    此外,如果a是布尔值,则不应编写类似while(a==false);的内容,只需编写while(a)

    在这种情况下,您也不必使用do - while循环。一个while循环将更合适

  2. # 2 楼答案

    这里有几件事

    首先,这里没有使用Timer。您是根据实际时间来计时,但在Java中,Timer(例如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除以一次)

  3. # 3 楼答案

    首先你在这里划分时间

    long gameTime = endTime - startTime;
    gameTime = (gameTime/1000);
    

    然后又是这里

    System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");
    

    难怪你会得到0:)。所以,不要再分了,只要把gameTime

    然后当你添加到tg数组列表时,你会添加tg.add(gameTime);,这是正确的时间,这就是为什么当你列出高分时,它会起作用