-3

Question: How can I fix my program to where it could continue after the third option?

I'm making a short adventure game,and there seems to be some problems. I'm brand new to this so any advice is great.

The main problem is when the player clicks "yes" ,or "no" at

<i>Go into the abandon house?</i>
<i>1.I don't see why not.</i>
<i>2.I rather stay outside,</i>(user_name)<i> snickers.</i>

After the player makes their choice the program ends. The program is supposed to continue into a fight after the player chooses,but nope!

Here's my code:

import java.util.Scanner;

public class NewAdventureProject {

public static void main(String[] args)
throws java.io.IOException{
      int MonsterHealth;
      int PlayerHealth ;
      int GhostHealth;
      char input='a';//The attack key for battles

        PlayerHealth=100;//Health of the player 
        MonsterHealth=100;//Health of the Monster
        GhostHealth=120;//Health of the ghost



   Scanner user_input=new Scanner(System.in);
   String user_name;
  System.out.println("Enter Your name or prefered nick name:");//player name option
  user_name=user_input.next();

  System.out.println(user_name+" you stepped into the light.");
  System.out.println("You see a house");
  System.out.println("It looks abandon. Do you want to investigate it?");
  System.out.println("1.Yes.");
  System.out.println("2.No.");
  Scanner scan = new Scanner(System.in);//Declares new scanner
  int a = scan.nextInt();//Helps me navigate around long if and while statements

    switch(a){
        case 1://outcome one
            System.out.println("As you get closer to the abandon house, you "
                    + "hear a random roar.");
            break;
        case 2://outcome two
            System.out.println("The clouds black out the sun.\nYour "
                    + "face is sprinkled with water"+". It's raining now.\nY"
                    + "ou shiver.");
            System.out.println("You hear a faint scream from the abandon "
                    + "house. Do you want to investigate?");
            break;
           }


  System.out.println("Go into the abandon house?");

     System.out.println("1.I don't see why not.");
     System.out.println("2.I rather stay outside,"+user_name+" snickers.");

     Scanner sc = new Scanner(System.in);//New scanner
     int b = sc.nextInt();//New int scanner 
     switch(b){

         case 1://if play goes into the abandon house 


       for(MonsterHealth=100; MonsterHealth<0;){//monster's health loop
          if(PlayerHealth>0){

      System.out.println("The house is dark and quiet.");
      System.out.println("A roar gets closer to you.");
      System.out.println("You get knocked back!\nYou're hurt!");
      PlayerHealth=PlayerHealth-6;
      System.out.println(user_name+"'s health is now"+PlayerHealth);
      System.out.println("You were attacked by a Green Monster!"+user_name
      +" take this!\n"+user_name+" obtained a shiny sword!");
      System.out.println("Press a to attack the Green Monster!");
        if(input=='a'){
            System.out.println("The Green Monster flinches.\nYou did 10 dam"
                    + "age to the monster!");
            MonsterHealth=MonsterHealth-15;
            System.out.println("The Green Monster lunges at you!\nYou lost"
                    + "5 health points!");
            System.out.println("your health is now"+PlayerHealth+"!");
        }else{
            PlayerHealth=PlayerHealth-5;
            System.out.println("The Green Monster lunges at you!\nYou lost"
                    + "5 health points!");
            input=(char)System.in.read();
        }
  }else{

  }
      if(MonsterHealth==0){
          System.out.println("The Green Monster makes an ear piercing growl."
                  + "The Green monster falls!\nYou win!");

      }else if(PlayerHealth==0){//if the player looses all of their health
          System.out.println("You see the light again"+user_name+"you have "
                  + "fallen.\nYou're dead.\nGameover");
            break;
      }
      }
     case 2://second option for switch
         for(GhostHealth=120; GhostHealth<0;){
          System.out.println("You get chills.\n The sky gets ominously dark.");
          System.out.println("You hear a screeching howl.\n You're pushed onto"
                  + " the ground!\nYou're hurt");
          PlayerHealth=PlayerHealth-5;

          System.out.println(user_name+"'s health is now"+PlayerHealth);
      System.out.println("You were attacked by a Ghost!");
      System.out.println("Press a ,followed by enter ,to attack the Ghost!");
        if(input=='a'){
            System.out.println("The Ghost howls!.\nYou did 15 damage to the"
                    + " Ghost!");
           GhostHealth=GhostHealth-15;
            System.out.println("The Ghost scratches you!\nYou lost"
                    + "10 health points!");
            PlayerHealth=PlayerHealth-10;
            System.out.println("your health is now"+PlayerHealth+"!");
        }else{
            PlayerHealth=PlayerHealth-5;
            System.out.println("The Ghost scratches you!\nYou lost"
                    + "10 health points!");
            input=(char)System.in.read();
        }

  }
      if(GhostHealth==0){
          System.out.println("The Ghost makes an ear piercing squeal."
                  + "The Ghost dissolves!The skies are now clear"
                  + "\nYou win!");

      }else if(PlayerHealth==0){
          System.out.println("You see the light again"+user_name+"you have "
                  + "fallen.\nYou're dead.\nGameover");


      }    
      }
      }
     }
Stacker-flow
  • 1,251
  • 3
  • 19
  • 39
HecTUr
  • 1
  • 1
  • Where do you tell your program to "keep going"? hint: [loops](http://www.tutorialspoint.com/java/java_loop_control.htm) – drew moore Nov 03 '14 at 03:14
  • You'll be better off more explicitly stating your question. – Alex K Nov 03 '14 at 03:19
  • Basically , how can I fix the program so that it would continue onto the next option/action? – HecTUr Nov 03 '14 at 03:23
  • Too much code. And possibly a duplicate of http://stackoverflow.com/questions/2710300/why-do-we-need-break-after-case-statements – Raedwald Jan 24 '16 at 16:54

1 Answers1

0

It was hard to see through this spaghetticode but there are breaks missing for the cases in the second switch.

ascorbin
  • 111
  • 4