Java不会让我使用多个else语句

时间:2016-05-07 05:09:16

标签: java

我试过让它看起来与我在网上发现的其他代码相同。 我试过嵌套它。 我做了大部分我认为会解决它的问题。 我是java的新手。有人可以帮忙吗?为什么不让我使用两个缺点?

import java.util.Scanner; 公共课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);

        }

}

}

1 个答案:

答案 0 :(得分:3)

您需要使用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);
}

}

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