Skip to content
Advertisement

Adding a Nested Statement

The problem,

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.

I currently have

import java.util.Scanner;
public class FourDecimalThirtySix { 
    public static void main (String[] args) {
        
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three sizes, speparated by spaces");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        input.close();

        if ( (a + b) > c) {
            if ( (a + c) > b) {
                if ( (b + c) > a)
                    System.out.printf("A triangle can be made of %.2f, %.2f, by %.2f.", a,
                        b, c);
                return;
            }
        }
    }
}

I am trying to add an else statement to this for it to say “these values cannot make a triangle” if the original conditions are not meet. Currently the program does not do anything if the 3 conditions are not met

Advertisement

Answer

If any condition is not met the code should print “these values cannot make a triangle”. So you just need to add else blocks for each if statement.

Code:-

import java.util.Scanner;

public class FourDecimalThirtySix {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three sizes, speparated by spaces");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        input.close();

        if ( (a + b) > c) {
            if ( (a + c) > b) {
                if( (b + c) > a)
                    System.out.printf("A triangle can be made of %.2f, %.2f, by %.2f.", a,
                        b, c);
                else{
                    System.out.println("these values cannot make a triangle");
                    return;
                }
            }
            else{
                System.out.println("these values cannot make a triangle");
                return;
            }
        }
        else{
            System.out.println("these values cannot make a triangle");
            return;
        }    
    }
    
}

The above approach is not recommended, as it uses return statements that end the function process abruptly.

A wiser approach is to use a single if with logical and operators and a single corresponding else block.

Code:-

import java.util.Scanner;

public class FourDecimalThirtySix {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three sizes, speparated by spaces");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        input.close();

        if((a+b) > c && (a+c) > b && (b+c) > a) {
            System.out.printf("A triangle can be made of %.2f, %.2f, by %.2f.%n", a, b, c);
        } 
        else{
            System.out.println("these values cannot make a triangle");
        }
        
    }
    
}

This is a cleaner approach.

Happy coding!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement