Return type 用不同的方法重写相同的代码

Return type 用不同的方法重写相同的代码,return-type,Return Type,不过,我编写了以下代码,试图找出如何通过在main方法请求用户输入,然后调用这些方法以生成输出的情况下,尽可能包含参数和布尔值来重写它: import java.util.*; class Congress { public static void main (String [] args) { int age, citizen, i, r; citizen = 0; i = 0;

不过,我编写了以下代码,试图找出如何通过在main方法请求用户输入,然后调用这些方法以生成输出的情况下,尽可能包含参数和布尔值来重写它:

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

/* *graphs-Congress.java,2013年11月18日下午5:38:15 * / 导入java.util

/** *班级代表大会。 * *@作者Rajakrishna V.Reddy *@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.");
    }
}