I have been working on this project for a bit now and I had a few questions because I am stumped.
I have it set up as a loop. Every time you would not select a one of the proper numbers it would error out. So what I did was create an error box to appear when you don't select the right one (right below). It won't pop up until after you selected and entered in a number then it it will pop up every time you select something. I think it has something to do with my placing of the code. One of my friends was saying it should break before it loops but if it breaks then the loop stops. Is there a way to have it break then start back in the loop?
"if(choice!=1||choice!=2||choice!=3||choice!=4||choice!=5)
JOptionPane.showMessageDialog(null,"Wrong option entered", " error",
JOptionPane.ERROR_MESSAGE);"
Next every time you choose to select a number it will error out. I have no clue how to fix this problem.
// This program is a loop that will calculate the area of shapes
// Written by Ian Dudek
import javax.swing.JOptionPane;
public class app1{
public static void main(
String args[])
{
String first,second;
double choice ;
double radius,width,area, length;
//intialize the string
String value=" ";
value =JOptionPane.showInputDialog("Please chose one of the options:"+"\n" +
"a)Enter 1 to calculate the area of the Circle"+ "\n"+
"b)Enter 2 to calculate the area of the Triangle"+ "\n"+
"c)Enter 3 to calculate the area of the Square"+ "\n"+
"d)Enter 4 to calculate the area of the Rectangle"+"\n"+
"e)Enter 5 to calculate the area of the Cube"+"\n"+
"f)Enter 6 to exit the program");
choice=Double.parseDouble(value);
// while option is not 6 continue
while(choice!=6){
//if selected number calculate the area of circle
if(choice==1){
first = JOptionPane.showInputDialog("Enter the value of radius");
radius = Double.parseDouble(first);
area = Math.PI*radius*radius;
//print out the result
JOptionPane.showMessageDialog(null,"The area of the Circle: "+area,"result",
JOptionPane.INFORMATION_MESSAGE);
}
//If selected number is 2 calculate the area of triangle and prints result
else if(choice==2){
first=JOptionPane.showInputDialog("Enter the value of lenght");
second=JOptionPane.showInputDialog("Enter the value of width");
length = Double.parseDouble(first);
width=Double.parseDouble(second);
area= (width*length)/2;
JOptionPane.showMessageDialog(null,"The area of triangle: "+ area,"result",
JOptionPane.INFORMATION_MESSAGE);
}
//If selected number is 3 calculate the area of square and prints result
else if(choice==3){
first = JOptionPane.showInputDialog("Enter the value of length");
length = Double.parseDouble(first);//ge string into integer
area=length*length;
JOptionPane.showMessageDialog(null,"The area of the square: "+ area," result",
JOptionPane.INFORMATION_MESSAGE);
}
//If selected number is 4 calculate the area of rectangle and prints result
else if(choice==4){
first=JOptionPane.showInputDialog("Enter the value of length");
second=JOptionPane.showInputDialog("Enter the value of width");
length=Double.parseDouble(first);
width=Double.parseDouble(second);
area=width*length;
JOptionPane.showMessageDialog(null,"The area of the rectangle: "+ area,"result",
JOptionPane.INFORMATION_MESSAGE);
}
//If selected number is 5 calculat the area of cube and prints result
else if(choice==5) {
first=JOptionPane.showInputDialog("Enter the value of length");
length=Double.parseDouble(first);
area=6*length;
JOptionPane.showMessageDialog(null,"The area of the cube: "+ area,"result",
JOptionPane.INFORMATION_MESSAGE);
}
value =JOptionPane.showInputDialog("Please chose one of the options:"+"\n" +
"a)Enter 1 to calculate the area of the Circle"+ "\n"+
"b)Enter 2 to calculate the area of the Triangle"+ "\n"+
"c)Enter 3 to calculate the area of the Square"+ "\n"+
"d)Enter 4 to calculate the area of the Rectangle"+"\n"+
"e)Enter 5 to calculate the area of the Cube"+"\n"+
"f)Enter 6 to exit the program");
choice=Double.parseDouble(value);
// If choice is not 1-5 a error message will pop up with an error
if(choice!=1||choice!=2||choice!=3||choice!=4||choice!=5)
JOptionPane.showMessageDialog(null,"Wrong option entered", " error",
JOptionPane.ERROR_MESSAGE);
//end of while loop if number selected is 6
}
System.out.println("Program terminated\n");
System.exit(0);
}
}