2015-11-18 41 views
-2

您剛剛開始作爲程序員工作。您已同意以下薪酬方案。古怪的Wags Java程序213a。使用幾個if else語句

•您每小時支付30美元。 •在工作時間超過8小時的任何一天中,您每小時可以多掙25.5美元。
•在任何一週內,您可以獲得額外的每小時15美元,超過40美元的時間。
•您在星期六工作時賺取的任何獎金都可獲得125%的獎金,並在星期天工作時獲得50%的獎金。

您的輸入文件將是您在星期天開始的每週工作的小時數。您需要編寫一個程序,該程序將繼續處理該文件,以計算每週工作的總工資,直至達到文件結尾。每行輸入將包含一個星期小時(每行7個整數,每個小於或等於24)。用美元符號輸出工資總額,四捨五入到工作每週最近的一分錢。

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

public class prog213a 
{ 
public static void main (String args []) 
{ 
    Scanner inFile = null; //setting scanned file as null 

    double weeklyPay=0,sunPay=0, monPay=0, tuesPay=0, wedPay=0, thurPay=0, friPay=0, satPay=0; 

    try 
    { 
     inFile =new Scanner(new File("prog213a.dat")); //reading file 
    } 
    catch(Exception e) //if file doesn't exist 
    { 
     System.out.println("File not found"); //output that file doesn't exist 
     System.exit (0); //exit program 
    } 

    while (inFile.hasNext()) 
    { 
     final int sunHours = inFile.nextInt(); 
     final int monHours = inFile.nextInt(); 
     final int tuesHours = inFile.nextInt(); 
     final int wedHours = inFile.nextInt(); 
     final int thurHours = inFile.nextInt(); 
     final int friHours = inFile.nextInt(); 
     final int satHours = inFile.nextInt(); 
     final int weeklyHours = monHours + tuesHours + wedHours + thurHours + friHours; 
     if(sunHours>8){ 
      sunPay=((sunHours-8)*55.50)+240; 
     } 
     else{ 
      sunPay= sunHours*30; 
     } 
     if(monHours>8){ 
      monPay=((monHours-8)*55.50)+240; 
     } 
     else{ 
      monPay= monHours*30; 
     } 
     if(tuesHours>8){ 
      tuesPay=((tuesHours-8)*55.50)+240; 
     } 
     else{ 
      tuesPay= tuesHours*30; 
     } 
     if(wedHours>8){ 
      wedPay=((wedHours-8)*55.50)+240; 
     } 
     else{ 
      wedPay= wedHours*30; 
     } 
     if(thurHours>8){ 
      thurPay=((thurHours-8)*55.50)+240; 
     } 
     else{ 
      thurPay= thurHours*30; 
     } 
     if(friHours>8){ 
      friPay=((friHours-8)*55.50)+240; 
     } 
     else{ 
      friPay= friHours*30; 
     } 
     if(satHours>8){ 
      satPay=((satHours-8)*55.50)+240; 
     } 
     if(weeklyHours>40){ 
      weeklyPay=((weeklyHours-40)*45)+1200; 
     } 
     System.out.print(sunPay); 
    } 
    } 
} 

我到目前爲止已經寫了這個代碼,但是當我去測試程序,看看它甚至獲得正確週日工資,我得到這個「295.5210.0180.0」 我需要幫助得到這個固定代碼以便它適合參數並輸出正確的答案。

數據文件:

+0

家庭作業問題被認爲是題外話。 –

+1

請閱讀[mcve],並確保您閱讀了頁面底部鏈接的「如何調試小程序」文檔。 – AdrianHHH

+0

@JasonD我認爲這個網站是爲了幫助程序員需要幫助他們的代碼,或者我錯了嗎? – madS

回答

0

我能夠固定編碼我自己。

Scanner inFile = null; //setting scanned file as null 

    double weeklyPay=0,sunPay=0, monPay=0, tuesPay=0, wedPay=0, thurPay=0, friPay=0, satPay=0, overPay=0; //initializing variables 
    int weekNumber=1; //which week it is 

    DecimalFormat twodigits= new DecimalFormat("0.00"); //formating numbers 

    try 
    { 
     inFile =new Scanner(new File("prog213a.dat")); //reading file 
    } 
    catch(Exception e) //if file doesn't exist 
    { 
     System.out.println("File not found"); //output that file doesn't exist 
     System.exit (0); //exit program 
    } 

    while (inFile.hasNextLine()) //while the file has a next line of data 
    { 
     System.out.println("Week " + weekNumber + ": "); //header 

     int sunHours = inFile.nextInt(); //getting the hours for each day 
     int monHours = inFile.nextInt(); 
     int tuesHours = inFile.nextInt(); 
     int wedHours = inFile.nextInt(); 
     int thurHours = inFile.nextInt(); 
     int friHours = inFile.nextInt(); 
     int satHours = inFile.nextInt(); 
     int weeklyHours = monHours + tuesHours + wedHours + thurHours + friHours; //weekly hours is summation of hours 

     System.out.println(sunHours + " " + monHours + " " + tuesHours + " " + wedHours + " " + thurHours + " " + friHours + " " + satHours); //output 

     //calculating daily pay depending on the specifications 

     if(sunHours>8) 
      sunPay=(((sunHours-8)*55.50)+240)*2.25; 
     else 
      sunPay=(sunHours*30)*2.25; 

     if(monHours>8) 
      monPay=((monHours-8)*55.50)+240; 
     else 
      monPay= monHours*30; 

     if(tuesHours>8) 
      tuesPay=((tuesHours-8)*55.50)+240; 
     else 
      tuesPay= tuesHours*30; 

     if(wedHours>8) 
      wedPay=((wedHours-8)*55.50)+240; 
     else 
      wedPay= wedHours*30; 

     if(thurHours>8) 
      thurPay=((thurHours-8)*55.50)+240; 
     else 
      thurPay= thurHours*30; 

     if(friHours>8) 
      friPay=((friHours-8)*55.50)+240; 
     else 
      friPay= friHours*30; 

     if(satHours>8) 
      satPay=(((satHours-8)*55.50)+240)*1.5; 
     else 
      satPay= (satHours*30)*1.5; 


     if(weeklyHours>40){ 
      overPay=(weeklyHours-40)*45; //pay if weekly hours are over 40 
      weeklyPay=(sunPay + monPay + tuesPay + wedPay + thurPay + friPay + satPay + overPay); //pay including over pay 
      System.out.println(twodigits.format(weeklyPay));//output 
     } 
     else{ 
      weeklyPay=(sunPay+monPay+tuesPay+wedPay+thurPay+friPay+satPay); //pay if hours is less than 40 
      System.out.println(twodigits.format(weeklyPay)); //output 
     } 

     weekNumber++; //increments the week number 
     System.out.println(); //blank line 
    }