有人可以帮助我找到这个Java编程的错误

时间:2017-03-07 05:21:21

标签: java

public class Test {
    public static void main(String[] args) {
        int age =56 ;

     ( if age<= 20) ;
            switch (age) {
                case 11:
                    System.out.println("You Are 11 Year Old");
                    break;
                case 12:
                    System.out.println("You Are 12 Year Old");
                    break;
                case 13:
                    System.out.println("You Are 13 Year Old");
                    break;
                case 14:
                    System.out.println("You Are 14 Year Old");
                    break;
                case 15:
                    System.out.println("You Are 15 Year Old");
                    break;
                case 16:
                    System.out.println("You Are 16 Year Old");
                    break;
                case 17:
                    System.out.println("You Are 17 Year Old");
                    break;
                case 18:
                    System.out.println("You Are 18 Year Old");
                    break;
                case 19:
                    System.out.println("You Are 19 Year Old");
                    break;
                case 20:
                    System.out.println("You Are 20 Year Old");
                    break;

我收到一个无法访问的语句错误在Intellij中有人可以给我指导我为什么会收到此错误?试图重新调整代码,但似乎没有任何工作。谢谢

2 个答案:

答案 0 :(得分:1)

代码中不应编译的是 -

( if age<= 20) ;

可能更像是被认为是 -

if(age<= 20) {    
//proper parenthesis which should be marked by IntelliJ as well
// the semi-colon though is not incorrect but terminates the if without any block to execute in your case

请注意,您之前将age的值设置为56,因此if条件应评估为false不执行该块。

答案 1 :(得分:1)

固定代码: IF条件写得不正确。

package com.art;

public class Test {
    public static void main(String[] args) {
        int age = 56;
        if (age <= 20) {
            switch (age) {
            case 11:
                System.out.println("You Are 11 Year Old");
                break;
            case 12:
                System.out.println("You Are 12 Year Old");
                break;
            case 13:
                System.out.println("You Are 13 Year Old");
                break;
            case 14:
                System.out.println("You Are 14 Year Old");
                break;
            case 15:
                System.out.println("You Are 15 Year Old");
                break;
            case 16:
                System.out.println("You Are 16 Year Old");
                break;
            case 17:
                System.out.println("You Are 17 Year Old");
                break;
            case 18:
                System.out.println("You Are 18 Year Old");
                break;
            case 19:
                System.out.println("You Are 19 Year Old");
                break;
            case 20:
                System.out.println("You Are 20 Year Old");
                break;
            }
        } else {
            System.out.println("Age is more than 20");
        }
    }
}