Results 1 to 2 of 2
  1. #1
    earnest is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default how to do switch in while loop

    I want to do a switch in while loop where at the break of every switch statement the while loop stops and ask for an input like F, R, C, Q. The statement below works but the statement does not break. Please help

    public static void main(String[] args)
    throws IOException
    {




    // start both with 1 point
    int goodTotal = 50;
    int monTotal = 50;

    // input switch statement




    while (goodTotal > 0 && monTotal > 0) {

    System.out.print("Type a letter: ");
    System.out.println("\n");
    System.out.print("F: Go out and Fight ");
    System.out.println("\n");
    System.out.print("R: Rest ");
    System.out.println("\n");
    System.out.print("C: Check Stats ");
    System.out.println("\n");
    System.out.print("Q: Quit ");
    int input = System.in.read();

    System.out.println("You typed: " + (char)input);

    switch(input) {
    case 'f': System.out.println("Continue the game"); break;
    case 'r': System.out.println("Players should rest"); break;
    case 'c': System.out.println("Checking the status of the game");
    System.out.print("Goodman has " + goodTotal + " points and Monster has " + monTotal + " points");
    System.out.println("\n"); break;
    case 'q': System.out.println("Game over");
    System.exit(input); break;
    default: System.out.println("Invalid selection");break;
    }


    // Set value of minimum and maximum damage
    int minDmg = 2;
    int maxDmg = 15;

    // Get random number;
    int damage = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();
    int damage2 = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();

    // remove value of damage from started value to get total value remaining
    goodTotal = goodTotal - damage;
    monTotal = monTotal - damage2;


    // print message if still in the game
    if(goodTotal > 0){
    System.out.println("Goodman has " + goodTotal + " points left. Not bad, Man! ");
    }

    // if Goodman survives round 2 print a message of encouragement
    if (goodTotal > 0 && count > 1 && count <= 2){
    System.out.print("This is encouraging. Goodman has lasted past roundhh " +count+ ". ");

    // print new message if Goodman passes round 3
    } else if (goodTotal > 0 && count == 3){
    System.out.print("Goodman is as strong as Samson. He has lasted round " +count+ " and still looks strong.");
    System.out.print(" 10 hit points has been added to your total");
    }


    if(monTotal > 0){
    System.out.println("Wait, Monster has a total of " + monTotal + " points and is still in the game");
    }


    // exit if have less than 0 point, and print game over. Congratulate the winner
    if(goodTotal < 0){
    System.out.println("Goodman you are out of the game");
    System.out.println("The monster will take over the village. This is sad");
    System.out.println("Game Over!");
    } else if(monTotal < 0){
    System.out.println("Goodman has been victorious");
    System.out.println("The monster is dead. The people live!!!!");
    System.out.println("Game Over!");
    }
    System.out.println("This is the end of round " + count +" ");
    System.out.println("\n");
    count = count + 1;

    }

    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

Similar Threads

  1. Replies: 4
    Last Post: 02-13-2011, 12:03 PM
  2. Loop inside a switch
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-26-2010, 03:25 AM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  4. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  5. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •