Java 破例

Java 破例,java,if-statement,exception,Java,If Statement,Exception,我不知道为什么这个例外不起作用 import java.util.*; public class a { public static void main(String[] args) { Scanner a = new Scanner(System.in); int x; x=a.nextInt(); if( x < 5) { System.out.print("This is if sta

我不知道为什么这个例外不起作用

import java.util.*;

public class a {
    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        int x; 
        x=a.nextInt();
        if( x < 5) {
            System.out.print("This is if statement");
        }else if(x<3){
            System.out.print("This is else if statement");
        }else{
           System.out.print("This is else statement");
        }
        a.close();
    }
}
import java.util.*;
公共a类{
公共静态void main(字符串[]args){
扫描仪a=新的扫描仪(System.in);
int x;
x=a.nextInt();
if(x<5){
系统输出打印(“这是if语句”);

}else if(x我相信你指的是
条件
而不是
异常
,但是,你没有得到预期结果的原因是因为
if语句的逻辑和优先级错误

if( x < 5) { // any input less than 5 will execute this if statement and will NOT proceed to the next blocks
     System.out.print("This is if statement");
}else if(x<3){ // this is not reachable because the first if statement will catch all inputs less than 5 INCLUDING those less than 3
     System.out.print("This is else if statement");
}else{
     System.out.print("This is else statement");
}

什么异常?欢迎来到Stack Overflow!请看一看,并通读,特别是什么异常?如何不工作?您可能指的是
条件
不是
异常
,如果是,那是因为
if语句的优先级错误
我建议您的名字要多想象一点。您有一个类
a
和一个扫描器
a
。一组
x
元素,其中
x
if( x < 3) { // this should come first
     System.out.print("This is if statement");
}else if(x<5){
     System.out.print("This is else if statement");
}else{
     System.out.print("This is else statement");
}