Reputation: 165
For my assignment I am using JOptionPane to ask the user to enter 3 sides of a triangle. The program is then suppose to use JOptionPane to tell them the type of triangle (equilateral, right, acute, obtuse, and isosceles but isosceles triangles are also either right, obtuse or acute) it is and to calculate the area.
When it is equilateral it comes back telling me that it is equilateral but it also tells me it is isosceles 3 times. Even though I only have the isosceles JOptionPane with the other types of triangles.
My other problem is that when it is right, acute, or obtuse it skips the JOptionPane telling me what type it is and just tells me it is isosceles.
package assignment.ii;
import javax.swing.JOptionPane;
import java.lang.*;
public class AssignmentII
{
public static void main(String[] args) {
int a = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a side of the triangle "));
int b = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a side of the triangle "));
int c = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a side of the triangle "));
double s = (.5*(a+b+c));
if(a==b){
if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a equilateral triangle");
}else if (((a*a)+(b*b)) == (c*c)) {
JOptionPane.showMessageDialog(null, "The triangle is a right triangle");
if (a==b)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (a==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
}else if (((a*a)+(b*b))<(c*c)){
JOptionPane.showMessageDialog(null, "The triangle is an obtuse triangle");
if (a==b)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (a==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
}else if (((a*a)+(b*b))>(c*c))
JOptionPane.showMessageDialog(null, "The triangle is an acute triangle");
if (a==b)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (a==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
double d;
d = ((s)*(s - a)*(s - b)*(s - c));
JOptionPane.showMessageDialog(null, "The area of the triangle is: " + Math.sqrt(d));
}
}
Upvotes: 1
Views: 1729
Reputation: 879
Another way to organize this in a less confusing manor is to check each attribute separately. Create separate methods to determine each thing you need to know. Then all the cases aren't all mixed up together. Something like:
int angle = getTriangleAngle(a,b,c);
boolean isocelesTriangle = isIsocelesTriangle(a,b,c);
boolean equalateralTriangle = isEqualateralTriangle(a,b,c);
int area = calcTriangleArea(a,b,c);
Then you can go about building the output string from those values.
Upvotes: 0
Reputation: 8188
You are definitely missing the "else" keyword before the "if statements" that compare the sides of the triangles. Try this:
if (a==b)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
else if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
else if (a==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
Or more concisely:
if (a==b || b==c || c==a)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
Another problem is that
if (a==b) {
if (b==c) {
}
}
is not the same as
if ((a==b) && (b==c)) { }
In the first case, if a==b but b != c then nothing will be executed while in the second case, your code will skip to the next else if
statement.
Upvotes: 3