JAVA 4.35 (Sides of a Triangle) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle.
Here's the code:
import java.util.Scanner;
class TriangleYN {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
double a;
double b;
double c;
System.out.print("Enter three
sizes, separated by spaces(decimals values are
acceptable):");
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
if((a+b) > c)
{
if((a+c) >
b)
{
if((b+c) > a)
{
System.out.printf("A triangle
could measure %.2f, %.2f, by %.2f.", a, b, c);
return;
}
}
}
}
}
So I'm having a problem getting the expected results.
Given the following was entered from the keyboard:
25 13 12
you displayed:
Enter three sizes, separated by spaces(decimals values are
acceptable):
instead of:
Enter three sizes, separated by spaces(decimals values are
acceptable):A triangle could not measure 25.00, 13.00, by
12.00.
Code:
import java.util.Scanner;
class TriangleYN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double a;
double b;
double c;
do {
System.out.print("Enter three sizes, separated by
spaces(decimals values are acceptable):");
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
}
while(!((a + b > c)&&(b + c > a) &&(c + a
> b)));
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.