Search code examples
javaif-statementbooleanjcreator

If statement [help] New to Java


I am REALLY new at Java and I've tried to make a simple program to find the sides of a triangle (Side,side,side OR side,angle,side). But I when I run it in Jcreator it asks "Are you working with an SSS?[y/n]" when I type in "y" or "n" it always comes up with the same answer. I have no idea what it is.

Here's the code:

import java.util.Scanner;

public class CosineLaw {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        boolean sss =true;

        System.out.println("Are you working with an SSS?[y/n]");
        char askingSSS =keyboard.next().charAt(0);  
        if(sss == true){
            System.out.println("Please enter the 3 sides:");
        }else if(sss == false){
            System.out.println("Please enter the 2 sides and 1 angle:");
        }
    }
}

Solution

  • There are two issues in your code:

    1) You never changed the boolean value of sss

    boolean sss =true;
    

    The value is always true and never changed.

    2) You dont require the if statement in else.

    if(sss == false)
    

    The above statement inside else is not making any sense

    You can better try something like this:

    if(askingSSS == 'y')
    else