我试图创建一个温度转换器,用户输入华氏温度,然后以摄氏度返回温度。这是代码:
import javax.swing.*;
public class tempconv {
public static void main (String[]args) {
int fahr, cel;
String fahrstring;
fahrstring = JOptionPane.showInputDialog(valueof("Enter your temperature in F:"));
fahr = new int[fahrstring];
cel = (fahr - 32) * 5/9;
JOptionPane.showMessageDialog(null, "The temperature in c is " + cel);
}
}
我试图将给定的输入对话框转换为int,但编译器阻止我说:
error: cannot find symbol
fahrstring = JOptionPane.showInputDialog(>valueof("Enter your temperature in F:"));
所以它必须是语法错误。根据编译器我还有另一个错误说
tempconv.java:10: error: incompatible types: int[] cannot be converted to int
fahr = >new int[fahrstring];
如何编写正确的代码以及我究竟做错了什么?
答案 0 :(得分:1)
使用 double 而不是 int ,因为温度可能 29.5 有时候
double fahr = Double.parseDouble(fahrstring);
double c = ((fahr - 32) * (5/9));
希望你的问题得到解决。保持编码好运。
答案 1 :(得分:0)
将fahrstring转换为int的另一种方法是使用函数:
Integer.parseInt(fahrstring)
(这是一个演示parseInt的教程) http://www.tutorialspoint.com/java/lang/integer_parseint.htm
答案 2 :(得分:0)
关于incompatible types
错误:
int fahr
是一个整数,new int [fahrString]
将创建一个数组对象。 fahr=new int [fahrstring]
表示您正在尝试将数组对象分配给int
变量。这显然不会起作用。
你能做些什么?
int fahr
更改为int fahr[]
,并且不会再次显示兼容性错误。