2013-11-18 4 views
-1

Я написал следующий код, однако попытался выяснить, как его переписать, включив параметры и логические значения, если это возможно, когда основной метод запрашивает ввод пользователя, а затем вызывает методы для получения результата:Внедрение того же кода разными способами

import java.util.*; 

class Congress 
{ 
    public static void main (String [] args) 
    { 
      int age, citizen, i, r; 
      citizen = 0; 
      i = 0; 
      r = 0; 
      age = -1; 
      Scanner keyboard = new Scanner(System.in); 
      System.out.println ("CONGRESS ELIGIBILITY"); 
      for (age = -1; age < 0; r++) 
      { 
        System.out.println(); 
        System.out.print ("Enter age of candidate: "); 
        age = keyboard.nextInt(); 
        System.out.print ("Enter years of US citizenship: "); 
        citizen = keyboard.nextInt(); 
        if (age <= 0) 
        { 
         System.out.print ("Please enter a proper age. "); 
        } 
      } 
      if (age >= 25 && citizen >= 7) { 
        i++; 
      } 
      if (age >= 30 && citizen >= 9) { 
        i++; 
      } 
      if (i == 0) { 
      System.out.println ("The candidate is not eligible for election to either the House of Representatives or the Senate."); 
      } 
      if (i == 1) { 
        System.out.println ("The candidate is eligible for election to the House of Representatives but is not eligible for election to the Senate."); 
      } 
      if (i == 2) { 
        System.out.println ("The candidate is eligible for election to both the House of Representatives and the Senate."); 
      } 
    } 

}

ответ

2

/* * графы - Congress.java, 18 ноября 2013 5:38:15 PM * / импорт java.util.;

/** * Конгресс-класс. * * @author Rajakrishna В. Редди * @version 1,0 * */ класса Конгресс {

/** 
* Eligible for senate. 
* 
* @param age 
*   the age 
* @param lengthOfCitizenship 
*   the length of citizenship 
* @return true, if successful 
*/ 
public static boolean eligibleForSenate(int age, int lengthOfCitizenship) 
{ 
    return age >= 30 && lengthOfCitizenship >= 9; 
} 

/** 
* Eligible for house. 
* 
* @param age 
*   the age 
* @param lengthOfCitizenship 
*   the length of citizenship 
* @return true, if successful 
*/ 
public static boolean eligibleForHouse(int age, int lengthOfCitizenship) 
{ 
    return age >= 25 && lengthOfCitizenship >= 7; 
} 

/** 
* The main method. 
* 
* @param args 
*   the arguments 
*/ 
public static void main(String[] args) 
{ 
    int age, citizen, i, r; 
    citizen = 0; 
    i = 0; 
    r = 0; 
    age = -1; 
    final Scanner keyboard = new Scanner(System.in); 
    System.out.println("CONGRESS ELIGIBILITY"); 
    for (age = -1; age < 0; r++) 
    { 
     System.out.println(); 
     System.out.print("Enter age of candidate: "); 
     age = keyboard.nextInt(); 
     System.out.print("Enter years of US citizenship: "); 
     citizen = keyboard.nextInt(); 
     if (age <= 0) 
     { 
      System.out.print("Please enter a proper age. "); 
     } 
    } 
    try 
    { 
     keyboard.close(); 
    } 
    finally 
    { 

    } 
    final boolean eligibleForHouse = eligibleForHouse(age, citizen); 
    final boolean eligibleForSenate = eligibleForSenate(age, citizen); 
    if (eligibleForHouse && !eligibleForSenate) 
    { 
     System.out.println("The candidate is eligible for election to the House of Representatives but is not eligible for election to the Senate."); 
    } 
    if (!eligibleForHouse && !eligibleForSenate) 
    { 
     System.out.println("The candidate is not eligible for election to either the House of Representatives or the Senate."); 
    } 
    if (eligibleForHouse && eligibleForSenate) 
    { 
     System.out.println("The candidate is eligible for election to both the House of Representatives and the Senate."); 
    } 
} 

}

Смежные вопросы