Results 1 to 3 of 3
  1. #1
    Spindraft is offline Member
    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Question Trying To Get This Code To Repeat

    I am trying to get this piece of code to loop until one of the correct choices is made. I have gotten close several times but it will usually get stuck on the original string (race) if the correct choice is not chosen the first time. One version would not recall the String(race) because it said the value was no longer valid. This version will run the loop one time but displays all of the array_race strings in consecutive order, if you go through this version twice incorrectly it will just skip to running the array_race strings in consecutive order. Any help will be much appreciated.


    Java Code:
         String[] array_race;
            array_race = new String[5];
                array_race[0] = "You come from the high country";
                array_race[1] = "You live among the cacti";
                array_race[2] = "Your home is in the trees";
                array_race[3] = "You make your home in the cool recesses";
                array_race[4] = "Your life is tied to water";
            int find_race = -1;
            int found = 0;
            String race;
                race = JOptionPane.showInputDialog("Choose Your Race: Highland Climber, Desert Dweller, Forest Walker, Canyon Slotter, River Runner", "Enter race here");          
                   if (race.equals("Highland Climber")){
                      find_race = + 1;
                      found = - 1;
                      JOptionPane.showMessageDialog(null, array_race[0]);
                      //GOTO CHAR CREATION 
                   }
                       
                   if (race.equals("Desert Dweller")){
                      find_race = + 1;
                      found = - 1;
                      JOptionPane.showMessageDialog(null, array_race[1]);
                      //GOTO CHAR CREATION
                   }
                
                   if (race.equals("Forest Walker")){
                      find_race = + 1;
                      found = - 1;
                      JOptionPane.showMessageDialog(null, array_race[2]);
                      //GOTO CHAR CREATION
                   }
                   if (race.equals("Canyon Slotter")){
                      find_race = + 1;
                      found = - 1;
                      JOptionPane.showMessageDialog(null, array_race [3]);
                      //GOTO CHAR CREATION
                   }
                   if (race.equals("River Runner")){
                       find_race = + 1;
                       found = - 1;
                       JOptionPane.showMessageDialog(null, array_race[4]);
                       //GOTO CHAR CREATION
                   }
                   else if (found > find_race){
                      JOptionPane.showInputDialog(null,"Choose Your Race: Highland Climber, Desert Dweller, Forest Walker, Canyon Slotter, River Runner", "Enter race here" );
                      if (race.equals("Highland Climber"));
                         JOptionPane.showMessageDialog(null, array_race[0]);
                       if (race.equals("Desert Dweller"));
                          JOptionPane.showMessageDialog(null, array_race[1]);
                        if (race.equals("Forest Walker"));
                           JOptionPane.showMessageDialog(null, array_race[2]);
                         if (race.equals("Canyon Slotter"));
                            JOptionPane.showMessageDialog(null, array_race[3]);
                          if (race.equals("River Runner"));
                          JOptionPane.showMessageDialog(null, array_race[4]);{
                           find_race = + 1;
                           found = - 1;
                          }
                           if (found > find_race){
                               return; 
                           }
                   }
                   
                   //THE ABOVE RETURN WORKS ONE TIME BUT RUNS THROUGH ALL OF THE ARRAY_RACE
                   //IF USED TWICE IT AUTO GOES THROUGH ALL OF THE ARRAY_RACE

  2. #2
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: Trying To Get This Code To Repeat

    Why do a loop? Use a switch ( choice ) after line 11.

  3. #3
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: Trying To Get This Code To Repeat

    Let me take a step back. You said you are "trying to get this piece of code to loop until one of the correct choices is made". Not sure why you want to do the loop with what you have in the code, but:

    If you want a loop until correct, why not a do-while statement while choice != correct?

    If no loops, just refer to my prior statement (switch ( choice )).
    Last edited by pakupakuman; 11-12-2012 at 12:37 PM. Reason: typo

Similar Threads

  1. Random does not repeat itself?
    By swedishfished in forum New To Java
    Replies: 5
    Last Post: 10-20-2012, 05:56 PM
  2. Replies: 18
    Last Post: 04-03-2012, 09:49 AM
  3. nextInt() repeat
    By trishtren in forum New To Java
    Replies: 3
    Last Post: 10-09-2011, 06:20 PM
  4. how to repeat my method?
    By poupas in forum New To Java
    Replies: 6
    Last Post: 11-20-2010, 06:29 PM
  5. Help with a repeat
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 01-01-2010, 09:42 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
  •