0

I've ran into a issue when I'm trying to create a game that has a time based element to it. In it, I've gave the user 300 Seconds to guess a number.

If the player runs out of time then the code would stop a while loop I've created, and print that the user has ran out of time.

However - the Code doesn't even parse the While Loop. It jumps straight to when the user would run out of time, and I can't find any fixes for it.

Here's my main Class:

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.");
    }
}

}

Here's my second class, which I've devoted to only running the While Loop for the time Element.

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);
}
}
}
Jordon Myers
  • 147
  • 1
  • 1
  • 9

1 Answers1

0

Because LocalTime is zero and your loop requires LocalTime > 0

int LocalTime = Time.Time;

refers to the static variable named 'Time'

public static final int Time = 0;

not to the local variable also named 'Time'

int Time = 300;
shnplr
  • 230
  • 3
  • 12
  • I've changed that, but now I'm getting the error where it seems like it still isn't passing time. It just, pauses the game and waits for the users input. – Jordon Myers Dec 21 '15 at 12:55