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

时间:2013-11-18 11:50:35

标签: return-type

我编写了下面的代码,但试图弄清楚如何通过包含参数和布尔值来重写它,如果可能的话,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.");
            }
    }

}

1 个答案:

答案 0 :(得分:3)

/ *  *图表 - Congress.java,2013年11月18日下午5:38:15  *   / import java.util。;

/ **  *班级大会。  *  * @author 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.");
    }
}

}