2015-09-26 75 views
0

即時編寫代碼,根據您的年齡和性別,它會顯示,如果你可以參加軍隊,但由於某種原因,在閱讀第一個聲明後,它已經跳轉到結果沒有評估第二個條件。這裏是代碼:If Else語句跳過第二個條件

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

見http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods –

回答

1

添加此行後年齡= input.nextInt();

input.nextLine();