如何多次调用方法

时间:2014-10-11 23:47:14

标签: java jgrasp

我如何在这里为每个问题调用一个方法?我尝试了一些不同的东西,但它不适合我。以下是我到目前为止的情况:

import java.util.Scanner;

public class TaxCalc
{
    public static void main(String [] args)
    {
      Scanner keyboard = new Scanner(System.in);

      System.out.print("Enter number of dependents: ");
      int dependents=keyboard.nextInt();

      System.out.print("Enter number of pigs: ");
      int pigs= keyboard.nextInt();

      System.out.print("Enter number of oinks: ");
      double oinks=keyboard.nextDouble()-(pigs*500)+(200*dependents);

      System.out.println("Oinks after rewards: " + oinks);

      if(oinks<10000) oinks -= oinks*0.02; //2% tax
      else if(oinks<5000) oinks -= oinks*0.1; //10% tax
      else oinks -= oinks*0.2; //20% tax
      System.out.println("Oinks after penalties: " + oinks);
    }
}

1 个答案:

答案 0 :(得分:0)

你可能正在寻找类似这样的东西

import java.util.Scanner;

    public class TaxCalc
    {
        public static void main(String [] args)
        {
          Scanner keyboard = new Scanner(System.in);

          int dependents = inputInt("Enter number of dependents: ", keyboard);

          int pigs = inputInt("Enter number of pigs: ", keyboard);

          double oinks= inputInt("Enter number of oinks: ", keyboard) -(pigs*500)+(200*dependents);

          System.out.println("Oinks after rewards: " + oinks);

          if(oinks<10000) oinks -= oinks*0.02; //2% tax
          else if(oinks<5000) oinks -= oinks*0.1; //10% tax
          else oinks -= oinks*0.2; //20% tax
          System.out.println("Oinks after penalties: " + oinks);
        }

    public int inputInt(String prompt, Scanner keyboard){
        System.out.println(prompt);
        return keyboard.nextInt();
    }

    public double inputDouble(String prompt, Scanner keyboard){
        System.out.println(prompt);
        return keyboard.nextDouble();
    }
}

虽然很难说出你想要达到的目标。