Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If-else语句不适用于JAVA_Java_If Statement_Ampersand - Fatal编程技术网

If-else语句不适用于JAVA

If-else语句不适用于JAVA,java,if-statement,ampersand,Java,If Statement,Ampersand,if语句开头的行导致了问题。当我运行它时,它会问我的性别,我说男孩,它说“你不能进入”。它甚至没有问年龄! 导入java.util.Scanner class Apples { //////Main Method public static void main (String[] args) { if (Apples.getGender() == "boy" && Apples.getAge() > 17) { System.out

if语句开头的行导致了问题。当我运行它时,它会问我的性别,我说男孩,它说“你不能进入”。它甚至没有问年龄! 导入java.util.Scanner

class Apples { 

//////Main Method
    public static void main (String[] args) { 

    if (Apples.getGender() == "boy" && Apples.getAge() > 17) { 
        System.out.printf("You can enter");
    }
    else { 
        System.out.printf("You cannot enter");
    }
}

/////Two Methods        
        public static String getGender() { 
        Scanner newGender = new Scanner(System.in);
        System.out.println("What gender are you?"); 
            String genderMF = newGender.nextLine();
            return genderMF;

        }

    public static int getAge() { 
        Scanner newAge = new Scanner(System.in);
        System.out.println("What age are you?");    
        int ageMF = newAge.nextInt();
        return ageMF;

    }
}   

您必须将字符串与Java中的
.equals
函数进行比较,而不是与==

s1.equals(s2);

在Java中,使用
equals(…)
not
=
比较字符串
s,例如:
Apples.getGender().equals(“boy”)