如何更新for循环中更改的变量?

时间:2021-07-21 07:27:32

Hi I'm an AP computer science student and this seems like a silly problem that I have but please bare with me.

嗨,我是一名AP计算机科学专业的学生,​​这看起来像是一个愚蠢的问题,但请与我同行。

Here is the assignment
Program Description: You just started working as a programmer. You've agreed to the following compensation package.

这是作业程序描述:你刚刚开始作为程序员工作。您已同意以下补偿方案。

• You are paid $30 an hour. • You earn an extra $25.50 an hour for any part of a day where you work more than 8 hours.
• You earn an extra $15 an hour for hours beyond 40 in any one week.
• You also earn a 125% bonus on any amount you earn for working on Saturday, and a 50% bonus for working Sunday.

•您每小时支付30美元。 •如果您工作超过8小时,您每小时可获得额外25.50美元的额外费用。 •在任何一周内,您每小时可获得超过40小时的额外15美元。 •您在周六工作时获得的任何金额都可获得125%的奖金,周日工作时可获得50%的奖金。

Work hours that I used , stored as txt for file reader

我使用的工作时间,存储为文件阅读器的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  

Sample Output: (be sure to run several test cases of your own by adding additional data to your data file)

示例输出:(确保通过向数据文件添加其他数据来运行您自己的几个测试用例)

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. 

The problem That I see is that what ever happens inside the for loops doesn't affect the outside of my code and I find that confusing.

我看到的问题是for循环中发生的事情不会影响我的代码外部,我发现这令人困惑。

Please don't change the entire code, I also want to understand the code at my current level so I can learn and progress, Thank you!

请不要更改整个代码,我也想了解当前级别的代码,以便我可以学习和进步,谢谢!

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. ");
    }
}

2 个解决方案

#1


3  

If I am understanding your problem, you may have a scope issue.

如果我理解您的问题,您可能会遇到范围问题。

Any variables declared inside of a scope (anything inside curly braces) only lasts for as long as you are in that scope.

在范围内(花括号内的任何内容)声明的任何变量只有在您在该范围内时才会持续。

In your case, your scope is the for loop.

在您的情况下,您的范围是for循环。

For instance.

例如。

(for int x = 0; x < 10; x++){
    int y = y+x;
}

Your y value here would be inaccessible outside of the for loop.

这里的y值在for循环之外是不可访问的。

However,

然而,

if you did:

如果你这样做:

int y = 0;
(for int x = 0; x < 10; x++){
    y = y+x;
}

Your y value would be saved and usable elsewhere since it was declared outside of the scope of the for loop.

您的y值将被保存并可在其他地方使用,因为它已在for循环的范围之外声明。

I hope this answers your question.

我希望这回答了你的问题。

I also see that your for loop conditions are x<=1 where you are declaring x=1. This will cause your loop to only run once. Maybe that's what you intended. I am not sure.

我还看到你的for循环条件是x <= 1,你声明x = 1。这将导致您的循环只运行一次。也许这就是你的意图。我不确定。

Good luck.

祝你好运。

#2


0  

First, use your IDEs autoformat to properly format your code; easy to read code is easy to reason about.

首先,使用IDE的autoformat正确格式化代码;易于阅读的代码易于推理。

Secondly, your for loops are like this:

其次,你的for循环是这样的:

              for(int x=1; x<=1; x++) {...}

Think about how many times the body of the loop will get executed.

想想循环体将执行多少次。

#1


3  

If I am understanding your problem, you may have a scope issue.

如果我理解您的问题,您可能会遇到范围问题。

Any variables declared inside of a scope (anything inside curly braces) only lasts for as long as you are in that scope.

在范围内(花括号内的任何内容)声明的任何变量只有在您在该范围内时才会持续。

In your case, your scope is the for loop.

在您的情况下,您的范围是for循环。

For instance.

例如。

(for int x = 0; x < 10; x++){
    int y = y+x;
}

Your y value here would be inaccessible outside of the for loop.

这里的y值在for循环之外是不可访问的。

However,

然而,

if you did:

如果你这样做:

int y = 0;
(for int x = 0; x < 10; x++){
    y = y+x;
}

Your y value would be saved and usable elsewhere since it was declared outside of the scope of the for loop.

您的y值将被保存并可在其他地方使用,因为它已在for循环的范围之外声明。

I hope this answers your question.

我希望这回答了你的问题。

I also see that your for loop conditions are x<=1 where you are declaring x=1. This will cause your loop to only run once. Maybe that's what you intended. I am not sure.

我还看到你的for循环条件是x <= 1,你声明x = 1。这将导致您的循环只运行一次。也许这就是你的意图。我不确定。

Good luck.

祝你好运。

#2


0  

First, use your IDEs autoformat to properly format your code; easy to read code is easy to reason about.

首先,使用IDE的autoformat正确格式化代码;易于阅读的代码易于推理。

Secondly, your for loops are like this:

其次,你的for循环是这样的:

              for(int x=1; x<=1; x++) {...}

Think about how many times the body of the loop will get executed.

想想循环体将执行多少次。