调用方法

时间:2014-08-23 03:03:35

标签: java function methods

我有一个项目,但在继续之前我需要掌握一些基础知识。这是我尝试从java中调用main函数但我无法编译的问题。我已阅读反馈,我使用开关调用每个功能。我总是得到"错误无法找到符号"。我在这里做了一些改变,试图让它运行。再一次,我觉得我缺乏一些非常基本的东西,这使我无法让它发挥作用。任何想法

import java.util.*;
import java.lang.*;
import java.io.*;

class Practice {

    static void wagecalc() {

        Scanner input = new Scanner(System.in);

        System.out.println("Input pay per hour:");
        int a = input.nextInt();
        int b;
        System.out.println("Input hours worked:");
        b = input.nextInt();
        int c;
        System.out.println("Input days worked:");
        c = input.nextInt();

        int gross = a * b * c;
        System.out.println("Your gross pay is:" + gross);

        // Determining tax rate//
        if (gross < 10000) {
            int taxrate = gross * 10 / 100;
            System.out.println("Your tax rate is 10%:/n");

            int total = gross - taxrate;
            System.out.println("Your net pay is:" + total + "/n");
        }

        else if (gross > 10000 || gross <= 30000) {
            int taxrate = gross * 15 / 100;
            System.out.println("Your tax rate is 15%:/n");

            int total = gross - taxrate;
            System.out.println("Your net pay is:" + total + "/n");
        }

        else if (gross >= 30000 || gross <= 70000) {
            int taxrate = gross * 20 / 100;
            System.out.println("Your tax rate is 20%:/n");

            int total = gross - taxrate;
            System.out.println("Your net pay is:" + total + "/n");

        }

        else if (gross > 70000) {
            int taxrate = gross * 25 / 100;
            System.out.println("Your tax rate is 25%:/n");

            int total = gross - taxrate;
            System.out.println("Your net pay is:" + total + "/n");
        }
    }

    static void autocalc() {

        Scanner input = new Scanner(System.in);

        // Declaring the variables as an integer//
        int auto;
        int loan;
        int interest;
        int mnydwn;
        int pymnt;
        int year;
        int month = 12;
        int tap;
        int term = year * month;
        int spec;

        System.out.println("Please enter the following information for your car loan:/n");
        System.out.println("Please enter the amount of the vehicle you wish to purchase:/n");
        auto = input.nextInt();
        System.out.println("Please enter the amount of money you wish to put down:/n");
        mnydwn = input.nextInt();
        System.out.println("Please enter the interest rate:/n");
        interest = input.nextInt();
        System.out.println("Please enter the term of the loan (years):/n");
        year = input.nextInt();
        System.out.println("Processing......../n");
        System.out.println("Processing............");

        // Calculations//

        loan = auto - mnydwn;
        int intamt = loan * interest;
        tap = loan + intamt;
        pymnt = tap / term;

        // Display//

        System.out.println("Process...Complete./n");
        System.out.println("Car amount:" + auto);
        System.out.println("Loan recieved:" + loan);
        System.out.println("Interest rate:" + interest);

        // if statement for proper output of year//
        if (year == 1) {
            System.out.println("Your monthly payments will be" + pymnt + "for a" + year + "year" + term + "months.");
        } else if (year > 1) {
            System.out.println("Your monthly payments will be" + pymnt + "for" + year + "years" + term + "months.");
        }

        System.out.println("Total amount paid at the end of the term" + tap);
    }

    public static void main(String[] args) throws java.lang.Exception {
        // User input//
        Scanner input = new Scanner(System.in);
        int count = 0; // Count for the while loop//

        // Instructions on how to begin interface//

        System.out.println("Hi!/n");
        System.out.println("...Thanks for using D Anomaly's Finance calculator!!/n");
        System.out.println("Please choose one of the following:/n");
        System.out.println(" 1. Wages:/n");
        System.out.println(" 2. Auto Loan:/n");
        // System.out.println(" 3. Home Loan:/n");//
        System.out.println("Choose 1-3");

        int calc = input.nextInt();

        switch (calc) {

        case 1:
            wagecalc();
            break;
        case 2:
            autolcalc();
            break;
        case 3: // homecalc();//
            break;

            if (calc >= 4) {
                System.out.println(" Invalid entry!!!/n");
                System.out.println("Please try again:");
                break;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这里有一些问题,包括:

1。 如果您想从public void wageinformation(int gross)

调用static,则需要声明main()

2。 您在收到它作为参数后立即计算int gross = a * b * c

3。 你有(或在这篇文章的初始版本中)有几个额外的括号。

4。 deductiontotal变量永远不会在wageinformation()方法中声明。

如果您希望提高自己的技能,我强烈建议您完成一些在线Java教程。