修复 Java 中错误 Error: Else Without if
本篇文章我们将了解在用 Java 编写代码时只说 'else' without 'if'
的错误。 我们还将找出导致此错误的可能原因并找到解决方案。
Java 中 error: 'else' without 'if'
通常,Java 编程新手都会遇到这种错误。 在探讨此错误的原因和解决方案之前,让我们编写一个产生此错误的程序并理解它。
所以,假设我们是 Python 专家和 Java 初学者。 因此,我们将编写包含 if-else 的 Java 程序,如下所示。
示例代码:
//import libraries
import java.util.Scanner;
//decide future activity based on the current temperature
public class Test{
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 ("Sking"); }//end main()
}//end Test Class
错误如下:
在这个程序中,我们从用户那里获取当前温度,并根据当前温度决定我们未来的活动。 上图显示我们收到了有关 NetBeans IDE 在编译时通知的逻辑错误。
因此,在解决错误之前我们甚至无法执行代码。 为此,我们必须了解以下可能的原因。
error: 'else' without 'if' 原因
该错误本身是解释性的,它表示 Java 编译器找不到与 else 语句关联的 if 语句。 请记住,除非与 if 语句关联,否则 else 语句不会执行。
因此,可能的原因如下。
- 第一个原因是我们忘记将 if 块写在 else 块之前。
- if 条件的右括号缺失。
- 我们使用分号结束 if 语句。
如何解决这个错误? 我们来看看下面的部分。
修复 Java 中 'else' without 'if' 的错误
示例代码:
//import libraries
import java.util.Scanner;
public class Test {
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");
}//end if
else if (temp <= 95) {
if (temp >= 80) {
System.out.println("Swimming");
} //end if
else if (temp >= 60) {
if (temp <= 80) {
System.out.println("Tennis");
}//end if
else if (temp >= 40) {
if (temp < 60) {
System.out.println("Golf");
}//end if
else if (temp < 40) {
if (temp >= 20) {
System.out.println("Sking");
}//end if
}//end else-if
}//end else-if
}//end else-if
}//end else-if
}//end main()
}//end Test Class
输出:
What's the current temperature?
96
Visit our shops
我们从 if 语句末尾删除了分号 (;
),并为每个块放置了 {}
,以修复只说 'else' without 'if'
的错误。
最好使用大括号 {}
,直到我们足够专业并且知道在哪里可以省略它们(当块中只有一个语句时我们可以省略它们)。
相关文章
Java 错误 Java.Net.SocketException: Network Is Unreachable
发布时间:2023/07/16 浏览次数:963 分类:Java
-
今天我们就来讨论一下Java编程时出现java.net.SocketException: Network is unreachable异常的可能原因及解决方法。Java中出现java.net.SocketException: Network is unreachable的可能原因及解决方案
Java 错误 Java.Net.ConnectException: Connection Timed Out
发布时间:2023/07/16 浏览次数:235 分类:Java
-
本篇文章将重点介绍如何使用此包进行基本的网络调用以及可能面临和解决的错误。在 Java 中使用 java.net 进行网络调用 进行网络调用是 Java 开发人员每天面临的最重要的事情之一。
Java 中错误 Attempt to Invoke Virtual Method on a Null Object Reference
发布时间:2023/07/16 浏览次数:948 分类:Java
-
本篇文章介绍如何解决 Java 中的 Attempt to invoke virtual method on a null object reference 错误。Java 中 Attempt to invoke virtual method on a null object reference 错误
Java 错误 Java.Security.InvalidKeyException: Illegal Key Size
发布时间:2023/07/15 浏览次数:644 分类:Java
-
本篇文章介绍包含 java.security.InvalidKeyException: Illegal key size 的 Java 代码。 然后,我们将了解其可能的原因。最后,它通过消除指定的错误来引导我们找到解决方案。
Java 错误 Java.SQL.SQLException: Access Denied for User Root@Localhost
发布时间:2023/07/15 浏览次数:165 分类:Java
-
本篇文章介绍如何解决 Java 中的 java.sql.SQLException: Access Denied for user 'root'@'localhost' 错误。修复 Java 中的 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
Java 错误 Gateway Process Exited Before Sending Its Port Number
发布时间:2023/07/15 浏览次数:885 分类:Java
-
本篇文章介绍了 Java 中 Java gateway process exited before sending the driver its port number 错误 Java gateway process exited before sending the driver its port number 错误
修复 Java 中 Java.Net.BindException: Address Already in Use: Bind 错误
发布时间:2023/07/15 浏览次数:250 分类:Java
-
本篇文章介绍了 Java 中的 java.net.BindException:Address already in use: Bind 错误。修复Java 中的 java.net.BindException:Address already in use: Bind
Java 中错误 Unsupported Major Minor Version
发布时间:2023/07/14 浏览次数:133 分类:Java
-
出现 Unsupported Major.minor version 错误或 Java.lang.UnsupportedClassVersionError 的原因是运行时 JDK 较低,编译时 JDK 较高。 本篇文章介绍如何解决Java中不支持的major.minor版本。
Java 错误 Error:Java: Javactask: Source Release 1.8 Requires Target Release 1.8
发布时间:2023/07/14 浏览次数:274 分类:Java
-
在使用IntelliJ for Java时,无法编译Java程序是一个常见的问题。 本教程提供了此错误的解决方案。Error:Java: Javactask: Source Release 1.8 Requires Target Release 1.8 错误