简体   繁体   中英

Please tell me what I'm doing wrong, the if statements and the conversions are not working

I made this program to convert Fahrenheit with Celsius. I'm getting a compile error because it's bypassing the if statements and the conversions aren't doing anything.

     package temp;

        import java.util.Scanner;

this is the class TheTemp

        public class TheTemp {
            private double tempValue;
            private char tempType;
            private static int obj;

this constructor sets the default 0 degrees and Celsius

        public TheTemp(){
            tempValue = 0;
            obj = 0;
        }

this next 3 constructors are for importing the values... this one

        public void setTemp(double tempValue){
            this.tempValue=tempValue;
        }

this one

        public void setTemp(char t){
            if(t == 'C'||t == 'c'){
                obj = 0;
                tempType = t;
            }
            if(t == 'F'||t == 'f'){
                obj = 1;
                tempType = t;
            }
            else{
                throw new IllegalArgumentException("Choose ether C or F");
            }

        }

and this one

        public void setTemp(char t, double tempValue){

            this.tempValue=tempValue;

            if(t == 'C'||t == 'c'){
                obj = 0;
                tempType = t;
            }
            if(t == 'F'||t == 'f'){
                obj = 1;
                tempType = t;
            }
            else{
                throw new IllegalArgumentException("Choose ether C or F");
            }

        }

this are the conversions the first if statement is Celsius to Fahrenheit and the second if statement is Fahrenheit to Celsius

        public void convertValue(){
            if(obj == 0){
                tempValue = 9*(tempValue/5) + 32;
            }
            if(obj == 1){
                tempValue = 5*(tempValue - 32) / 9;
            }
            else{
                throw new IllegalArgumentException("Choose ether C or F");

            }
        }

        public double getTempValue(){
            return tempValue;
        }

this is the program to test the class

            public static void main(String[] args) {
                TheTemp A = new TheTemp();
                Scanner kb = new Scanner(System.in);
                double a;

                A.setTemp(55);
                A.convertValue();
                a=A.getTempValue();
                System.out.println(a);
            }

        }

Okay this code.. is weird in many ways.

Can you say what exactly the compile error is ?

But just as a start

 double tempType = 'c';

in your constructor makes no sense, 'c' is not a double. And you have defined tempType on top as a char, so if anything your constructor should look like this

    public TheTemp(){
        tempType = 'c';
        tempValue = 0;
        obj = 0;
    }

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