奇怪的摇摆 Java 计划 213a。使用多个 if else 语句
Weird Wags Java Program 213a. Using several if else statements
您刚刚开始从事程序员工作。您已同意以下补偿方案。
• 您每小时支付 30 美元。
• 如果您在一天中的任何时间工作超过 8 小时,您每小时可额外赚取 25.50 美元。
• 任何一周超过 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”
我需要帮助来修复此代码,以便它适合参数并输出正确的答案。
数据文件:
9 8 10 8 9 9 5
7 8 8 8 0 8 9
6 10 5 0 0 0 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
}
您刚刚开始从事程序员工作。您已同意以下补偿方案。
• 您每小时支付 30 美元。
• 如果您在一天中的任何时间工作超过 8 小时,您每小时可额外赚取 25.50 美元。
• 任何一周超过 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” 我需要帮助来修复此代码,以便它适合参数并输出正确的答案。
数据文件:
9 8 10 8 9 9 5
7 8 8 8 0 8 9
6 10 5 0 0 0 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
}