dimanche 8 octobre 2017

Java : Only the first if-statement working

So, I tried to make a simple phytagoras 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 pytaghoras_console {
public static void main (String Args[]){
    String  shypo,scat1,scat2;
    double  hypo, cat1, cat2;
    boolean hypoSelected, cat1Selected, cat2Selected;

    Scanner 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();

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

    cat1 = Double.parseDouble(scat1);
    cat2 = Double.parseDouble(scat2);
    hypo = Double.parseDouble(shipo);

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

//HERE THE METHOD FUNCTION
public static double hypoC(double cat1,double cat2){
    double ohypo;
    ohipo = 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?

Aucun commentaire:

Enregistrer un commentaire