如何在数组中每隔一个值进行更改(Java)

huangapple go评论62阅读模式
英文:

How To Change Every Other Value in An Array (Java)

问题

对于这个编程作业,我的教授要求我们使用 for 循环将数组中每隔一个数字替换为 100:

    //-- (8) 使用下面的名为 grades 的数组。
	//--	 从索引 0 开始,使用 for 循环将每隔一个成绩改为 100。
	//--	 在进行更改之前和之后,使用 Printf 打印出数组在变化前和变化后的样子。
   
    int i;
    int g;
    int grades[] = { 87, 95, 65, 70, 77, 99, 0, 65, 25, 80, 90, 11 };
    System.out.println("\n(8) 变更前: " + Arrays.toString(grades)); 
    for (i = 0; i < grades.length; ++i)
      {
      //FIXME: 对于每隔一个成绩(索引 0、2、4、6、8),将成绩改为 100
       
      }
	 System.out.println("\n(8) 变更后:" + Arrays.toString(grades));

然而,我不确定如何实现这一点。我尝试了像这样的方法:grade[0] = 100; grade[2] = 100; 等等,但是我的教授希望我们假装程序不知道我们把这些数字放入数组中(如果这有意义的话)。我感谢你们能给我的任何帮助。

英文:

For this programming assignment, my professor wants us to replace every other number in this array into a 100 using a for loop:

//-- (8) Use the array called grades, below.
//--	 starting with index 0 use a for loop to change every other 
//--	 grade to 100.
//--	 Printf what the array was BEFORE the changes, and AFTER then Changes.

int i;
int g;
int grades[] = { 87, 95, 65, 70, 77, 99, 0, 65, 25, 80, 90, 11 };
System.out.println (&quot;\n(8) Before change: &quot; + Arrays.toString(grades)); 
for (i = 0; i &lt; grades.length; ++i)
  {
  //FIXME: For every other grade (index 0, 2, 4, 6, 8), change grade to 100
   
  }
 System.out.println (&quot;\n(8) After change:&quot; + Arrays.toString(grades));

However, I am not sure how to do this. I have tried something like this: grade[0] = 100; grade[2] = 100; etc., but my professor wants us to pretend the program doesn't know that we put in those numbers for the array (if that makes sense). I appreciate any help you guys can give me.

答案1

得分: 4

你可以通过调整 for 循环的参数来实现这一点:

for (i = 0; i < grades.length; i+=2) {
   grades[i] = 100;
}

因此,在这里,每次迭代后,你将使用 i+=2 而不是 i++i 增加 2

英文:

You can achieve this by playing with the for loop arguments:

for (i = 0; i &lt; grades.length; i+=2) {
   grades[i]=100;
}

So here, after each iteration, you will increment i by 2 by using i+=2 instead of i++.

答案2

得分: 1

使用循环。每次递增2而不是1。例如,

for (i = 0; i < grades.length; i += 2)
{
    grades[i] = 100;
}

或者,如果你不能改变++i,只需:

for (i = 0; i < grades.length; ++i)
{
    grades[i] = 100;
    ++i;
}

或者

for (i = 0; i < grades.length; ++i)
{
    grades[i++] = 100;
}
英文:

Use a loop. Increment by 2 instead of by 1. For example,

for (i = 0; i &lt; grades.length; i += 2)
{
    grades[i] = 100;
}

Or, if you can't change ++i just

for (i = 0; i &lt; grades.length; ++i)
{
    grades[i] = 100;
    ++i;
}

or

for (i = 0; i &lt; grades.length; ++i)
{
    grades[i++] = 100;
}

答案3

得分: 1

可以甚至使用取模运算符。

for (i = 0; i < grades.length; ++i)
{
    if (i % 2 == 0) {
      grades[i] = 100;
    }
}
英文:

Could even use the modulo operator

for (i = 0; i &lt; grades.length; ++i)
{
    if (i % 2 == 0) {
      grades[i] = 100;
    }
}

huangapple
  • 本文由 发表于 2020年10月10日 06:59:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64288303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定