繁体   English   中英

请告诉我我做错了什么,if 语句和转换不起作用

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

我制作了这个程序来将华氏度转换为摄氏度。 我收到一个编译错误,因为它绕过了 if 语句并且转换没有做任何事情。

     package temp;

        import java.util.Scanner;

这是 TheTemp 类

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

此构造函数设置默认的 0 度和摄氏度

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

接下来的 3 个构造函数用于导入值......这个

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

这个

        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");
            }

        }

和这个

        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");
            }

        }

这是第一个 if 语句是摄氏度到华氏度的转换,第二个 if 语句是华氏度到摄氏度的转换

        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;
        }

这是测试课程的程序

            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);
            }

        }

好的,这段代码在很多方面都很奇怪。

你能说出编译错误到底是什么吗?

但作为一个开始

 double tempType = 'c';

在您的构造函数中没有意义,'c' 不是双精度数。 并且您已在顶部将 tempType 定义为字符,因此如果有任何内容,您的构造函数应如下所示

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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