2014-10-11 80 views
-1

如何爲每個問題調用此處的方法?我嘗試了一些不同的東西,但它不適合我。以下是我迄今爲止:如何多次調用方法

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

你想在每次輸入後調用一個方法嗎?開始[這裏](http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html)。 – Celeo 2014-10-11 23:49:24

+1

我不確定你在問什麼。你打算用什麼方法打電話?它應該在哪裏被調用?你現在的代碼有什麼問題? – Pshemo 2014-10-11 23:53:29

+0

這是我們必須做的http://gyazo.com/e4d2af76dbb9704a620f6d65f59b547f – user4068770 2014-10-11 23:59:20

回答

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

雖然很難說什麼是你想要實現的。

+1

如果「很難說出[提問者]想要達到什麼目的」,考慮標記脫離主題 - > _unclear你問什麼,而不是猜測答案。 – 2014-10-12 01:31:37