我执行了以下代码,但是出现了以下错误。 我为最后一部分进行了一些编辑,以便以代码格式进行编辑。 RunUnitConvert.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
/**
*
* This is a test driver for the UnitConverter class
* Write a unit conversion program that asks users to
* identify the unit from which they want to convert and
* the unit to which they want to convert. Legal units
* are in, ft, mi, mm, cm, m, and km. Define two
* objects of a class UnitConverter that convert
* between meters and a given unit.
*
*/
public class RunUnitConverter
{
public static void main(String[] args)
{
BufferedReader br;
char fromUnit = '\0';
char toUnit = '\0';
double value = 0;
// prompt the user to enter unit to convert from
System.out.print("Convert from: ");
// open up standard input
br = new BufferedReader(new InputStreamReader(System.in));
try {
fromUnit = (char)br.read();
} catch (IOException ioe) {
System.exit(1);
}
// prompt user to enter unit to convert to
System.out.print("Convert to: ");
br = new BufferedReader(new InputStreamReader(System.in));
try {
toUnit = (char)br.read();
} catch (IOException ioe) {
System.exit(1);
}
// create new UnitConvert objects
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
// prompt user to enter initial value to convert
System.out.print("Value: ");
br = new BufferedReader(new InputStreamReader(System.in));
try {
value = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.exit(1);
}
double metric = from.toMetric(value);
double converted = to.fromMetric(metric);
if (converted < 0) {
System.out.println("ERROR: Bad Input");
}
else {
System.out.println(value + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
}
UnitConvert.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
/**
*
* This is a test driver for the UnitConverter class
* Write a unit conversion program that asks users to
* identify the unit from which they want to convert and
* the unit to which they want to convert. Legal units
* are in, ft, mi, mm, cm, m, and km. Define two
* objects of a class UnitConverter that convert
* between meters and a given unit.
*
*/
public class RunUnitConverter
{
public static void main(String[] args)
{
BufferedReader br;
char fromUnit = '\0';
char toUnit = '\0';
double value = 0;
// prompt the user to enter unit to convert from
System.out.print("Convert from: ");
// open up standard input
br = new BufferedReader(new InputStreamReader(System.in));
try {
fromUnit = (char)br.read();
} catch (IOException ioe) {
System.exit(1);
}
// prompt user to enter unit to convert to
System.out.print("Convert to: ");
br = new BufferedReader(new InputStreamReader(System.in));
try {
toUnit = (char)br.read();
} catch (IOException ioe) {
System.exit(1);
}
// create new UnitConvert objects
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
// prompt user to enter initial value to convert
System.out.print("Value: ");
br = new BufferedReader(new InputStreamReader(System.in));
try {
value = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.exit(1);
}
double metric = from.toMetric(value);
double converted = to.fromMetric(metric);
if (converted < 0) {
System.out.println("ERROR: Bad Input");
}
else {
System.out.println(value + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
}
这是我的错误 输出和错误如下: javac RunUnitConverter.java char不能被解除引用 switch(strUnit.charAt(0)){ ^ char不能被解除引用 switch(strUnit.charAt(0)){ ^ 2个错误
答案 0 :(得分:2)
您的问题与错误消息所说的完全相同。 &#34; strUnit&#34;是char类型,它不是Object,因此没有方法。