Need Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Need Help

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Posts
    14

    Need Help

    Hello to all:
    I need set up the java program for the "while statements" and the "for statements". I know how to do the "do while statements" as followsNote this "do while" is just a example of what I need to do with the "while" and "for" statements).

    import javax.swing.*; //shows path to newer swing classes
    import java.awt.*; //shows path to older AWT classes
    public class extends JFrame
    {
    public () // method name with same name as class.

    {
    int correctNumber = (int)(Math.random()*100+1);//comp picks number 1-100.
    int userAnswer =0; //userAnswer initialized to 0
    int tally = 1;// this is for the tally count.

    System.out.println("The random number generated was " + correctNumber);
    do
    {
    String guess = JOptionPane.showInputDialog
    ("Pick a number between 1 and 100 - your guess? ");
    userAnswer = Integer.parseInt(guess);
    if (userAnswer < 1 || userAnswer > 100)//sets the limit to the numbers between 1 and 100.

    }while (userAnswer != correctNumber);
    {// the curly brackets are needed because more than one statement for "if" are used.




    But I also need to set this same program with the "while" only statements and then I need to set another program for the "for" only statements. In other words what would I change in the "do while" statements so I can use the "while statements" and what would I change so I can use the "for statements" Any help. Thankyou.
    Last edited by Recency; 04-08-2006 at 09:59 AM.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The while loop is a lot like the do while loop - just not a guaranteed first execution because the state of the loop condition might be false. You have a test of a boolean statement at the start, if it is true then perform the actions in the loop, and return to the test.

    The for loop uses a "counter", a test of the counter, and self incrementing of the counter. As long as the test is true, then perform the actions in the loop. As you finish the loop the program increments the counter and tests again.

    Certain actions or program structures are better for one or the other, but you can use both. Sometimes it makes sense only to use one or the other.

    [by the way, you're going to need to name your class if you expect this to compile]

  3. #3
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    while
    Code:
    import javax.swing.*; 
    import java.awt.*; 
    public class extends JFrame
    {
    public ()
    
    {
    boolean StopLoop=false;
    int correctNumber = (int)(Math.random()*100+1);
    int userAnswer =0; 
    int tally = 1;
    
    System.out.println("The random number generated was " + correctNumber);
              while (!StopLoop){
              String guess = JOptionPane.showInputDialog
              ("Pick a number between 1 and 100 - your guess? ");
              userAnswer = Integer.parseInt(guess);
             
                        if (userAnswer < 1 || userAnswer > 100){
                        StopLoop=false;
                        }
                        else{
                        StopLoop=true;
                        }
    
              }
    
    }
    
    }
    for loop
    Code:
    import javax.swing.*; 
    import java.awt.*; 
    public class extends JFrame
    {
    public ()
    
    {
    
    int correctNumber = (int)(Math.random()*100+1);
    int userAnswer =0; 
    int tally = 1;
    
    System.out.println("The random number generated was " + correctNumber);
              for (int i=0; i <1; i++){
              String guess = JOptionPane.showInputDialog
              ("Pick a number between 1 and 100 - your guess? ");
              userAnswer = Integer.parseInt(guess);
             
                        if (userAnswer < 1 || userAnswer > 100){
                        i--;
                        }
              }
    }
    
    }
    hope it works.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Your while could also be (without the need to introduce a sentinel value):

    Code:
         while( correctNumber != userAnswer)
         {
             String guess = JOptionPane.showInputDialog
                       ("Pick a number between 1 and 100 - your guess? ");
             userAnswer = Integer.parseInt(guess);
             tries++;
         }
         System.out.println("Way to go, you guessed the number in " + tries + " guesses!");
    The "for loop" is a little tricky for this. I don't know of a good way to do this, right now. Your present loop is evidence of how challenging it is.
    Last edited by nspils; 04-08-2006 at 08:35 PM.

  5. #5
    Join Date
    Feb 2006
    Posts
    14
    The "for loop" is a little tricky for this. I don't know of a good way to do this, right now. Your present loop is evidence of how challenging it is.[/QUOTE]



    Yes it is challenging, but I have completed the doWhile statement without help. I have enclosed the completed copy of the dowhile, so you can get an idea how challenging the dowhile was.

    import javax.swing.*; //shows path to newer swing classes
    import java.awt.*; //shows path to older AWT classes
    public class GuessingGamedw extends JFrame
    {
    public GuessingGamedw() // method name with same name as class.

    {
    int correctNumber = (int)(Math.random()*100+1);//comp picks number 1-100.
    int userAnswer =0; //userAnswer initialized to 0
    int tally = 1;// this is for the tally count.

    System.out.println("The random number generated was " + correctNumber);
    do
    {
    String guess = JOptionPane.showInputDialog
    ("Pick a number between 1 and 100 - your guess? ");
    userAnswer = Integer.parseInt(guess);
    if (userAnswer < 1 || userAnswer > 100)//sets the limit to the numbers between 1 and 100.
    {
    JOptionPane.showMessageDialog
    (null," You entered an incorrect number. You have taken " + tally
    + " guesses. Please try again.");
    tally++;//calculates the number of attempts the users has made to find the correct number.
    }
    else if (userAnswer > correctNumber)
    {
    JOptionPane.showMessageDialog
    (null, "You guessed too high. Pick a number less than " + userAnswer
    + ". You have taken " + tally + " guesses.");
    tally++;//calculates the number of attempts the users has made to find the correct number.
    }
    else if (userAnswer < correctNumber)
    {
    JOptionPane.showMessageDialog
    (null, "You guessed too low. Pick a number greater than " + userAnswer
    + ". You have taken " + tally + " guesses.");
    tally++;//calculates the number of attempts the users has made to find the correct number.
    }
    }while (userAnswer != correctNumber);
    {// the curly brackets are needed because more than one statement for "if" are used.
    if (tally <= 5)
    {
    JOptionPane.showMessageDialog
    (null, " WOW!! You guessed the correct number and your " + tally + " guesses was under 6 "
    + " tries, which is well above average. This game is done.");
    tally++;//calculates the number of attempts the users has made to find the correct number.
    }
    else if (tally <= 10)
    {
    JOptionPane.showMessageDialog
    (null, " Good!! You guessed the correct number and your " + tally + " guesses was under 11 "
    + " tries, which is an average score. This game is done.");
    tally++;//calculates the number of attempts the users has made to find the correct number.
    }
    else if (tally >= 10)
    {
    JOptionPane.showMessageDialog
    (null, " You guessed right, but I hope you pick my number sooner. It took you " + tally
    + " tries, which is way to many. This game is done.");
    tally++;//calculates the number of attempts the users has made to find the correct number.
    }//ends the last block JP statments.
    } // ends the multi-statements for if.

    }// ends GuessingGame constructor
    public static void main(String args [] )
    {
    GuessingGamedw application = new GuessingGamedw();
    System.exit(0);
    }//ends main.
    }// ends class (program).

    // The above should compile and run.

  6. #6
    Join Date
    Feb 2006
    Posts
    14

    Thankyou to all.

    The information that everyone provided should be enough for me to manage to complete the dowhile and for statements. Now I have a general idea of the values that get inserted in the loops and what values or statements that can be used for the "ifs" or "else".

    Again, ThankYou to all for the tutor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center