2013-11-25 65 views
0
package developer; 
import java.util.*; 
import java.lang.Math.*; 

public class Developer 
{ 
    static Scanner console = new Scanner(System.in); 

    String workType; // This will be either an app, or game 
    String name; 
    int pay; 
    int weekPay; 
    int hrsWorked; 
    double tax; 

    public Developer() 
    { 
     name = "Ciaran"; 
    } 

    Developer(String appType, String coderName) 
    { 
     workType = appType; 
     name = coderName; 
    }// End developer 

    Developer(String appType, int pay) // Class to choose the pay rate depending on if it is a game or app 
    { 
     System.out.println("Are you coding an app or a game? "); 
     appType = console.next(); 

     if(appType == "app") 
     { 
      pay = 20; 
     } 
     if(appType == "game") 
     { 
      pay = 30; 
     } 
     else 
     { 
      System.out.println("Please enter either 'app' or 'game' "); 
     } 
    }// End developer 

    Developer(int hrsWorked, double tax, int weekPay, int pay) // Class to choose the tax bracket which the developer is in 
    { 
     System.out.println("Please enter how many hours you have worked this week: "); 
     hrsWorked = console.nextInt(); 

     weekPay = hrsWorked * pay; 

     if(weekPay >= 865) 
     { 
      tax = 0.4; 
     } 
     else 
     { 
      tax = 0.21; 
     } 


    }// End developer 

    Developer(int weekPay, int tax) // Gets the pay after tax 
    { 
     weekPay = weekPay * tax; 
    }// End developer 



    public void display() 
    { 
     System.out.println("This display method works"); 
     System.out.println("User: " + name); 
    } 


    public static void main(String[] args) 
    { 
     Developer myDev = new Developer(); 

     myDev.display(); 
    } // End main 

}// End public class developer 

我試圖讓這個程序詢問用戶他們的名字是什麼;如果他們正在開發一款遊戲或應用程序以及其上的小時數。有了這些信息,我想要計算開發人員賺取的稅金是多少。我似乎無法通過display()方法向用戶提問,但我不知道該怎麼辦。我希望有人能幫助我。輸出用戶數據以顯示類

+0

這個won'r work if(appType ==「app」)'。您必須將字符串與'equals'進行比較,即if(「app」.equals(appType))' –

回答

0

有可能在你的代碼被不同地做幾件事情,讓我們把它分解:

1.No需要進行控制檯靜態類型,你可以使用:

private Scanner console = new Scanner(System.in); 

2。 weekPay是int類型的,但你的稅是雙重,如果你不想weekPay強制轉換爲整數,將其更改爲:

double weekPay; 

3.Later上,你是稅後計算weekPay,讓我們introduc e變量爲:

double weekPayAfterTax; 

4.所有這些Developer()方法都是構造函數,我認爲你在這裏稍微有些困惑。當然,你可以有很多的構造函數,但對我們來說,我們只保留無PARAMS構造:

public Developer() { 
    name = "Ciaran"; 
    //you could initialise all the other variables here as well, 
    //I'll leave it as an exercise for you :) 
} 

5.Let的創建會問的所有問題,並設置相應的變量的方法:

void setData() { 
    //let's get the name 
    System.out.print("What's your name: "); 
    name = console.nextLine(); 

    System.out.print("Are you coding an app or a game? "); 

    //since we want the user to enter 'app' or 'game' 
    //we need to loop until we got these 
    //we can do this by creating endless while loop, 
    //which we will end when we have correct input 
    while (true) { 
     workType = console.next(); 

     if (workType.equals("app")) { 
      pay = 20.0; 

      //stop the loop 
      break; 
     } 
     else if (workType.equals("game")) { 
      pay = 30.0; 

      //stop the loop 
      break; 
     } 
     else { 
      System.out.print("Please enter either 'app' or 'game': "); 
      //back to top 
     } 
    } 

    //ok, we're out the loop, let's get number of hours 
    System.out.print("Please enter how many hours you have worked this week: "); 
    hrsWorked = console.nextInt(); 

    //calculate weekPay 
    weekPay = hrsWorked * pay; 

    if(weekPay >= 865) { 
     tax = 0.4; 
    } 
    else { 
     tax = 0.21; 
    } 

    //calculate pay after tax 
    weekPayAfterTax = weekPay - weekPay * tax; 
} 

6.Let的更新我們的display()方法來顯示所有的信息:

public void display() { 
    System.out.println("This display method works"); 
    System.out.println("User: " + name); 
    System.out.println("Work type: " + workType); 
    System.out.println("Pay: " + pay); 
    System.out.println("Week pay: " + weekPay); 
    System.out.println("Week pay after tax: " + weekPayAfterTax); 
} 

7.In你的主要方法,你終於可以創建開發類的實例,並獲取數據:

public static void main(String[] args) { 
    Developer myDev = new Developer(); 

    myDev.setData(); 
    myDev.display(); 
} 

上面的代碼可以改善(如檢查,如果用戶輸入在那裏的預期數量),並且您的問題當然可以做不同的,但這裏的開始。

請查看一些教程來了解基本知識,如this onethis one。最重要的是,試驗並不要讓別人因爲不理解某事而讓你失望。

1

System.in將從命令行讀取輸入。你應該用java.util.Scanner中和nextLine像這樣把它包:

Scanner scanner = new Scanner(System.in); 
String user_input = scanner.nextLine(); 

一定要檢查

scanner.hasNextLine() 

,然後再繼續,否則你會得到一個錯誤。

+0

讓我們避免使用'== true'反模式。 :) – Zong

+0

我只是想讓用戶在打電話之前看看是否有下一行,但你說得對。 – mttdbrd