I’m having problems with my do-while loops and my while loop. Also my switch statement ends and does not loop to ask for another input. I’m confused. Any suggestion that can help me or send me one the right track I would appreciate it.
This is the assignment:

The Miskatonic University’s math department wants you to write a computer program that will find all of the Rectangular Prisms with integer sides whose volume is equal to an inputted integer volume of cubic units (units can be feet, inches, meters, or whatever according to your preference).

The Miskatonic mathematicians realize that nested repetition will be required to solve this problem, and never missing a chance to test their statistical skills, they have an additional specification to this problem. The mathematicians have heard of while, for, and do-while loops and are interested in seeing how well they perform compared to one another. As such once you have discovered an algorithm to solve their Rectangular Prism Options problem, they want you to code the solution three times: once using only while loops, once using only for loops, and once using only do-while loops. The mathematicians want all of this code in one Java program which will allow the user to select which version of the algorithm to use (i.e., “Press 1 for while loops, Press 2 for for loops, Press 3 for do-while loops). They require that you use a switch statement to accomplish this selection.

If the user inputs any non-positive values for the volume of the rectangular prism, then the program should display an error message and prompt the user for input again. Your list of Rectangular Prism Options should be displayed in a JScrollPane within a dialog box as shown in the example output below.


Here is my code so far:

import javax.swing.*;

    public class RectangularPrism
   {
       public static void main(String args[])
      {
         int counter = 0;                       //Sets counter to 1
         int a, b, c, volume;                   //Volume of rectangular prism
         int i;											//Case counter
         int choice;                            //User's choice of method to solve                     
         String inputFeet, inputChoice;         //Value entered by user
         String output = "Solution #\tL*W*H\n";
      
      //Input Dialog Box for volume of recangular prism
         inputFeet = JOptionPane.showInputDialog("Enter volume of a rectangular prism in feet");
         volume = Integer.parseInt(inputFeet);
      
      //Input Dialog Box for choice of repetition method
         inputChoice = JOptionPane.showInputDialog("Enter 1 to solve by While loops\nEnter 2 to solve by For loops\nEnter 3 to solve by a Do-While loop");
         choice = Integer.parseInt(inputChoice);
      
      //Start user choice selection
         switch (choice)
         {
            case 1: //While loop solution
               a = 0; b = 0; c = 0; i = 0;
               while (a <= volume)
               {
                  a++;
                  while (b <= volume)
                  {
                     b++;
                     while (c <= volume)
                     {              
                        c++;              
                        while (a * b * c == volume)
                        {
                           counter++;
                           output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
                           a = 0;
                        }
                     }
                  }
               }
              	 
               break;
                     
            case 2: //For loop solution (Finished)
               for (a = 0; a <= volume; a++)
               {
                  for (b = 0; b <= volume; b++)
                  {
                     for (c = 0; c <= volume; c++)
                     {
                        if ( a *  b * c == volume)
                        {
                           counter++;
                           output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
                        }
                     }
                  }
               }
               break;
         
            case 3: //Do-while loop solution
               a = 0; b = 0; c = 0;        
               do
               {
                  do
                  {
                     do
                     {
                        do
                        {
                           c++;
                        }while(c <= volume);
        						b++;
                     }while(b <= volume);
       					b = 0;              
							b++;
                  }while(c <= volume);
                  counter++;
                  output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
               }while(a * b * c == volume);
               break;
            
            default: //Message diplayed if not a valid choice is chosen
               JOptionPane.showMessageDialog(null,"Please enter a valid choice!");
         }
      
      //Output += counter + ".\t" + volume;
      //Output Dialog Box Outside informatio
         JTextArea area = new JTextArea(15, 10);
         area.setText(output);
         JScrollPane scroller = new JScrollPane(area);
         JOptionPane.showMessageDialog(null, scroller, "Volume of " + volume, JOptionPane.INFORMATION_MESSAGE);
         System.exit(0);//End Program
      }//End Main
   }//End Class Rectangular Prism

Recommended Answers

All 6 Replies

where is your while loop ?? I didnt find any in your code.

Case 1 and 2 I cannot come up with the solution.

To solve the problem with not looping to ask for a new input, you need to include the entire Dialog, Switch statements in a Do-while loop too.
In essance your main app, only asks for input once, and then processes the switch statment, and then exits the program.

I have updated my solution to work better...I need now to find a answer to my solutions because they are not all listed.

import javax.swing.*;

    public class RectangularPrism
   {
       public static void main(String args[])
      {
         int counter = 0;                       //Sets counter to 1
         int a, b, c, volume;                   //Volume of rectangular prism
         int choice;                            //User's choice of method to solve                     
         String inputFeet, inputChoice;         //Value entered by user
         String output = "Solution #\tL*W*H\n";
      
      //Input Dialog Box for volume of recangular prism
         inputFeet = JOptionPane.showInputDialog("Enter volume of a rectangular prism in feet");
         volume = Integer.parseInt(inputFeet);
      
      //Input Dialog Box for choice of repetition method
         inputChoice = JOptionPane.showInputDialog("Enter 1 to solve by While loops\nEnter 2 to solve by For loops\nEnter 3 to solve by a Do-While loop");
         choice = Integer.parseInt(inputChoice);
      
      //Start user choice selection
         switch (choice)
         {
            case 1: //While loop solution
               a = 0; b = 0; c = 0; i = 0;
               while (a <= volume)
               {
                  b = a + 1;
                  while (b <= volume)
                  {
                     c = 1;
                     if (a * b * c == volume)
                     {
                        counter++;
                        output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
                     }
                     c++;
                     b++;
                  }
                  a++;
               }
              	 
               break;
                     
            case 2: //For loop solution 
                
               for (a = 0; a <= volume; a++)
               {
                  for (b = a + 1; b <= volume; b++)
                  {
                     c = 1;
                     if (a *  b * c == volume)
                     {
                        counter++;
                        output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
                     }
                     c++;
                  }
               }
               break;
         
            case 3: //Do-while loop solution
               a = 0; b = 0; c = 0;        
               do
               {
                  b = a + 1;
                  do
                  {
                     c = 1;
                     if (a * b * c == volume)
                     {
                        counter++;
                        output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
                     }
                     c++;
                     b++;
                  }while(b <= volume);
                  a++;
               }while(a <= volume);
               break;
            
            default: //Message diplayed if not a valid choice is chosen
               JOptionPane.showMessageDialog(null,"Please enter a valid choice!");
         }
      
      //Output += counter + ".\t" + volume;
      //Output Dialog Box Outside informatio
         JTextArea area = new JTextArea(15, 10);
         area.setText(output);
         JScrollPane scroller = new JScrollPane(area);
         JOptionPane.showMessageDialog(null, scroller, "Volume of " + volume, JOptionPane.INFORMATION_MESSAGE);
         System.exit(0);//End Program
      }//End Main
   }//End Class Rectangular Prism

Almost perfect!!! One more thing to do is get my while loop solutions right (case 1). Everything else is great!!! This should not be hard for someone to figure out.

import javax.swing.*;

    public class RectangularPrism
   {
       public static void main(String args[])
      {
         int counter = 0;                       //Sets counter to 1
         int a, b, c, volume;                   //Volume of rectangular prism
         int choice;                            //User's choice of method to solve                     
         String inputFeet, inputChoice;         //Value entered by user
         boolean run = false;                   //Choice Selection is wrong
         String output = "Solution #\tL*W*H\n";
      
      //Input Dialog Box for volume of recangular prism
         inputFeet = JOptionPane.showInputDialog("Enter volume of a rectangular prism in feet");
         volume = Integer.parseInt(inputFeet);
         while(!run)
         {
         //Input Dialog Box for choice of repetition method
            inputChoice = JOptionPane.showInputDialog("Enter 1 to solve by While loops\nEnter 2 to solve by For loops\nEnter 3 to solve by a Do-While loop");
            choice = Integer.parseInt(inputChoice);
            if ((choice <= 3) && (choice >= 1))
            {
            //Start user choice selection
               switch (choice)
               {
                  case 1: //While loop solution
                     a = 0;
                     while (a <= volume)
                     {
                        b = a;
                        while (b <= volume)
                        {
                           c = a;
                           while (c <= volume)
                           {
                              if (a * b * c == volume)
                              {
                                 counter++;
                                 output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
                              }
                              c++;
                           }
                           b++;
                        }
                        a++;
                     }
                  
                     break;
                     
                  case 2: //For loop solution 
                     for (a = 0; a <= volume; a++)
                     {
                        for (b = a; b <= volume; b++)
                        {
                           for (c = b; c <= volume; c++)
                           {
                              if (a *  b * c == volume)
                              {
                                 counter++;
                                 output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
                              }
                           }
                        }
                     }
                     break;
               
                  case 3: //Do-while loop solution
                     a = 0;        
                     do
                     {
                        b = a;
                        do
                        {
                           c = b;
                           do
                           {
                              if (a * b * c == volume)
                              {
                                 counter++;
                                 output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
                              }c++;
                           }while(c <= volume);
                           b++;
                        }while(b <= volume);
                        a++;
                     }while(a <= volume);
                     break;
               
                  default: //Message diplayed if not a valid choice is chosen
                     JOptionPane.showMessageDialog(null,"Please enter a valid choice!");
               }
            
            //Output Dialog Box Outside information
               JTextArea area = new JTextArea(15, 10);
               area.setText(output);
               JScrollPane scroller = new JScrollPane(area);
               JOptionPane.showMessageDialog(null, scroller, "Volume of " + volume, JOptionPane.INFORMATION_MESSAGE);
               run = true;
            }
            else//Message displayed if not a valid choice is chosen 
            {
               JOptionPane.showMessageDialog(null,"Please enter a valid choice!");
            }//End If/Else Statement
         }//End While Statement
         System.exit(0);//End Program
      }//End Main
   }//End Class Rectangular Prism

Finnaly finish it...In case 1 it is c = b not c = a. Thanks to everyone who tryed to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.