Reputation: 37
I have an assignment to write a program that uses the method triangleType that i must write to take three int inputs from the user and output the triangle type. In the method, I first need to sort the integers in ascending order so that the comparisons I am required to use will work correctly. I know I did the sorting part in the code correctly, because I tested it before I even started trying to determine the triangle type. I am required to use these comparisons to find the triangle type: "if A + B <= C, then the sides do not represent a valid triangle. if A = C (all the sides must be the same length) then the triangle is EQUILATERAL. if A = B or B = C, then the triangle is ISOSCELES; otherwise the triangle is SCALENE" For some reasons my if statements at the end of the triangleType method are not working correctly and I get all sorts of output, including "Invalid Triangle" plus other outputs no matter the integers that I enter.
package trianglemethod;
import javax.swing.JOptionPane;
public class TriangleMethod
{
public static void main(String[] args)
{
String wordaA, wordbB, wordcC, answer;
do
{
System.out.println("Please enter all 3 side lengths of the triangle in any order.");
wordaA = JOptionPane.showInputDialog("Enter side 1:");
wordbB = JOptionPane.showInputDialog("Enter side 2:");
wordcC = JOptionPane.showInputDialog("Enter side 3:");
int aA = Integer.parseInt(wordaA);
int bB = Integer.parseInt(wordbB);
int cC = Integer.parseInt(wordcC);
triangleType(aA,bB,cC);
System.out.println("Would you like to enter another triangle?");
answer = JOptionPane.showInputDialog("Would you like to enter another triangle?");
} while (answer.equalsIgnoreCase("yes"));
}
static void triangleType(int aA, int bB, int cC) {
int a=0, b=0, c=0;
if (aA > bB && aA > cC)
{
if (bB > cC)
{
a = cC;
b = bB;
c = aA;
}
else if (cC > bB)
{
a = bB;
b = cC;
c = aA;
}
}
if (bB > aA && bB > cC)
{
if (aA > cC)
{
a = cC;
b = aA;
c = bB;
}
else if (cC > aA)
{
a = aA;
b = cC;
c = bB;
}
}
if (cC > aA && cC > bB)
{
if (aA > bB)
{
a = bB;
b = aA;
c = cC;
}
else if (bB > aA)
{
a = aA;
b = bB;
c = cC;
}
}
if (a+b<=c)
{
JOptionPane.showMessageDialog(null,"Invalid Triangle");
} if (a==c) {
JOptionPane.showMessageDialog(null,"Triangle is Equilateral");
} if (a==b || b==c){
JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
}
else {
JOptionPane.showMessageDialog(null,"Triangle is Scalene");
}
}
}
Upvotes: 1
Views: 177
Reputation: 307
There's actually 2 issues.
if (a==c)
which is true, so it prints "Triangle is Equilateral". Then it checks:
if (a==b || b==c)
which is also true, so it prints "Triangle is Isosceles".
if(aA>=bB && aA>=cC){
a = aA;
if(bB >= cC){
b = bB;
c = cC;
} else {
b = cC;
c = bB;
}
} else if (bB >= aA && bB >= cC){
a = bB;
if(aA >= cC){
b = aA;
c = cC;
} else {
b = cC;
a = aA;
}
} else {
a = cC;
if(cC >= aA){
b = cC;
c = aA;
} else {
b = aA;
c = cC;
}
}
Upvotes: 0
Reputation: 86
I'm confused, why can't you just do
if (a == b && b == c) {
JOptionPane.showMessageDialog(null,"Triangle is Equilateral");
} else if ((a == b && a != c) || (a == c && a != b) || (b == c && b != a)) {
JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
} else if (a != b && a != c && b != c){
JOptionPane.showMessageDialog(null,"Triangle is Scalene");
} else {
JOptionPane.showMessageDialog(null,"Invalid Triangle");
}
Upvotes: 0
Reputation: 312289
You have three different if
statements there that are evaluated separately. Instead, from the description of the problem, it sounds like you need a single if
statement with multiple condition branches (i.e., else if
clauses):
if (a+b<=c)
{
JOptionPane.showMessageDialog(null,"Invalid Triangle");
} else if (a==c) {
JOptionPane.showMessageDialog(null,"Triangle is Equilateral");
} else if (a==b || b==c){
JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
} else {
JOptionPane.showMessageDialog(null,"Triangle is Scalene");
}
Upvotes: 1