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 3 of 3

Thread: Java Loops help please?

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Loops help please?

    How can i modify this program so it has loops within loops?

     
    import javax.swing.*;
     
    class team
    {
    public static void main (String[] param)
    { 
    insultme(); 
    System.exit(0);
     
    } // END main
     
    /* *********************************
     
    A method that asks to insult you 
    */
    public static void insultme() 
    {
     
    // Declare variables
     
    String textinput; // whatever the person types 
    int number1; // goals scored
    int number2; // goals scored
    String Team1 = "";
    String Team2 = "";
     
    // Get some text (a string) from the user then
    // convert (known as parsing) it into a number version
     
    while (!Team1.equals("quit")) 
    {
    Team1 = TeamName();
     
    if (Team1.equals("quit")) 
    {
    break;
    }
     
    textinput = JOptionPane.showInputDialog( "Score of Team?");
    number1 = Integer.parseInt(textinput);
     
    Team2 = TeamName();
     
    textinput = JOptionPane.showInputDialog( "Score of Team?");
    number2 = Integer.parseInt(textinput);
     
    //test score here:
    testScore(Team1, Team2, number1, number2);
     
    } //end while
     
    return;
     
    }//end method
     
    gg
    /***********************************
    * Method to check score and either ask for re-entry if invalid or print result.
    */
     
    public static void testScore(String Team1, String Team2, int number1, int number2)
    {
    if(number1 < number2) 
    {
    JOptionPane.showMessageDialog( null, Team2 + " won " + number2 + "-" + number1 + " against " + Team1);
    } 
    else if (number2 < number1)
    {
    JOptionPane.showMessageDialog( null, Team1 + " won " + number1 + "-" + number2 + " against " + Team2);
    } 
    else 
    {
    JOptionPane.showMessageDialog( null, "This score is wrong, please enter a correct score");
     
     
    String textinput = JOptionPane.showInputDialog( "Score of Team 1 again");
    number1 = Integer.parseInt(textinput);
    textinput = JOptionPane.showInputDialog( "Score of Team 2 again");
    number2 = Integer.parseInt(textinput);
     
    testScore(Team1, Team2, number1, number2); // call method if re-entry
     
    }
     
    } //end method
     
     
    /* *********************************
     
    */
    public static String TeamName()
     
    {
    return JOptionPane.showInputDialog("Team Name?"); 
    }
     
     
     
     
    } // END class team


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Java Loops help please?

    Hello Sarah-Perkin.

    Welcome to the Java Programming Forums

    What are you trying to achieve exactly?

    The only loop I can see in your code is a 'while' loop so if you want to add a loop within a loop, you can add another loop there.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Loops help please?

    to pass my task my program has to contain loops within loops somewhere so can you help me do this. thanks

Similar Threads

  1. Program with for loops help
    By ixjaybeexi in forum Loops & Control Statements
    Replies: 23
    Last Post: October 8th, 2009, 10:05 AM
  2. :!! Unsure how to set this up/ Still learning java loops!!!!!
    By raidcomputer in forum Loops & Control Statements
    Replies: 4
    Last Post: September 29th, 2009, 10:28 AM
  3. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM
  4. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 03:10 AM
  5. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM