【问题标题】:If,Else statement skipping the second conditional [duplicate]If,Else 语句跳过第二个条件[重复]
【发布时间】:2015-12-24 11:48:13
【问题描述】:

我正在编写一个代码,根据你的年龄和性别,它会显示你是否可以入伍,但是由于某种原因,在阅读了第一条语句后,它会在不评估第二个条件的情况下跳转到结果。这是代码:

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");
    }
}

【问题讨论】:

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


【解决方案1】:

Age = input.nextInt();

之后添加这一行
input.nextLine();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多