gpt4 book ai didi

Java 不允许我使用多个 else 语句

转载 作者:行者123 更新时间:2023-12-01 21:34:22 24 4
gpt4 key购买 nike

我尝试让它看起来与我在网上找到的其他代码片段相同。我试过嵌套它。我已经做了大部分我认为可以解决问题的事情。我是java新手。有人可以帮忙吗?为什么它不允许我使用另外两个?

导入java.util.Scanner;公共(public)课 FirstProgram {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner user_input = new Scanner(System.in);

System.out.print("Enter a number>>>");
String Number1 = user_input.next();

System.out.print("Enter another number>>>");
String Number2 = user_input.next();
{
if (Number1.equals(Number2))
System.out.println("These numbers are equal");

else if (Number1.compareTo(Number2) < 0);
System.out.println(Number1 + " Is less than " + Number2);

else if (Number1.compareTo(Number2) > 0);
System.out.println(Number1 + " Is greater than " + Number2);

}

}

}

最佳答案

您需要从带有 else 的行中删除分号

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner user_input = new Scanner(System.in);

System.out.print("Enter a number>>>");
String Number1 = user_input.next();

System.out.print("Enter another number>>>");
String Number2 = user_input.next();
{
if (Number1.equals(Number2))
System.out.println("These numbers are equal");
else if (Number1.compareTo(Number2) < 0) // removed semicolon
System.out.println(Number1 + " Is less than " + Number2);
else if (Number1.compareTo(Number2) > 0) //removed semicolon
System.out.println(Number1 + " Is greater than " + Number2);
}

}

我发现使用大括号是一种很好的做法,可以帮助避免此类错误。

关于Java 不允许我使用多个 else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com