【问题标题】:Error: 'else' without 'if'错误:没有 \'if\' 的 \'else\'
【发布时间】:2022-11-14 06:51:25
【问题描述】:

得到一个没有 if 语句的 else:

import java.util.Scanner;

public class LazyDaysCamp
{
    public static void main (String[] args)
    {
        int temp;
        Scanner scan = new Scanner(System.in);

        System.out.println ("What's the current temperature?");
        temp = scan.nextInt();
        if (temp > 95 || temp < 20);
            System.out.println ("Visit our shops");
            else if (temp <= 95)
                if (temp >= 80)
                System.out.println ("Swimming");
                else if (temp >=60) 
                    if (temp <= 80)
                    System.out.println ("Tennis");
                    else if (temp >= 40)
                        if (temp < 60)
                        System.out.println ("Golf");
                        else if (temp < 40)
                            if (temp >= 20)
                            System.out.println ("Skiing");                                                                                                                                                                                                                                                                   
    }
}

如果这就是它看起来像这样的原因,我需要使用级联。另外,如果我正确地进行了级联,你能告诉我吗?如果我只是尽力了解级联的含义,我就无法找到级联的好例子。

LazyDaysCamp.java:14: error: 'else' without 'if'
            else if (temp <= 95)
            ^
1 error

这就是我得到的错误

【问题讨论】:

  • 请使用大括号。它对解决这类问题有很大帮助。
  • 您还有一些多余的 if 语句。至少可以删除 4 个 if 语句而不会影响程序逻辑,因为它们只是测试已知信息。
  • @Ted Hopp,大括号对解决这类问题没有帮助。几乎在任何地方都可以使用代码块 ({})。无需在代码块之前声明循环或条件。事实上,有时它们只是用来专门限制某些局部变量的范围很有趣。但是,以自动方式格式化代码的适当 IDE 将有助于解决此问题。

标签: java if-statement scope curly-braces


【解决方案1】:

删除此行末尾的分号:

if (temp > 95 || temp < 20);

请使用大括号! Java 是不是像 Python 一样,缩进代码会创建一个新的块作用域。最好安全地使用它并始终使用大括号 - 至少在您获得更多的语言经验并准确理解之前什么时候你可以省略它们。

【讨论】:

  • 分号也抓住了我。
【解决方案2】:

问题是第一个 if if (temp &gt; 95 || temp &lt; 20); 与使用普通缩进相同

if (temp > 95 || temp < 20)
{
}

也就是说,如果温度不在 20 到 95 之间,则执行一个空块。没有别的了。

下一行 else 然后没有与之对应的 if ,因此会产生您的错误

处理这个问题的最好方法是始终使用大括号来显示 if 之后执行的内容。这并不意味着编译器会捕获错误,但首先您更有可能通过查看缩进看到任何问题,并且错误可能看起来更具可读性。但是,您可以使用 eclipse、checkstyle 或 FindBugs 等工具来告诉您是否未使用 {} 或使用空块。

更好的方法可能是在重新测试时整理逻辑

if (temp > 95 || temp  < 20)  
{
  System.out.println ("Visit our shops");
} else if (temp >= 80)
{
    System.out.println ("Swimming");
} else if (temp >=60)
{ 
   System.out.println ("Tennis");
} else if (temp >= 40)
{
     System.out.println ("Golf");
} else if (temp >= 20)
{
   System.out.println ("Skiing");                                                                                                                                                                                                                                                                   
}

【讨论】:

    【解决方案3】:

    我将为您重新格式化。如果你使用大括号,你永远不会遇到这个问题。

    public class LazyDaysCamp
    {
        public static void main (String[] args)
        {
            int temp;
            Scanner scan = new Scanner(System.in);
    
            System.out.println ("What's the current temperature?");
            temp = scan.nextInt();
            if (temp > 95 || temp < 20) //<-- I removed the semicolon that caused the error
            {
                System.out.println ("Visit our shops");
            }
            else if (temp <= 95)
            {
                if (temp >= 80)
                {
                    System.out.println ("Swimming");
                }
                else if (temp >=60)
                {
                    if (temp <= 80)
                    {
                        System.out.println ("Tennis");
                    }
                    else if (temp >= 40)
                    {
                        if (temp < 60)
                        {
                            System.out.println ("Golf");
                        }
                        else if (temp < 40)
                        {
                            if (temp >= 20)
                            {
                                System.out.println ("Skiing");
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      出现此错误是因为您在 if 语句后输入了分号。 删除第 12 行第一个 if 语句末尾的分号。

          if (temp > 95 || temp < 20);
      

      【讨论】:

        【解决方案5】:

        如果(温度 > 95 || 温度 < 20); 删除这个分号

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-27
        相关资源
        最近更新 更多