我是 Java 新手,仍然习惯于细微的差别,所以请原谅任何你可能觉得荒谬的错误。
我正在尝试编写一个存储温度的程序,并且可以用来以摄氏度或华氏度来调用该温度。我唯一的问题是命令行参数,在成功编译我的程序后,我输入以下内容:
java Driver 0.0C 32.0F
然后我得到这个:
Exception in thread "main" java.lang.NumberFormatException: For input string:
"0.0C"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
at java.lang.Float.parseFloat(Float.java:452)
at Driver.main(Driver.java:47)
我的程序还没有完全完善,所以我知道 getter 可以写得非常高效,而且驱动程序甚至不调用温度类,但这不是我现在关心的问题。我的驱动程序应该接受输入并从“C”或“F”字符确定值是摄氏度还是华氏度。然后它解析字符串并截断 C 或 F 并将字符串中包含的值存储为浮点数。我正在使用 Eclipse,程序是面向对象的,这是我的代码:
public class Temperature {
private float temperature;
private char scale;
// default constructor
Temperature() {
this.temperature = 0;
this.scale = 'C';
}
Temperature(float temperatureIn) {
this.temperature = temperatureIn;
this.scale = 'C';
}
Temperature(char scaleIn) {
this.temperature = 0;
this.scale = scaleIn;
}
Temperature(float temperatureIn, char scaleIn) {
this.temperature = temperatureIn;
this.scale = scaleIn;
}
float degreesC(float degreesF) {
float degreesC = (5 * (degreesF - 32)) / 9;
return degreesC;
}
float degreesF(float degreesC) {
float degreesF = (9*(degreesC / 5)) + 32;
return degreesF;
}
void setTemperature(float temperatureIn) {
temperature = temperatureIn;
}
void setScale(char scaleIn) {
scale = scaleIn;
}
void setBothValues(float temperatureIn, char scaleIn) {
temperature = temperatureIn;
scale = scaleIn;
}
int compareTemps(Temperature temp1, Temperature temp2) {
// both values will be compared in Farenheit
Temperature temp1temp = temp1;
if (temp1temp.scale == 'C') {
temp1temp.temperature = degreesF(temp1temp.temperature);
temp1temp.scale = 'F';
}
Temperature temp2temp = temp2;
if (temp2temp.scale == 'C') {
temp2temp.temperature = degreesF(temp2temp.temperature);
temp2temp.scale = 'F';
}
if (temp1temp.temperature == temp2temp.temperature) {
return 0;
}
if (temp1temp.temperature > temp2temp.temperature)
return 1;
if (temp1temp.temperature < temp2temp.temperature)
return -1;
return 0;
}
}
和主驱动程序:
public class Driver {
public static void main(String[] args) {
// ints to hold the temperature values
float temp1Value = 0;
float temp2Value = 0;
// strings to hold the scale types
char temp1Scale = 'C';
char temp2Scale = 'C';
// declare objects of type temperature
Temperature firstTemp = null;
Temperature secondTemp = null;
// copy scale values of temperatures
int scaleIndex = 0;
int scaleIndex2 = 0;
if (args.length > 0) {
if (args[0].indexOf('C') != -1)
{
scaleIndex = args[0].indexOf('C');
temp1Scale = args[0].charAt(scaleIndex);
}
else if (args[0].indexOf('F') != -1)
{
scaleIndex = args[0].indexOf('F');
temp1Scale = args[0].charAt(scaleIndex);
}
if (args[1].indexOf('C') != -1)
{
scaleIndex = args[1].indexOf('C');
temp2Scale = args[1].charAt(scaleIndex2);
}
else if (args[1].indexOf('F') != -1)
{
scaleIndex = args[1].indexOf('F');
temp2Scale = args[1].charAt(scaleIndex2);
}
}
// parse the values to exclude scales and copy to strings holding temperature values
if (args.length > 0) {
temp1Value = Float.parseFloat(args[0].substring(0, scaleIndex));
temp2Value = Float.parseFloat(args[1].substring(0, scaleIndex2));
}
}
}