-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
58 lines (46 loc) · 1.74 KB
/
Triangle.java
File metadata and controls
58 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Sukhamrit Singh
// Triangle
/*
This program asks for the lengths of the three
sides of a triangle and computes the perimeter
if the input is valid. The input is valid if
the sum of every pair of two sides is greater
than the remaining side.
*/
// Importing library to get user input
import java.util.Scanner;
public class Triangle {
public static void main(String[] args){
// Variable that defines the perimeter
double perimeter;
// Variable to keep getting user input
boolean loop = true;
// Loop continuing to the next sides
while (loop) {
// Made a scanner to get user input
Scanner input = new Scanner(System.in);
// Displays a title to enter the sides of the triangle
System.out.print("Enter lengths of sides of the "
+ "triangle: ");
// Turns the users inputted numbers into variables
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
/* If statements determining the perimeter of
the triangle if input is valid, if not valid,
prints statement
*/
if (side1 + side2 > side3 && side2 + side3 >
side1 && side1 + side3 > side2) {
perimeter = side1 + side2 + side3;
System.out.println("The perimeter of the triangle is "
+ perimeter);
} else {
System.out.println("Those sides do not specify a valid "
+ "triangle");
}
// Displays a space after the result
System.out.println("");
}
}
}