我是编程新手,非常感谢您对这个问题的帮助:一个计算经济援助金额的程序。如果家庭年收入在 30000 美元至 40000 美元之间,并且家庭至少有 3 个 child ,则每个 child 的金额为 1000 美元。如果家庭年收入在 20000 美元至 30000 美元之间,并且家庭至少有 3 个 child ,则每个 child 的金额为 1500 美元。如果家庭年收入低于 20000 美元,则每个 child 的金额为 2000 美元。我必须实现一个方法并使用 -1 作为哨兵值。这是我到目前为止的程序
public static void main(String[] args) {
// **METHOD** //
Scanner in = new Scanner(System.in);
System.out.println("What is the annual household income?");
double income = in.nextDouble();
System.out.println("How many children are in the household?");
int children = in.nextInt();
System.out.println("The financial assistance for this household is: "
+ assistance(income, children));
}
//**TEST PROGRAM**//
public static double assistance(double income, int children)
{
if(income >= 30000 && income < 40000 && children >= 3)
{
assistance = children * 1000; //says that it cannot find the symbol
}
else if(income >= 20000 && income < 30000 && children >= 2)
{
assistance = children * 1500;
}
else if(income < 20000)
{
assistance = children * 2000;
}
else
{
assistance = 0.0;
}
return assistance;
}
}
最佳答案
您可能正在考虑另一种语言,例如 Basic,您可以在其中声明一个函数并使用函数名称返回该函数的值。
Java 只是让你返回值。
其他人已要求您声明援助
。请考虑一个不同名称的变量,ans
。最后,您将返回 ans
。
public static double assistance(double income, int children)
{
double ans = 0.0;
if(income >= 30000 && income < 40000 && children >= 3)
{
ans = children * 1000; //says that it cannot find the symbol
}
else if(income >= 20000 && income < 30000 && children >= 2)
{
ans = children * 1500;
}
else if(income < 20000)
{
ans = children * 2000;
}
return ans;
}
}
关于java - 经济援助计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22173765/