lundi 23 octobre 2017

Switch statement to If statements - JAVA

new to programming here. I am having a bit of difficulty to transform a Switch statement to an If statement. Any help and explanation would be appriciated.

` public void setScale(String scale){ //everything will be in lower case switch (scale.toLowerCase()) {

        //convert for Celsius - 0 since first place holder in array
        case "c":
        case "celsius":

            if (!this.scale.equals(scales[0])) {

                convertToCelsius();
                this.scale = scales[0];
            }

            break;

        //convert for fahrenheit - 2nd place holder in array
        case "f":
        case "fahrenheit":
            if (!this.scale.equals(scales[1])) {

                convertToFahrenheit();
                this.scale = scales[1];
            }
            break;

        //convert for kelvin - last place holder in array    
        case "k":
        case "kelvin":
            if (!this.scale.equals(scales[2])) {
                convertToKelvin();
                this.scale = scales[2];
            }
            break;

        default:
            System.out.println("Invalid value of scale! Try again");
    }

} '

Aucun commentaire:

Enregistrer un commentaire