Timeunit未在第二类中解析

时间:2015-12-21 11:36:41

标签: javascript java eclipse timeunit

当我尝试创建一个基于时间元素的游戏时,我遇到了一个问题。在其中,我已经给用户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);
}
}
}

1 个答案:

答案 0 :(得分:0)

因为LocalTime为零而你的循环需要LocalTime&gt; 0

int LocalTime = Time.Time;

指的是名为'Time'的静态变量

public static final int Time = 0;

不是名为'Time'的局部变量

int Time = 300;