简体   繁体   English

有人可以告诉我这个嵌套循环中发生了什么吗?

[英]Can someone explain to me what is going on within this nested loop?

Someone please explain what is going on within the nested loop below. 有人请解释下面的嵌套循环中发生了什么。 I'm having trouble understanding how it works. 我在理解其工作方式时遇到了麻烦。

int numberOfTimesheets;
int centsPerHour = 0;
int hoursWorked;
total = 0;
numberOfTimesheets = stdin.nextInt();

for(int i = 1; i <= numberOfTimesheets; i++){
   hoursWorked = 0;
   centsPerHour = stdin.nextInt();
   for (int ii = 1; ii <= 5; ii++){
      hoursWorked = hoursWorked + stdin.nextInt();
   }
   total = total + (hoursWorked * centsPerHour);
}

Nested for loop is itearting 5 times, summing 5 user inputs to variable hoursWorked . 嵌套for循环将出现5次,将5个用户输入加到变量hoursWorked Point to all this is probably to count number of hours the user worked in that week (each iteration for each day of the week). 指向所有这一切可能是要计算用户在该周中工作的小时数(每个星期中的每一天的每次迭代)。 Then get his salary by multiplying his hours by his pay per hour, and add it to total . 然后通过将他的小时数乘以每小时的工资来获得他的薪水,并将其加到total

It's pretty straightforward, the only thing you may not understand is: 这非常简单,您可能唯一不了解的是:

hoursWorked = hoursWorked + stdin.nextInt();

It translates to something like this: 它翻译成这样的东西:

new value of hoursWorked = old value of hoursWorked + userInput

It can also be written as: 它也可以写成:

hoursWorked += stdin.nextInt();

This is the code commented with what it is doing at each step: 这段代码注释了每一步的操作:

//go through each time sheet
for(int i = 1; i <= numberOfTimesheets; i++){
    hoursWorked = 0; // reset the number of hours worked (presumably for that week).
    centsPerHour = stdin.nextInt(); //get the wage of the current timesheet

    //go through each day for the current timesheet
    for (int ii = 1; ii <= 5; ii++){
        hoursWorked = hoursWorked + stdin.nextInt(); //add up the number of hours worked in that week
    }
    total = total + (hoursWorked * centsPerHour); //add the amount of money made this week to their current total (salary).
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以向我解释为什么此嵌套循环函数以这种方式打印吗? - Can someone explain to me why this nested loop function print this way? 有人可以解释这个数组发生了什么吗? - Can someone explain what is going on with this array? java-有人可以向我解释发生了什么吗? 有很多事情还没有教给我 - java - Can someone explain to me what is going on? there's a lot of things going on in which I haven't been taught yet 有人能告诉我为什么这种方法会进入无限循环吗? - Can someone tell me why this method is going into an infinite loop? 有人能解释一下这个数学方法的作用吗? - Can someone explain me what this math's method does? java 中的数据源是什么? 有人可以用简单的语言向我解释吗? - What is a datasource in java? Can someone please explain me in simple language? 有人可以告诉我这里的Java代码在做什么吗? - Can someone please explain to me what the java code here is doing? 有人可以向我解释一下哨兵在Java中的作用吗? 或者它是如何工作的? - Can someone explain to me what a sentinel does in Java? Or how it works? 谁能给我解释一下Java中关于内存的嵌套for循环的行为? - Can anyone explain to me the behavior of a nested for loop in Java in regards to memory? 有人可以向我解释这种递归方法吗? - Can someone explain me this recursive method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM