嘿伙计们,感谢先前的帮助在我尝试解释我的问题之前,你们需要知道代码是什么.它几乎是"编写一个用户猜测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
Run Code Online (Sandbox Code Playgroud)
所以基本上我很困难,我希望你们能给我一些指示或某种帮助,以便我能解决问题,,,,,任何帮助都表示赞赏....顺便说一下这是我的第一个程序,基本上还处于学习阶段所以请轻松评论.代码如下:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
首先,你把时间分开
long gameTime = endTime - startTime;
gameTime = (gameTime/1000);
Run Code Online (Sandbox Code Playgroud)
再来这里
System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");
Run Code Online (Sandbox Code Playgroud)
难怪你为什么得到0 :).所以,不要再分,只是把gameTime
然后当你添加到tg数组列表时添加tg.add(gameTime);哪个是正确的时间,这就是为什么当你列出Highscores时它的工作原理.
| 归档时间: |
|
| 查看次数: |
384 次 |
| 最近记录: |