0
我試圖創建一個具有基於時間的元素的遊戲時遇到問題。其中,我已經給了用戶300秒來猜測一個數字。Timeunit未在第二類中解析
如果玩家用完了時間,那麼代碼將停止我創建的while循環,並打印出用戶耗盡的時間。
但是 - 代碼甚至不解析While循環。它會直接跳到用戶耗盡時間,而我找不到任何修復程序。
這是我的主要類別:
import java.util.Scanner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class start {
public static void main(String[] args) throws InterruptedException {
Random random = new Random();
Scanner reader = new Scanner(System.in);
byte Lives = 10;
System.out.println("Hey there, and welcome to my Guessing game!");
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("I'm thinking of a number between 1 and 100");
int Guessed_Number = random.nextInt(100 - 1);
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("You have 10 lives!");
System.out.println("");
System.out.println("Ready? When you start, you will have 5 minutes to complete the game");
int LocalTime = Time.Time;
System.out.println(LocalTime);
boolean Win = true;
while(Lives>0 & Win & LocalTime > 0)
{
try {
System.out.println("I'm thinking of a number between 1 and 100");
System.out.println("Guess the number I'm thinking of!");
System.out.println("You have " + Lives + " Lives remaining");
byte Guess = reader.nextByte();
if(Guess > Guessed_Number)
{
System.out.println("Your number is too big!");
Lives--;
}
else if(Guess < Guessed_Number)
{
System.out.println("Your number is too small!");
Lives--;
}
else if(Guess == Guessed_Number)
{
System.out.println("You have guessed my number correctly!");
System.out.println("My number was " + Guessed_Number);
System.out.println("Welldone! You had " + Lives + " Lives remaining!");
Win = false;
}
else
{
System.out.println("Odd. Something weird happened. Try again!");
}
}
catch (Exception error)
{
System.out.println("Error!");
System.out.println("Please try to only put in numbers");
TimeUnit.MILLISECONDS.sleep(1000);
reader.next();
}
}
if(LocalTime == 0)
{
System.out.println("You ran out of time! Try again!");
System.out.println("The number I was thinking of was " + Guessed_Number);
}
else if(Lives ==0)
{
System.out.println("You ran out of lives! Try again!");
System.out.println("The number I was thinking of was " + Guessed_Number);
}
else
{
System.out.println("Foobar! The code ran into a error while working");
System.out.println("Please contact me at [email protected] with the Error code FOOBAR and I'll try and fix it.");
}
}
}
這裏是我的第二類,我專門只運行While循環的時間因素。
import java.util.concurrent.TimeUnit;
public class Time extends InterruptedException {
private static final long serialVersionUID = 1L;
public static final int Time = 0;
{
int Time = 300;
while(Time > 0);
{
Time = Time - 1;
TimeUnit.SECONDS.sleep(1);
}
}
}
我改變了這一點,但現在我發現錯誤似乎還沒有消逝。它只是暫停遊戲並等待用戶輸入。 –