lundi 27 juillet 2020

Java: Can No Longer Use System.out.println() In My Application

I made a little "guess the random number" game that takes the user's input and checks if it is equal to the number generated using a for loop. If yes, the application prints a congrats statement and then breaks out of the loop. If no (and failed 5 times), the application is supposed to print out a game over-type statement

For some reason, my game over statement System.out.println("Sorry " + answer+ ". the answer was " + rand + ". Game Over."); does not print, even though all the other println() statements do. Why isn't it working?? Any help would be much appreciated!

Below is my code:

import java.util.Random;
import java.util.Scanner;

public class randgame_draft {
    public static void main(String[] args) {
        System.out.println("Welcome to RandGame. What's your name?");
        Scanner scanner = new Scanner(System.in);
        String answer = scanner.next();
        System.out.println("Hello, " + answer + ". Shall we begin?");
        Scanner ask = new Scanner(System.in);
        String response = ask.next();
        if (response.equals("yes")) {
            System.out.println("Alright!");
        } else {
            System.out.println("Oh, okay. Later!");
            return;
        }

        Random random = new Random();
        int rand = random.nextInt(20) + 1;
        for (int i = 0; i < 5; i++) {

            System.out.println(answer + ", please provide a random number between 1 and 20.");
            Scanner begin = new Scanner(System.in);
            int end = begin.nextInt();
            System.out.println("you chose " + end);

            if (end != rand && end > rand) {
                System.out.println("Hint: guess lower");
            } else if (end != rand && end < rand) {
                System.out.println("Hint: guess higher");
            } else if (end == rand){
                System.out.println("congratulations, you are correct!");
                System.out.println("answer was " + rand);
                System.out.println("thanks for playing, " + answer + "!");
                return;
            } else {
                System.out.println("Sorry " + answer+ ". the answer was " + rand + ". Game Over.");
            }

        }

    }
}



Aucun commentaire:

Enregistrer un commentaire