Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default Do Whiles loops with JOptionPane help?

    I need to write 3 JOptionPane.ShowMessage using do and while loops.

    Calculate the volume and print the answer in the JOptionPane dialogbox.

    #1) prompts the user to select height between 1 and 50
    if the user enters outside the range, simply repromt. Go to next condition(#2) if successful.

    #2) prompts the user to select width 1 and 300
    if the user enters outside the range, simply repromt.Go to next condition(#3) if successful.

    #3) prompts the user to select length between 1 and 10
    if the user enters outside the range, simply repromt.


    And then multiply them and show the answer in single JOptionPane dialog box.


    My attempt:

    Java Code:
    import javax.swing.JOptionPane;
     
    public class Volume{
      public static void main(String args[])
      {
       
        double height;
        double length;
        double width;
        double volume;
       
        do{
          height = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter height between 1 and 50"));
        }
        while((height > 0 && height <50) || height < 0);
       
        do{
          length = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter length between 1 and 300"));
        }
        while((length > 0 && length <300) || length < 0);
       
        do{
          width = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter width between 1 and 10"));
        }
        while((width > 0 && width <300) || width < 0);
       
        volume = height * length * width;
        JOptionPane.showMessageDialog(null, "The Volume is " +volume, JOptionPane.PLAIN_MESSAGE);
      }
    }
    Last edited by racewithferrari; 10-07-2012 at 08:08 PM. Reason: formatting

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Do Whiles loops with JOptionPane help?

    What is not working or what errors are you seeing with your attempt?

  3. #3
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default Re: Do Whiles loops with JOptionPane help?

    eh! I am not totally familiar with the syntax when multiple writing conditions.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Do Whiles loops with JOptionPane help?

    Quote Originally Posted by racewithferrari View Post
    eh! I am not totally familiar with the syntax when multiple writing conditions.
    This deals not so much Java programming expertise but rather basic logic.

    For instance, if you want the user to enter a number, x, that is greater than 0 and less then some number, X_MAX, then you'll want to repeatedly get the input while these conditions are not met.

    So you know that you want to repeat the loop if x is < 0, and you want to repeat the loop if x >= X_MAX. So picture the two possible ways of doing this:

    Java Code:
    // declare int variable x
    int x;
    do {
      // ... get user input and put into variable x
    while (x < 0 && x > X_MAX);
    vs.
    Java Code:
    // declare int variable x
    int x;
    do {
      // ... get user input and put into variable x
    while (x < 0 || x > X_MAX);
    In the first situation using &&, x must be both less than 0 and at the same greater than or equal to X_MAX. Is this ever possible? In the second situation using the or operator, ||, x must be less than 0 or it must be greater than or equal to X_MAX. Which seems the most logical to use?

Similar Threads

  1. Need help with while loops
    By tangel in forum New To Java
    Replies: 0
    Last Post: 10-04-2011, 04:37 AM
  2. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  3. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 06:51 PM
  4. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 08:30 PM
  5. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 03:59 AM

Tags for this Thread

Posting Permissions

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