Home > database >  Adding a Nested Statement
Adding a Nested Statement

Time:02-08

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

CodePudding user response:

Use a single compound if with logical and (&&) and add an else like

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.printf("A triangle can't be made of %.2f, %.2f, by %.2f.%n", a, b, c);
}

CodePudding user response:

If you fix the indentation that would make it easier for you to see the format of the code. Then, if you want the logic of the code block to only show the statement "these values cannot make a triangle" if the 3 preceding conditions are not met then you can add the else {} after the third (inner most) if {}.

This will result in the else block executing only if the first two ifs are evaluated to be true. So you might need to consider what you want to print after each statement, not just if the first two are true and the last if {} evaluates to false.

You can find useful documentation on java code styling here (link I added is specifically for indentation rules): https://www.cs.cornell.edu/courses/JavaAndDS/JavaStyle.html#Indentation

CodePudding user response:

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!

  •  Tags:  
  • Related