Java 为什么我的代码在else语句之后有编译错误?
请容忍我 创建if-else语句后,每次我检查else语句是否可以返回菜单,如果字符串不正确,它总是以错误结束,如:线程“main”java.lang.NumberFormatException:For input string:“Java 为什么我的代码在else语句之后有编译错误?,java,if-statement,Java,If Statement,请容忍我 创建if-else语句后,每次我检查else语句是否可以返回菜单,如果字符串不正确,它总是以错误结束,如:线程“main”java.lang.NumberFormatException:For input string:“ 违约 如果这是您编写的所有代码,那么编译错误都在enter()中和默认值 我认为您必须在类中实现enter()方法,并将最后一个错误更改为default: 请参阅下面的代码,无编译错误: public class geo { public static vo
违约 如果这是您编写的所有代码,那么编译错误都在
enter()中代码>和默认值代码>
我认为您必须在类中实现enter()
方法,并将最后一个错误更改为default:
请参阅下面的代码,无编译错误:
public class geo {
public static void main(String[] args) {
byte choice = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
float num4;
double result1 = 0;
boolean quit;
String UnitofMeasurement;
String feet = "feet";
String inches = "inches";
DecimalFormat format = new DecimalFormat("0.00");
Scanner key = new Scanner(System.in);
while (choice != 1) {
System.out.println("\t1. Determine the perimeter of a square");
switch (choice) {
case 1:
System.out.println("The perimeter of a square is computed by multiplying the measure of one side by 4.");
System.out.println("Enter the unit of measurement(i.e. inches/feet):");
UnitofMeasurement = key.next();// gets the unit of measurement
if (UnitofMeasurement.equals(feet) || UnitofMeasurement.equals(inches)) {
System.out.println("You have chosen " + UnitofMeasurement + " as the unit of measurement.");
System.out.println("Enter the measurement of one side: ");
num1 = key.nextInt();
System.out.println("Side of the square: " + format.format(num1));
key.nextLine();
result1 = num1 * 4;
System.out.println("Perimeter of the square = " + format.format(result1) + " " + UnitofMeasurement);
} else {
System.out.println("Please only enter feet/inches");
}
break;
default:
}
}
}
}
异常消息不是编译错误。另外,此异常发生在哪一行?java.lang.NumberFormatException:对于输入字符串:“
告诉您试图将字符串转换为数字,而字符串是”
,因此当然,它无法转换为数字。@rgetman它发生在else语句之后
public class geo {
public static void main(String[] args) {
byte choice = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
float num4;
double result1 = 0;
boolean quit;
String UnitofMeasurement;
String feet = "feet";
String inches = "inches";
DecimalFormat format = new DecimalFormat("0.00");
Scanner key = new Scanner(System.in);
while (choice != 1) {
System.out.println("\t1. Determine the perimeter of a square");
switch (choice) {
case 1:
System.out.println("The perimeter of a square is computed by multiplying the measure of one side by 4.");
System.out.println("Enter the unit of measurement(i.e. inches/feet):");
UnitofMeasurement = key.next();// gets the unit of measurement
if (UnitofMeasurement.equals(feet) || UnitofMeasurement.equals(inches)) {
System.out.println("You have chosen " + UnitofMeasurement + " as the unit of measurement.");
System.out.println("Enter the measurement of one side: ");
num1 = key.nextInt();
System.out.println("Side of the square: " + format.format(num1));
key.nextLine();
result1 = num1 * 4;
System.out.println("Perimeter of the square = " + format.format(result1) + " " + UnitofMeasurement);
} else {
System.out.println("Please only enter feet/inches");
}
break;
default:
}
}
}
}