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

     2023-02-19     74

关键词:

【中文标题】为啥我的代码在 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:
            
        
    
  

【讨论】:

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

】为啥Java会出现“无法访问的语句”编译器错误?【英文标题】:WhydoesJavahavean"unreachablestatement"compilererror?为什么Java会出现“无法访问的语句”编译器错误?【发布时间】:2011-04-1706:31:31【问题描述】:在调试程序时,... 查看详情

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

】为啥使用“if-else”语句会在看似相同的三元运算符构造不会产生TypeScript编译器错误?【英文标题】:Whydoesusingan\'if-else\'statementproduceaTypeScriptcompilererrorwhenaseeminglyidenticalternaryoperatorconstructdoesnot?为什么使用“if-else”语句会在看... 查看详情

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

】当我的代码超出函数范围时,为啥会出现编译器错误“未命名类型”?【英文标题】:WhydoIgetthecompilererror"doesnotnameatype"whenmycodeisoutsideofafunctionscope?当我的代码超出函数范围时,为什么会出现编译器错误“未命名类型”... 查看详情

为啥我的 ELSE 语句被跳过?

】为啥我的ELSE语句被跳过?【英文标题】:WhyismyELSEstatementbeingskipped?为什么我的ELSE语句被跳过?【发布时间】:2012-08-3019:49:04【问题描述】:我一直在尝试使用IF/ELSE语句来查询我的MySQL数据库,但似乎无法弄清楚为什么ELSE语句... 查看详情

为啥编译器告诉我没有运算符与我的“if”和“else if”语句中的操作数类型匹配?

】为啥编译器告诉我没有运算符与我的“if”和“elseif”语句中的操作数类型匹配?【英文标题】:Whydoesthecompilertellmethatnooperatorsmatchtheoperandtypeinmy\'if\'and\'elseif\'statements?为什么编译器告诉我没有运算符与我的“if”和“elseif”语... 查看详情

为啥我在 Java 中出现无法访问的语句错误?

】为啥我在Java中出现无法访问的语句错误?【英文标题】:WhydoIgetunreachablestatementerrorinJava?为什么我在Java中出现无法访问的语句错误?【发布时间】:2014-10-0202:23:50【问题描述】:我正在整理我在在线教程中找到的冰雹序列的... 查看详情

为啥启用浮点异常后出现多个陷阱错误

】为啥启用浮点异常后出现多个陷阱错误【英文标题】:WhyafterenablingfloatingpointexceptionsIgotmultipletrapserror为什么启用浮点异常后出现多个陷阱错误【发布时间】:2015-11-2714:37:52【问题描述】:使用MSVC2015专业版环境Windows10,使用/EH... 查看详情

为啥sql在引发错误后执行语句?

】为啥sql在引发错误后执行语句?【英文标题】:Whysqlexecutesthestatementsafterraisingtheerror?为什么sql在引发错误后执行语句?【发布时间】:2019-04-1219:56:30【问题描述】:我已经写了这段代码。问题是,在引发错误之后,它仍然会在... 查看详情

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

】为啥我的代码中出现分段错误(核心转储)错误?【英文标题】:WhyamIgettingsegmentationfault(coredumped)errorinmycode?为什么我的代码中出现分段错误(核心转储)错误?【发布时间】:2016-04-0305:08:31【问题描述】:此代码用于使用邻... 查看详情

为啥我的 React 应用程序会出现这个错误?

】为啥我的React应用程序会出现这个错误?【英文标题】:WhyamigettingthiserrorinmyReactapplication?为什么我的React应用程序会出现这个错误?【发布时间】:2021-11-0809:55:27【问题描述】:我在编译我的react应用程序时收到此错误:TypeErro... 查看详情

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

】有人可以解释为啥我的最后一个else语句不会运行吗?【英文标题】:Couldsomeoneexplainwhymylastelsestatementwillnotrun?有人可以解释为什么我的最后一个else语句不会运行吗?【发布时间】:2020-11-1902:40:03【问题描述】:我正在尝试将我... 查看详情

为啥我的代码在 Windows 7 上不会出现段错误?

】为啥我的代码在Windows7上不会出现段错误?【英文标题】:Whywon\'tmycodesegfaultonWindows7?为什么我的代码在Windows7上不会出现段错误?【发布时间】:2011-06-0511:58:38【问题描述】:这是一个不寻常的问题,但这里是:在我的代码中... 查看详情

为啥在第一次 Fetch 后出现字段计数不匹配错误?

】为啥在第一次Fetch后出现字段计数不匹配错误?【英文标题】:WhyamIgettingaFieldCountMismatcherrorafterthefirstFetch?为什么在第一次Fetch后出现字段计数不匹配错误?【发布时间】:2021-08-1317:25:40【问题描述】:我已经尝试了所有我能想... 查看详情

为啥我的代码出现类型错误?

】为啥我的代码出现类型错误?【英文标题】:WhyamIgettingaTypeErroronmycode?为什么我的代码出现类型错误?【发布时间】:2021-10-1802:57:34【问题描述】:当我的代码在我的代码中达到“if(entered_scoredefcalc_average(score_1,score_2,score_3,score... 查看详情

为啥我的 python 代码出现内部服务器错误?

】为啥我的python代码出现内部服务器错误?【英文标题】:WhyamIgettinginternalservererrorwithmypythoncode?为什么我的python代码出现内部服务器错误?【发布时间】:2021-09-1720:40:32【问题描述】:我正在试验和开发一个Flask网页,它将使用... 查看详情

添加另一个方法后,dispatch()和if-else语句出现问题(代码片段)

...以获取存储在Firebase中的特定位置网址的详细信息。原始代码(版本1)没有问题,我调度authGetToken(),代码识别存储在redux中的令牌(字符串),然后使用它来获取存储的数据。版本1returndispatch=>dispatch(authGetToken()).then(token=&g... 查看详情

为啥在编译我的代码C(linux)时出现分段错误(核心转储)[关闭]

】为啥在编译我的代码C(linux)时出现分段错误(核心转储)[关闭]【英文标题】:whygetIsegmentationfault(coredumped)oncompilingmycodeC(linux)[closed]为什么在编译我的代码C(linux)时出现分段错误(核心转储)[关闭]【发布时间】:2014-03-181... 查看详情

C++ 唯一指针;为啥这个示例代码会出现编译错误?错误代码太长了,我无法指定

】C++唯一指针;为啥这个示例代码会出现编译错误?错误代码太长了,我无法指定【英文标题】:C++unique_ptr;Whythissamplecodesgetcompileerror??errorcodesaresolongthatIcan\'tspecifyitC++唯一指针;为什么这个示例代码会出现编译错误?错误代码... 查看详情