简体   繁体   English

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

[英]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.我收到一个编译错误,因为它绕过了 if 语句并且转换没有做任何事情。

     package temp;

        import java.util.Scanner;

this is the class TheTemp这是 TheTemp 类

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

this constructor sets the default 0 degrees and Celsius此构造函数设置默认的 0 度和摄氏度

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

this next 3 constructors are for importing the values... this one接下来的 3 个构造函数用于导入值......这个

        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这是第一个 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;
        }

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.在您的构造函数中没有意义,'c' 不是双精度数。 And you have defined tempType on top as a char, so if anything your constructor should look like this并且您已在顶部将 tempType 定义为字符,因此如果有任何内容,您的构造函数应如下所示

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

暂无
暂无

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

相关问题 谁能告诉我我在做什么错? -堆栈 - Can anyone tell me what I'm doing wrong? - Stacks 有人可以告诉我在Java中设置此Robot类时我做错了什么吗? - Can someone tell me what I'm doing wrong to setup this Robot class in Java? 有人能告诉我我做错了什么吗? 通过LinkedList计数和循环 - Can someone tell me what i'm doing wrong? Counting and looping through LinkedList 有一种有效的“合并排序”功能,但不是完全可以,有人可以告诉我我做错了什么吗? - Have a sort of working Merge Sort, but not quite, could someone tell me what I am doing wrong? Java 的新手,不确定我做错了什么。 我的 If 语句不起作用 - New to Java, and not sure what I'm doing wrong. My If statements aren't working HackerRank 任务“Mini Max Sum”解决方案未通过 13 个测试用例中的 3 个,有人能告诉我我做错了什么吗 - HackerRank Task “Mini Max Sum” solution not passing 3 of the 13 test cases, can someone tell me what i'm doing wrong Java:请告诉我怎么了? - Java: Please tell Me what's wrong? replaceAll方法对我不起作用我在做什么错? - The replaceAll method is not working for me what am I doing wrong? 我正在尝试使用getParameterName()。但是异常正在上升。如果我做错了方法,请告诉我正确的方法 - i am trying to use getParameterName().but exception is raising.please tell me the correct way if i am doing in wrong way 请我在这段代码中做错了什么 - Please what am i doing wrong in this code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM