如何修复程序中的编译错误?

时间:2014-06-10 21:16:02

标签: java

我正在为学校编写一个Java程序来解决下面的问题。每次我尝试编译程序时,它都会给我编译错误。如果有人能帮我弄清问题是什么,我将不胜感激。

问题:一个非政府组织需要一个计划来计算贫困家庭的经济援助金额。公式如下:

- 如果家庭年收入在30,000美元至40,000美元之间且家庭中至少有三个孩子,则每名儿童的收入为1,000美元。

- 如果家庭年收入在20,000美元至30,000美元之间且家庭中至少有两个孩子,则每名儿童的收入为1,500美元。

- 如果家庭年收入低于20,000美元,则每名儿童的收入为2,000美元。

实现此计算的方法。编写一个程序,询问每个申请人的家庭收入和子女数量,打印您的方法返回的金额。使用-1作为输入的标记值。

我的节目:

    import java.util.Scanner;

    /**
       This program computes the financial assistance that a family 
       can receive based on their income and the number of children.
*/

public class FinancialAidCalc
{
   public static void main(String[] args)
   {

   /** This declares the variables 
          for the main method.
      */

      int income = 0;
      int children = 0;
      int financialAid = 0;


      /** This creates the method to request 
      the household income amount.
      @param income the amount of houshold income 
         that the user inputs
      @return the household income
      */

      public static int householdIncome(int income)
      {
         do
         {
         Scanner in = new Scanner(System.in);
         System.out.print("Please enter the total household income.  Enter -1 to quit.");
         int income = in.nextInt();
         }
         while (income < -1);

         if (income >= 0) {return income;}
         if (income = -1) {return;}            
         }
      }



      /** This creates the method to request 
      the number of children in the household.
      @param children the number of children 
         in the houshold that the user inputs
      @return the number of children
      */

      public static int noOfHouseholdChildren(int children)
      {
         do
         {
         Scanner in = new Scanner(System.in);
         System.out.print("Please enter the total number of children in the household.  Enter -1 to quit.");
         int children = in.nextInt();
         }
         while (children < -1);

         if (children >= 0) {return children;}
         if (children = -1) {return;}

      }   


      /** This method uses the returned results from the householdIncome and noOfHouseholdChildren
          methods in order to calculate the amount of financial assistance
          that a family will receive.
          @param financialAid shows the amount of financial aid that the family is eligible for

      */

      public static int availableFinancialAid(int income, int children)
      {
         /** This determines if the household income is between 
             $30,000 and $40,000 with at least 3 children.
         */

         if (income.isBetween(30000, 40000) && children >= 3)  
         {
            financialAid = (children * 1000);
            System.out.println("Your household income is: $" + income);
            System.out.println("You have " + children + " in your household.");
            System.out.println("Your household is eligible for $" + financialAid + "in financial aid.");
         }

         /** This determines if the household income is between 
             $20,000 and $30,000 with at least 2 children.
         */

         if (income.isBetween(20000, 30000) && children >= 2)  
         {
            financialAid = (children * 1500);
            System.out.println("Your household income is: $" + income);
            System.out.println("You have " + children + " in your household.");
            System.out.println("Your household is eligible for $" + financialAid + "in financial aid.");
         }

         /** This determines if the household income is less than $20,000..
         */

         if (income < 20000)   
         {
            financialAid = (children * 2000);
            System.out.println("Your household income is: $" + income);
            System.out.println("You have " + children + " in your household.");
            System.out.println("Your household is eligible for $" + financialAid + "in financial aid.");
         }

         /** This advises the user that they do not qualify for 
         financial aid if the income is over $40,000.
         */

         if (income > 40000) 
         {
            System.out.print("You do not qualify for financial aid.");
         }
      }
   }

2 个答案:

答案 0 :(得分:4)

您正在尝试在方法中声明方法。仔细看看主要方法。您没有关闭该方法,但您正在声明其他方法。

好像你打算创建一个类,然后通过main方法使用它。在这种情况下,你想要这样的东西:

import java.util.Scanner;

public class FinancialAidCalc {
    public static void main(String[] args) {
        /* code */
    }

    static int income = 0;
    static int children = 0;
    static int financialAid = 0;

    public static int householdIncome(int income) {
        /* code */
    }

    public static int noOfHouseholdChildren(int children) {
        /* code */
    } 

    public static int availableFinancialAid(int income, int children) {
       /* code */
    }
}

您的代码中可能有其他编译错误,但由于混乱,我没有仔细观察。

答案 1 :(得分:2)

我看到这样的行

  

if(income = -1)

我想知道你是否真的这么说。您可以使用Java在条件语句中进行分配,但您更有可能这样做:

  

if(income == -1)

单一等号是赋值,而双等号是等号测试。