Question

JAVA 4.35 (Sides of a Triangle) Write an application that reads three nonzero values entered by...

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether...
(Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2 in c programming
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the accounts. Its main() method uses the CustomerAccounts class to manage a collection of CustomerAccount objects: reading customer accounts data & transactions, and obtaining a String showing the customer accounts data after the operations are complete. You will need to complete the readCustomerAccounts () and applyTransactions() methods in the CustomerAccounts class. First, please fill in your name in the...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...