我的主要方法有什么问题?

时间:2013-10-09 00:37:29

标签: java

我是一个java noob。我一直在主方法中遇到错误。请帮忙!我想我并没有以正确的方式调用这些方法。其他一切都应该有效。

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);
    }
}

5 个答案:

答案 0 :(得分:3)

看起来你的某些方法正在期待参数,而你却没有提供它们。

例如

public static double calculateThreshold (char married){}

您无法调用此方法calculateThreshold();

你需要传入已婚的char calculateThreshold('y');

答案 1 :(得分:0)

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

答案 2 :(得分: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)确保对你的程序的其余部分做同样的事情。

答案 3 :(得分:0)

实际上你的所有方法都会返回一些答案,在这些答案中,它可以传递给下一个方法......

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

答案 4 :(得分: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);

    }