为啥我的代码在 else 语句后出现编译错误?

Posted

技术标签:

【中文标题】为啥我的代码在 else 语句后出现编译错误?【英文标题】:Why does my code have a compilation error after the else statement?为什么我的代码在 else 语句后出现编译错误? 【发布时间】:2019-02-09 17:39:48 【问题描述】:

忍受我。

创建 if-else 语句后,每次我检查 else 语句时,如果字符串不正确,它是否可以返回菜单,它总是以错误结束,例如:线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“”

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");

                
            enter();
            break;

默认;

【问题讨论】:

异常消息不是编译错误。另外,这个异常发生在哪一行? java.lang.NumberFormatException: For input string: "" 告诉你,你试图将字符串转换为数字,而字符串是"",所以当然不能转换为数字。 @rgettman 它发生在 else 语句之后 【参考方案1】:

如果这是您编写的所有代码,则编译错误在enter();default;

我认为你必须在你的类中实现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:
            
        
    
  

【讨论】:

以上是关于为啥我的代码在 else 语句后出现编译错误?的主要内容,如果未能解决你的问题,请参考以下文章

当我的代码超出函数范围时,为啥会出现编译器错误“未命名类型”?

为啥使用“if-else”语句会在看似相同的三元运算符构造不会产生 TypeScript 编译器错误?

为啥 Java 会出现“无法访问的语句”编译器错误?

有人可以解释为啥我的最后一个 else 语句不会运行吗?

为啥我的代码中出现分段错误(核心转储)错误?

只想编译 if-else 中的 if 语句