简体   繁体   中英

Java : Only the first if-statement working

So, I tried to make a simple pythagoras app that can measure the side length of a triangle after inputting the length of the 2 other sides. To measure that side, user supposed to input 'x' or 'X' into one of three sides of the triangle. Here's my code:

    import java.util.Scanner;
    public class pythagoras_1 {
    public static void main (String Args[]){
    //string data type to check user input 'x' or not
    String  shypo,scat1,scat2;
    double  hypo, cat1, cat2;
    boolean hypoSelected, cat1Selected, cat2Selected;

    Scanner console = new Scanner (System.in);

    System.out.print( "Hypotenuse = ");
    shypo = console.next();

    System.out.print("Cathetus 1 = ");
    scat1 = console.next();

    System.out.print("Cathetus 2 = ");
    scat2 = console.next();

    hypoSelected = shypo.equals("x")||shypo.equals("X");
    cat1Selected = scat1.equals("x")||scat1.equals("X");
    cat2Selected = scat2.equals("x")||scat2.equals("X");

    if(hypoSelected){
        cat1 = Double.parseDouble(scat1);
        cat2 = Double.parseDouble(scat2);
        System.out.println("Hypotenuse = "+ hypoC(cat1,cat2));
    }
    else if(cat1Selected){
        hypo = Double.parseDouble(shypo);
        cat2 = Double.parseDouble(scat2);
        System.out.println("cathetus 1 = "+ Cat1C(hypo,cat2));
    }
    else if(cat2Selected){  
        hypo = Double.parseDouble(shypo);
        cat1 = Double.parseDouble(scat1);
        System.out.println("cathetus 2 = "+ Cat2C(hypo,cat1));
    }
    else{
        System.err.println("ERROR");
    }
}

//HERE THE METHOD FUNCTION
public static double hypoC(double cat1,double cat2){
    double ohypo;
    ohypo = Math.sqrt((cat1*cat1)+(cat2*cat2));
    return ohypo;
}
public static double Cat1C(double hypo,double cat2){
    double ocat1;
    ocat1 = Math.sqrt((hypo*hypo)-(cat2*cat2));
    return ocat1;
}
public static double Cat2C(double hypo,double cat1){
    double ocat2;
    ocat2 = Math.sqrt((hypo*hypo)-(cat1*cat1));
    return ocat2;
}
}

But it works only on hypotenuse(the first if-statement), if I input 'x' on cathetus 1 or 2 the app ends without executing the code. How can I fix this?

here's the output when scat1 or scat 2 value is 'x'

 run:
 Hypotenuse = 5
 Cathetus 1 = 4
 Cathetus 2 = x
 ERROR
 BUILD SUCCESSFUL (total time: 5 seconds)

here's the output when shypo is 'x'

 run:
 Hypotenuse = x
 Cathetus 1 = 4
 Cathetus 2 = 3  
 Hypotenuse = 5.0
 BUILD SUCCESSFUL (total time: 6 seconds)

Error i was getting in your code were typo's and parsing exception since it was trying to parse x to double, that's the reason it was not executing code. check logs of your program for that.

Corrected Main function of your program

public static void main(String Args[]) {
        String shypo, scat1, scat2;
        Scanner console = null;
        try {
            console = new Scanner(System.in);
            // input of each side
            System.out.print("hypotenuse = ");
            shypo = console.next();

            System.out.print("cathetus 1 = ");
            scat1 = console.next();

            System.out.print("cathetus 2 = ");
            scat2 = console.next();

            if ("x".equalsIgnoreCase(shypo)) {
                System.out.println("Hypotenuse = " + hypoC(Double.parseDouble(scat1), Double.parseDouble(scat2)));
            } else if ("x".equalsIgnoreCase(scat1)) {
                System.out.println("cathetus 1 = " + Cat1C(Double.parseDouble(shypo), Double.parseDouble(scat2)));
            } else if ("x".equalsIgnoreCase(scat2)) {
                System.out.println("cathetus 2 = " + Cat2C(Double.parseDouble(shypo), Double.parseDouble(scat1)));
            } else {
                System.err.println("ERROR");
            }
        } finally {
            if (console != null)
                console.close();
        }
    }

You've got loads of typos:

cat2Selected = scat1.equals("x")||scat1.equals("X");

Change scat1 to scat2

This is the key one:

  hypo = Double.parseDouble(shipo);

shipo should be shypo , as otherwise hypo is undefined in calls to Cat1C or Cat2C . and further, 'x' is not going to parse as a Double . You could place the definitions of hypo , cat1 and cat2 inside the condition.

Finally:

 ohipo = Math.sqrt((cat1*cat1)+(cat2*cat2));

ohipo should be ohypo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM