0

我是一个java菜鸟。我在主要方法中不断出错。请帮忙!我认为我没有以正确的方式调用这些方法。其他一切都应该正常工作。

import java.util.Scanner;

public class Assignment5 {

    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        inputName(kbd);
        inputIncome(kbd);
        inputMarried(kbd);
        calculateThreshold();
        incomeBelowThreshold();
        incomeAboveThreshold();
        taxBelowThreshold();
        taxAboveThreshold();
        totalTaxes();
        displayResults();

    }

    public static String inputName (Scanner kbd) {
        System.out.print("What is your name?");
        String name = kbd.nextLine();
        return name;
    }

    public static double inputIncome (Scanner kbd) {
        System.out.print("What is your annual income?");
        double userIncome = kbd.nextDouble();
        return userIncome;
    }

    public static char inputMarried (Scanner kbd) {
        System.out.print("Are you married? (y for yes, n for no)");
        char married = kbd.next().charAt(0);
        return married;
    }

    public static double calculateThreshold (char married) {
        double incomeThreshold;
        if (married == 'y') {
            incomeThreshold = 80000;
        } else {
            incomeThreshold = 40000;
        } 
        return incomeThreshold;
    }

    public static double incomeBelowThreshold (double userIncome , double incomeThreshold) {
        double incomeBelowThreshold;
        if (userIncome <= incomeThreshold) {
        incomeBelowThreshold = incomeThreshold - userIncome;
        } else {
            incomeBelowThreshold = userIncome;
        }
        return incomeBelowThreshold;
    }

    public static double incomeAboveThreshold (double userIncome, double incomeThreshold) {
        double incomeAboveThreshold;
        if (userIncome >= incomeThreshold) {
            incomeAboveThreshold = incomeThreshold - userIncome;
        } else {
            incomeAboveThreshold = 0;
        }
        return incomeAboveThreshold;
    }

    public static double taxBelowThreshold (double incomeBelowThreshold) {
        double taxBelowThreshold;
        taxBelowThreshold = incomeBelowThreshold * .25;
        return taxBelowThreshold;
    }

    public static double taxAboveThreshold (double incomeAboveThreshold) {
        double taxAboveThreshold;
        taxAboveThreshold = incomeAboveThreshold *.35;
        return taxAboveThreshold;
    }

    public static double totalTaxes (double taxBelowThreshold, double taxAboveThreshold) {
        double totalTaxes;
        totalTaxes = taxBelowThreshold + taxAboveThreshold;
        return totalTaxes;
    }

    public static void displayResults (String Name, char married, double income, double totalTaxes) {
        System.out.print("Name:" + Name);
        String marriedStatus;
        if (married == 'y') {
            marriedStatus = "Married";
        } else {
            marriedStatus = "Single";
        }
        System.out.print("Marital Status:" + marriedStatus);
        System.out.printf("Income: %.2f" + income);
        System.out.printf("Taxes: %.2f" + totalTaxes);
    }
}
4

5 回答 5

3

看起来您的某些方法需要参数,而您没有提供它们。

例如

public static double calculateThreshold (char married){}

你不能调用这个方法calculateThreshold();

您需要为已婚calculateThreshold('y')传递char;

于 2013-10-09T00:40:40.760 回答
0

一个错误是您有需要参数的方法,但您没有传递参数。 calculateThreshold(),例如,接受一个参数,并incomeBelowThreshold()接受两个。您也没有从输入例程中返回值;这不是错误,但可能会被标记为警告(取决于您编辑和编译内容的方式),但您需要将输入例程中的值传递给其他例程,可能会转换它们预先。

于 2013-10-09T00:43:00.720 回答
0

谢谢你!!!!!!

calculate threshold(char) in the type Assignment 5 is not applicable for the arguments ()

将来,请始终尽可能多地发布有关实际错误的信息。不要让每个人都猜测:)

在这种情况下:

1)保存“已婚”的返回值:

   public static void main(String[] args) {
        ...
        char married = inputMarried(kbd);

2) 然后将其传递给“calculateThreshold()”。保存“threshold()”的返回值:

    ...
    double threshold = calculateThreshold (married);

3) 确保对程序的其余部分执行相同的操作。

于 2013-10-09T00:43:41.817 回答
0

实际上,您所有的方法都会返回一些答案,假设它应该传递给下一个方法左右......

您的错误是您没有将返回的值分配给变量..所以请更正它

于 2013-10-09T00:44:02.803 回答
0
public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        inputName(kbd);
        inputIncome(kbd);
        inputMarried(kbd);
        // modify this line calculateThreshold();
        calculateThreshold('a');
        // modify this line incomeBelowThreshold();
        incomeBelowThreshold(2.1, 2.2);
        // modify this line incomeAboveThreshold();
        incomeAboveThreshold(2.1, 2.2);
        // modify this line taxBelowThreshold();
        taxBelowThreshold(2.1);
        // modify this line taxAboveThreshold();
        taxAboveThreshold(2.1);
        // modify this line totalTaxes();
        totalTaxes(2.1, 2.1);
        // modify this line displayResults();
        displayResults("",'a', 2.1, 2.1);

    }
于 2013-10-09T00:44:15.477 回答