Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help adding non repetition.

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Help adding non repetition.

    Ok, im looking for something to add in this code to avoid the cases 0-9 being repeated. Out of the 10 cases I want 5 to happen, and for them to be totally random and different each time. Anyone help me add the missing lines for no-repetition?
    import javax.swing.JOptionPane;
    public class KK
    {
         public static void main (String arg[])
        {    
           int menu;
           menu = Integer.parseInt (JOptionPane.showInputDialog("Seleccione una opcion :\n1. Jugar Trivia.\n2. Salir."));
           while(menu<3)
            {
              switch (menu)
                {
                case 1:
                  int repetidor, correctas, preguntas, respuesta;
                  repetidor= 0;
                  correctas= 0;
                  while(repetidor<5)
                  {
                    repetidor = repetidor+1;
                    preguntas= ( int ) ( Math.random() * 10 );
                    switch (preguntas)
                      {
                      case 0:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog("blablablalba\n1. La respuesta es 1\n.2. La respuesta es 2\n3. No existe respuesta \n4. Es un numero irreal..."));
                      switch (respuesta)
                      {
                      case 1:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      } break;
                      case 1:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog(" blablabla = \n1. La respuesta es 4\n.2. La respuesta es 5\n3. No existe respuesta \n4. La respuesta es 33"));
                      switch (respuesta)
                      {
                      case 2:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      }break;
                      case 2:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog(" blablablabala \n1. La respuesta es 27\n.2. La respuesta es 17\n3. La respuesta es 47 \n4. La respuesta es 57"));
                      switch (respuesta)
                      {
                      case 4:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      }break;
                      case 3:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog(" blablablablabal\n1. La respuesta es 17\n.2. La respuesta es 82\n3. No existe respuesta \n4. La respuesta es 12."));
                      switch (respuesta)
                      {case 4:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      }break;
                      case 4:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog("blablablabala:\n1. Eds Adventures\n.2. Full Metal Alchemist\n3. ED & EDDE & EDDY\n4. Pokemon"));
                      switch (respuesta)
                      {case 2:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      }break;
                      case 5:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog("blablablabla:\n1. Perro\n.2. Gato\n3. Canario\n4. Pokemon"));
                      switch (respuesta)
                      {case 2:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      }break;
                      case 6:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog("blablabla:\n1. Filosofia\n.2. Arte\n3. Quimica\n4. Danza"));
                      switch (respuesta)
                      {case 3:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      }break;
                      case 7:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog("blablabla:\n1. Soyla\n.2. Alex\n3. Mario\n4. Ese evento no existe...-.-"));
                      switch (respuesta)
                      {case 4:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      }break;
                      case 8:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog("blablabla:\n1. 30%\n.2. 44%\n3. 20%\n4. 25%...-.-"));
                      switch (respuesta)
                      {case 4:
                        correctas= correctas +1;
                        break;
                      default:
                        correctas= correctas;
                        break;
                      }break;
                      case 9:
                      respuesta =Integer.parseInt (JOptionPane.showInputDialog("25x3 =\n1. 70\n.2. 50\n3. 75\n4. 25"));
                      switch (respuesta)
                      {case 3:
                        correctas= correctas +1;
                        break;
                      default:
     
                        break;
     
                      }break;
                    } 
                  }
                  break;
                case 2:
                  System.exit(0);
                  break;
            }
         }
    }
    }
    Last edited by copeg; June 15th, 2011 at 02:27 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Help adding non repetition.

    It would be much easier if you tell us what have to do your code and change that language (spanish?) to english

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help adding non repetition.

    Out of the 10 cases I want 5 to happen, and for them to be totally random and different each time.
    Put the numbers 1 to 10 in a list, shuffle the list and remove and use the first 5.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help adding non repetition.

    while
        switch
            while
                switch
                    switch
    When you have this many nested levels you need to:
    Review your design. Are you sure that the code is doing what is needed.
    Refactor the code. Break it down into smaller parts and move these into their own methods.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help adding non repetition.

    correctas= correctas;
    Just spotted this is several places. Completely pointless.

Similar Threads

  1. [SOLVED] stuck on an assignment and need help with repetition
    By teeej86 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 20th, 2011, 07:03 PM
  2. [SOLVED] Using a For loop for repetition
    By cb5950 in forum Loops & Control Statements
    Replies: 7
    Last Post: March 8th, 2011, 05:53 PM
  3. checking for draw by repetition in chess app
    By aisthesis in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 16th, 2011, 02:40 AM
  4. java Logic Random with out repetition
    By Jhovarie in forum Loops & Control Statements
    Replies: 1
    Last Post: January 13th, 2011, 03:25 PM
  5. [SOLVED] changing character repetition
    By vendetta in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 16th, 2010, 06:16 PM

Tags for this Thread