2016-09-21 1 views

답변

0

그냥 허용 연령에 해당하는 if 지점 내에서의 성별을 요청 코드 이동 : 로직이 더 복잡를 얻을 수

public static void main (String[] args) { 

    final int DRINKING_AGE = 21; 
    final int ADULT = 18; 
    final String LEGAL_DRINKING_AGE_MESSAGE = "You are legally able to drink"; 

    int age = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Age")); 

    boolean ageCheck = age >0 && age <=100; 
    //check to see if the age is within range 
    if (ageCheck) { 
    String gender = JOptionPane.showInputDialog("Enter Your Gender (F/M)"); 
    boolean genderCheck = gender.equalsIgnoreCase("M") || gender.equalsIgnoreCase("F"); 
    //check to see if the user entered M or F 
    if (genderCheck) { 
      //if we pass both checks, then do the rest of the logic 
      JOptionPane.showMessageDialog(null,"OUTPUT: Your age is " + age + " and Gender is " + gender); 

    } else { 
     //Write an error for invalid Gender 
     JOptionPane.showMessageDialog(null,"Error: Gender must be F or M") ; 
    } 

    } else { 
    //Write an error for invalid Age 
    JOptionPane.showMessageDialog(null,"Error: Age must be between 0 -100") ; 

    } 
} 

경우, 나는를 분리 리팩토링을 제안을 연령과 성별을 별도의 방법으로 얻는 논리. (나는이 코드를 위해서조차도 하겠지만, 여러분이 물어 본 것의 범위를 벗어납니다.)

관련 문제