OK 反射ゲームを作成する割り当てを求められました。これにより、ユーザーはタイマーを開始および停止し、最後に時間を表示できます。現在、それは主に機能していますが、現在、意図したとおりに1を押して開始し、タイマーを開始するという問題がありますが、意図しない秒数が表示される前にこのメッセージが再度表示されます. したがって、誰かがこれを引き起こしている可能性のある何かを助けたり、指摘したりできれば幸いです
public class TimerTest {
static int attempts = 4;
static Timer timer = new Timer();
static int controler;
static int seconds = 1;
static Scanner keyboard = new Scanner(System.in);
static int time1, time2, time3;
public static void run() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (controler == 1) {
System.out.println(seconds++ + " seconds");
}
} // closed the running of timer
}, 0, 1000);
// closed timer
}
public static void main(String args[]) {
System.out.println("\t\t\tthe rules\n");
System.out.println("1) press 1 then enter to start the game");
System.out.println("2) press 2 then enter as soon as you can to stop the timer");
System.out.println("3)only enter the specified numbers none other");
System.out.println("4) numbers only no letters\n\n");
do {
System.out.println("\t\t\tplease push 1 to start");
controler = keyboard.nextInt();
if (controler == 1 && attempts != 0) {
run();
attempts--;
} else if (controler == 2 && attempts == 3) {
System.out.println("stopped ");
controler = controler + 1;
System.out.println("your time is " + seconds);
time1 = seconds;
seconds = 0;
} else if (controler == 2 && attempts == 2) {
System.out.println("stopped ");
controler = controler + 1;
System.out.println("go 2 " + seconds);
time2 = seconds;
seconds = 0;
} else if (controler == 2 && attempts == 1) {
System.out.println("stopped ");
controler = controler + 1;
time3 = seconds;
System.out.println("final go " + seconds);
seconds = 0;
attempts = 0;
}
System.out.println("\nyou have " + attempts + " attempts remaining\n");
} while (attempts != 0);
if (time1 > time2 && time1 > time3) {
System.out.println("your first go was slowest with a time of " + time1 + " seconds");
} else if (time2 > time1 && time2 > time3) {
System.out.println("your second go was slowest with a time of " + time2 + " seconds");
} else if (time3 > time1 && time3 > time2) {
System.out.println("your final go was slowest with a time of " + time3 + " seconds");
} else {
System.out.println("STOP FUCKING UP MY PROGRAM JAVA");
}
if (attempts == 0) {
System.out.println("\ngame over, thanks for playing and please try again");
System.exit(0);
}
}
}