[英]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.