vendredi 18 septembre 2015

Nesting If-Else statements in bulk with Java

Okay, so I have a Java code that is supposed to go down different decision paths, using if-else. That would allow for 8 paths to be taken, with 3 decisions in each. YYY/NNN/YYN/YNN/NYY/NNY/YNY/NYN. I have the basic code down, but my if-else statements aren't nesting correctly and it's telling me to remove some 'else's. I need some help with this. I know it's a problem with the brackets, I just don't know what the problem is.

/* Objective: practice creating and using if-else,block statements */
import java.io.*;
import java.util.*;
public class Decisions { 
public static void main(String args[])  {
int answer = 0;
Scanner kbReader = new Scanner(System.in);  


System.out.println("This is a test of your decision making. Think of it as a survival game. Only one path is correct.");
System.out.println("Type 0 for No, and 1 for Yes, unless prompted otherwise.");
System.out.println("You are driving down a forest path at night when you realize you're almost out of gas. You spy a house nearby that might have a place for you to stay, but you're a little apprehensive about the idea. Do you go to the house?");
answer = kbReader.nextInt();
if (answer == 1) 
    System.out.println("You stop at the house, your car sputtering to a halt. Great. There goes the last of the gas. There are two buildings at this house. A shed, and a house. Type 0 for the shed, and 1 for the house.");
    answer = kbReader.nextInt();
    if (answer == 1)
        System.out.println("You walk into the house. It has a musty atmosphere. Looks like it hasn't been used in a while. All of a sudden, a loud voice yells 'GET OUT!' Do you investigate the sound of the voice?");

        answer = kbReader.nextInt();
        if (answer == 1) 
            System.out.println("What are you on, and where can I get some? You investigate the source of the voice and die a haunted horrible death because investigating these things gets people killed. Try again."); 

        else  
            System.out.println("YOU ARE NOT DEALING WITH THIS. You sprint to your car, but quickly get overtaken by whatever yelled at you. RIP.");}
    else 
        System.out.println("No way. You're going into the shed. It might have gas in it, anyway. You walk to the shed. You hear something move inside the shed. Open the shed anyway?"); 

    answer = kbReader.nextInt();
    if (answer == 1) 
        System.out.println("Do you have a death wish? You open the shed. Never have you see so many deadly insects/arachnids. Before you can close that shed, you're overtaken by spiders, wasps, etc. What a way to go."); 

    else
        System.out.println("You book it to your car. There is no way you're dealing with that today. As you're running back to your car, you trip on a rock, and fall on some rocks. Ow. You keep running, but you stop after a second. Something does not feel right. You feel your face, and it feels cold. You run back to the rocks you tripped onto, and notice something on them. They were poisoned. Well. You didn't really want to die today, but I guess you just had to.");

else
    System.out.println("You aren't dealing with no ghosts today. Investigating these things gets people killed. You drive onward, but your car runs out of gas anyway. You get out of your car. Great. At least you got out of the woods. There seems to be a gas station nearby. Maybe you can go over there and get some gas. You also have your phone. You can call help. Type 0 for the gas station, and type 1 for your phone.");

    answer = kbReader.nextInt();
if (answer == 1) 
    System.out.println("You walk to the gas station and start to think that going there was a bad idea. It screamed 'shady' the moment you approached. Continue to the gas station?"); 

    answer = kbReader.nextInt();
    if (answer == 1)  
        System.out.println("You don't care HOW shady this place is. You need gas, and you're going to get gas! You walk up to the gas station. A thug is standing by the door, and notices you. Without provocation, he draws a gun and shoots. He misses. No, wait, he misses YOU. He hit the gas kiosk. Explosions occur. Whoops. Looks like you were ordered well done. Try again."); 

    else
        System.out.println("On second thought, maybe you should go back. This place looks pretty shady. You turn to walk back, when you get shot in the back. Just like that! Shady indeed, I guess. Try Again.");

    if (answer == 0) 
        System.out.println("Mhm. Let's go to the shady gas station. NO! You're going to use your phone! Now, where did you put it? I guess you have to look for it. Where do you look? Type 0 for the front of the car. Type 1 for the back of the car");

    if (answer == 1)
        System.out.println("You take a look in the back of your car... and hear footsteps. All of a sudden, a sharp pain enters your spine. You were just stabbed! Well, today really isn't your lucky day. Try again.");

    else 
        System.out.println("You look in the front of your car... success! There's your phone! You call for help. As you sit in your car, getting towed to a gas station that isn't shady, you relax a little, knowing that you are safe... for now. You win!");

}

Aucun commentaire:

Enregistrer un commentaire