I just started a java class at uni, and am trying to solve a practise exercise where the code reads two angles of a triangle and outputs the third angle.
I wrote an if-statement to make sure the sum of the three angles would never be larger than 180, but the terminal seems to ignore the "else" part of the if-statement completely.
What am I missing?
Originally, I had written "if (triangle == 180) ...", then "if (triangle != 180) .." etc, switching things around. But the "else" is still ignored. In the below code snippet I added a separate int for the triangle, and another one for the sum of all angles.
This is the code I'm working with now:
import java.util.Scanner;
public class Ex2 {
public static void main(String[] args) {
Scanner wheatley = new Scanner(System.in);
System.out.println("Write two angles of a triangle.");
int firstAngle = wheatley.nextInt();
wheatley.nextLine();
int secondAngle = wheatley.nextInt();
wheatley.nextLine();
int thirdAngle = 180 - firstAngle - secondAngle;
int angleSum = firstAngle + secondAngle + thirdAngle;
int triangle = 180;
if (angleSum == triangle) {
System.out.println("The third angle is " + thirdAngle + " degrees.");
} else {
System.out.println("This is not a triangle.");
}
}
}
After compiling the code and inputting the two angles I get the following result:
"Write two angles of a triangle. 200 200 The third angle is -220 degrees."
I was expecting:
"Write two angles of a triangle. 200 200 This is not a triangle."
Aucun commentaire:
Enregistrer un commentaire