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