奇怪的摇摆Java程序213a。使用几个if-else语句
你刚开始做程序员。您已同意以下赔偿方案 •你的工资为每小时30美元。 •一天中工作超过8小时的任何时间,你都可以额外赚取25.50美元一小时。奇怪的摇摆Java程序213a。使用几个if-else语句,java,computer-science,Java,Computer Science,你刚开始做程序员。您已同意以下赔偿方案 •你的工资为每小时30美元。 •一天中工作超过8小时的任何时间,你都可以额外赚取25.50美元一小时。 •任何一周超过40小时,每小时可额外赚取15美元。 •您还可以在周六工作时获得125%的奖金,周日工作时获得50%的奖金 您的输入文件将是从星期日开始的一周中您每天工作的小时数。你需要编写一个程序,继续处理文件,计算你每周工作的总工资,直到文件结束。每行输入将包含一周小时(每行7个整数,每个整数小于或等于24)。输出每周工作的总工资,用美元符号四舍五入到
•任何一周超过40小时,每小时可额外赚取15美元。
•您还可以在周六工作时获得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”
我需要帮助修复此代码,使其符合参数并输出正确答案
数据文件:
9810108995
788089
6105000我能够自己修复代码
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
}
家庭作业问题被视为离题。请阅读并确保阅读页面底部链接的“如何调试小程序”文档。@JasonD我以为这个网站是为了帮助需要代码帮助的程序员,还是我错了?是的,但是你问问题的方式不是这里的人可以帮助的。您已经发布了原始问题和整个解决方案;但你在向我们提出的问题的结尾是“我需要帮助修复此代码”。正如你在上面Adrian发布的链接中所看到的,请尽量减少你的问题,使之足够小,以便在你自己完全调试完代码后,它会集中在你认为不起作用的代码部分。