gpt4 book ai didi

java - If,Else 语句跳过第二个条件

转载 作者:行者123 更新时间:2023-12-01 11:07:25 26 4
gpt4 key购买 nike

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

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

最佳答案

Age = input.nextInt();之后添加此行

input.nextLine();

关于java - If,Else 语句跳过第二个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32802605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com