如果,Else语句跳过第二个条件

时间:2015-09-26 22:19:28

标签: java if-statement return conditional-statements evaluation

我正在编写一个代码,根据你的年龄和性别,它会显示你是否可以参军,但是由于某些原因,在读完第一个语句后,它已经跳到结果而不评估第二个条件。这是代码:

package exercicios1;
import java.util.Scanner;

public class Idade
{
private int Age;
private String Gender;

public int getAge() 
{
    return Age;
}

public void setIdade(int age) 
{
    Age = age;
}

public String getGender()
{
    return Gender;
}

public void setGender(String G)
{
    Gender = G;
}

public void ageVerification()
{
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter your age: ");
    Age = input.nextInt();
    System.out.print("Please enter your gender(M/F) :");
    Gender = input.nextLine();

    if(18 <= Age & Age <= 27 & Gender.equals("m"))
    {
        System.out.printf("You are viable to be enlisted");
    }
    else
    {
        System.out.printf("You are not viable to be enlisted");
    }
}

1 个答案:

答案 0 :(得分:1)

年龄= input.nextInt();

之后添加此行
input.nextLine();