当我尝试创建具有基于时间的元素的游戏时,我遇到了一个问题。在其中,我给了用户 300 秒的时间来猜测一个数字。
如果播放器超时,那么代码将停止我创建的 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 JordonMyers123@Gmail.Com 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);
}
}
}