我是一名AP计算机科学专业的学生,这是作业 程序描述:你刚刚开始担任程序员。您已同意以下补偿方案。
•您每小时支付30美元。 •如果您工作超过8小时,您每小时可获得额外25.50美元的额外费用 •在任何一周内,您每小时可获得额外15美元的小时数 •您在周六工作时获得的任何金额还可获得125%的奖金,周日工作时可获得50%的奖金。
我使用的工作时间,存储为文件阅读器的txt
9 8 10 8 9 9 5 7 8 8 8 0 8 9 6 10 5 0 0 0 0 8 8 8 8 8 8 8 6 6 6 6 6 6 6 10 3 4 5 7 5 4 8 8 8 8 8 0 0
示例输出:(确保通过向数据文件添加其他数据来运行您自己的几个测试用例)
Hours Worked: 9 8 10 8 9 9 5 Week # 1 $2581.88 Hours Worked: 7 8 8 8 0 8 9 Week # 2 $2033.25 Hours Worked: 6 10 5 0 0 0 0 Week # 3 $681 Hours Worked: End of reading from file. Vs My output Total Hours Worked: 58 Week #1 $2010.00 Total Hours Worked: 48 Week #2 $1560.00 Total Hours Worked: 21 Week #3 $630.00 Total Hours Worked: 56 Week #4 $1920.00 Total Hours Worked: 42 Week #5 $1290.00 Total Hours Worked: 38 Week #6 $1140.00 Total Hours Worked: 40 Week #7 $1200.00 Hours Worked: End of reading from file.
问题我看到for循环中发生的事情不会影响我的代码外部,我发现这令人困惑。
请不要更改整个代码,我也想了解当前级别的代码,以便我可以学习和进步,谢谢!
import java.util.*;
import java.io.*;
import java.text.*;
public class wages {
public static void main(String[] args) {
Scanner inFile = null;
int TotalW = 0; // total hours per week
int TotalD = 0; // total hours per day
double Wage = 0.0; // Wage for the weekdays
double WageS = 0.0; // wage for sat
double WageSu = 0.0; // wage for Sundays
double Total = Wage + WageS + WageSu;
int week = 1; // week # counter
DecimalFormat TD = new DecimalFormat("0.00"); // formatter
try {
// Create a scanner to read the file, file name is parameter
inFile = new Scanner(new File("Prog213a.txt"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
// Stop program if no file found
System.exit(0);
}
while (inFile.hasNext()) // as long as there is a next variable...
{
for (int x = 1; x <= 5; x++) // count the week days / first 5 variables
{
TotalD = inFile.nextInt();
if (TotalD > 8) Wage = Wage + ((TotalD - 8) * 25.50); // bonus if worked above 8 hours per day
TotalW = TotalW + TotalD;
}
for (int x = 1; x <= 1; x++) {
TotalD = inFile.nextInt();
if (TotalD > 8) WageS = WageS + ((TotalD - 8) * 25.50);
WageS = WageS + (WageS * 2.25);
TotalW = TotalW + TotalD;
}
for (int x = 1; x <= 1; x++) {
TotalD = inFile.nextInt();
if (TotalD > 8) WageSu = WageSu + ((TotalD - 8) * 25.50);
WageSu = WageSu + (WageSu * 2);
TotalW = TotalW + TotalD;
}
if (TotalW > 40) Total = Total + ((TotalW - 40) * 15);
Total = Total + (TotalW * 30);
System.out.println("Total Hours Worked: " + TotalW);
System.out.println("Week #" + (week++) + " $" + TD.format(Total));
TotalW = 0;
TotalD = 0;
Wage = 0;
WageS = 0;
WageSu = 0;
Total = Wage + WageS + WageSu;
}
System.out.println("Hours Worked: End of reading from file. ");
}
}
答案 0 :(得分:3)
如果我了解您的问题,您可能会遇到范围问题。
在范围内声明的任何变量(花括号内的任何内容)只有在您在该范围内时才会持续。
在您的情况下,您的范围是for循环。
例如。
(for int x = 0; x < 10; x++){
int y = y+x;
}
你的y值在for循环之外是不可访问的。
然而,
如果你这样做:
int y = 0;
(for int x = 0; x < 10; x++){
y = y+x;
}
您的y值将被保存并在其他地方使用,因为它是在for循环范围之外声明的。
我希望这能回答你的问题。
我还看到你的for循环条件是x <= 1,你声明x = 1。这将导致您的循环只运行一次。也许这就是你的意图。我不确定。
祝你好运。答案 1 :(得分:0)
首先,使用IDE的autoformat正确格式化代码;易于阅读的代码易于推理。
其次,你的for循环是这样的:
for(int x=1; x<=1; x++) {...}
考虑循环体将执行多少次。