jeudi 8 octobre 2015

Netbeans (Java): Nested If Statements

I'm making a video game for a project and it's a text-based RPG where there are a lot of decisions that can be made (branching story). To start off my game, the user inputs "1" and within the if statement, there is another if statement. But whenever I run the code, I'd input "1", the println text would show and the build would just end instead of continuing on. Can I not have nested if-statements like this?

package personalProject;

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

public class Main{

    public static void main(String[] args) {

//System objects

Scanner in = new Scanner(System.in);
Random rand = new Random();

//Game variables

//

    System.out.println("--------------------------");
    System.out.println("# What Might've Happened #");
    System.out.println("--------------------------\n");
    System.out.println("You wake up. It's almost 7. Better get moving,\ncan't be late on the first day of school!");
    System.out.println("1. Continue");
    System.out.println("\nPress 1 and then press enter");

    String input = in.nextLine();

    if(input.equals("1")) {
            System.out.println("\nYou get to school at 7:15.\nYou can either:");
            System.out.println("1. Go to your first class.");
            System.out.println("2. Go see if you can make some friends.");
    switch (input) {
        case "1":
            System.out.println("");
            break;
        case "2":
            System.out.println("\n Do you want to go to the media center or the locker bay?");
            break;
        default:
            System.out.println ("Invalid Command!");
            break;
}
    }
else {
    System.out.println ("Invalid Command!");
}

}

}

Aucun commentaire:

Enregistrer un commentaire