2014-03-04 92 views
1

我是編程新手,希望對這個問題有所幫助:計算財務援助金額的計劃。如果家庭年收入在30000美元至40000美元之間,家庭至少有3個孩子,則每個孩子的收入爲1000美元。如果家庭年收入在20000美元至30000美元之間,家庭至少有3個孩子,則每個孩子的收入爲1500美元。如果家庭年收入低於20000美元,則每個孩子的收入爲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; 
} 
} 
+0

你在哪裏申報援助? –

+0

我可以在哪裏申報援助?它會在if語句之前嗎? – DragonAge99

+2

@ DragonAge99你有沒有爲方法和變量使用相同的名字? – amudhan3093

回答

0

您需要在function public static double assistance

public static double assistance(double income, int children) 
{ 
    double assistance = 0.0; 

    "Rest of your code here" 
} 
+0

好的,感謝您的幫助 – DragonAge99

+0

這對您是否成功? –

0

首先,我要的開始申報assistance假設你有你的一類,因爲上市代碼在Java中,一切都是一個類。截至目前,您尚未定義可變協助。所以你我建議驗證你的輸入,以便你可以處理用戶不輸入數字值的情況。另一個想法是幫助一個對象,因爲它不一定是靜態的。然後,您可以使用該對象來調用輔助方法。就像這樣:

import java.util.Scanner; 

public class Main { 
    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: " 
      + new Assistance.getAssistance(income,children)); 
} 

} 

public class Assistance { 
    public double getAssistance(double income, int children) { 

    /* Declare assistance here */ 
    double assistance; 
    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; 
} 
} 
1

你也許會想其他語言,如基本的,在這裏你聲明一個函數,並使用函數名返回函數的值。

Java只是你返回的價值。其他人請您聲明assistance。請考慮一個不同名稱的變量,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; 
} 
}