0

import java.util.Scanner;

public class ChooseYourOwnAdventure { static Scanner in = new Scanner(System.in);

public static void main (String[] args){

    System.out.println("[This is a choose your own adventure.] It's 1941; you are joining the military. What branch do you choose? Army [1], Marine Corps [2], Navy [3] or Air Force [4]?");

    String branch = "undecided";

    String input = "";
    System.out.println("Please input a command:");
    input = in.nextLine();

    if (input.equals("1"))
    {   
    System.out.println("You have joined the Army. Defend America well. Press 0 to continue.");
    branch = "Army";
    }
    else if (input.equalsIgnoreCase("2"))
    {
    System.out.println("You have Joined the Marine Corps. Semper Fi. Press 0 to continue");
    branch = "Marine Corps";
    }
    else if (input.equals("3"))
    {
    System.out.println("You have joined the Navy. Patrol the sea with pride. Press 0 to continue.");
    branch = "Navy";
    }
    else if (input.equals("4"))
    {
    System.out.println("You have joined the Air Force. Take to the skies. Press 0 to continue.");
    branch = "Air Force";
    }

    String input2 = "";
    input = in.nextLine();

    if (input2.equals(0))
    {
    System.out.println("You complete basic training for the " + branch + " with flying colors. Now, you can choose your specialty.");
    }

    if (branch == "Army")
    {
    System.out.println("You can choose to be an Infantryman [q] or a Marksman [w].");
    }

    else if (branch == "Marine Corps")
    {
    System.out.println("You can choose to be a Paratrooper [e] or a Sniper [r].");
    }

    else if (branch == "Air Force")
    {
    System.out.println("You can choose to fly a Fighter [t] or a Bomber [y].");
    }

    else if (branch == "Navy")
    {
    System.out.println("You can choose to be a Sailor [u] or a Pilot [i].");
    } 

    in.nextLine();


    String input77 = "l";
    input = in.nextLine();

    if (branch == "Army")
    {
    if (input77.equals("q"))
    {
    System.out.println("infantryman");
    }
    else if (input77.equals("w"))
    {
    System.out.println("DM");
    }
    }
    if (branch == "Marine Corps")
    {
    if (input77.equals("e"))
    {
    System.out.println("paratrooper");
    }
    else if (input77.equals("r"))
    {
    System.out.println("sniper");
    }
    }
    if (branch == "Air Force")
    {
    if (input77.equals("t"))
    {
    System.out.println("fighter");
    }
    else if (input77.equals("y"))
    {
    System.out.println("bomber");
    }
    }
    if (branch == "Navy")
    {
    if (input77.equals("u"))
    {
    System.out.println("sailor");
    }
    else if (input77.equals("i"))
    {
    System.out.println("pilot");
    }
    }

}

}

OK, this is my program. It malfunctions when you choose your "specialty", (called input 77) and when i type in my input, nothing happens. What am i missing?

  • 2
    Use String.equals to compare strings instead of == – Juned Ahsan Feb 02 '15 at 21:53
  • 3
    Why in one place you are using `if (input.equals("4"))` but in other `if (branch == "Army")`? You should read http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Pshemo Feb 02 '15 at 21:53

0 Answers0