简体   繁体   English

循环和输出

[英]For-loop and output

This is a homework question that I cannot figure out. 这是我无法解决的作业问题。 It's in Java. 在Java中。

Suppose we want to write a for loop that produces the following nine lines of output: 假设我们要编写一个for循环,该循环产生以下九行输出:

 4:
 8:
12:
16:
20:
24:
28:
32:
36:

Suppose we will use the following for-loop to produce the output: 假设我们将使用以下for循环来生成输出:

int index;
for (index = __A__; __B__ ; __C__ ) {
    System.out.printf(__D__, index);
}

Answer what should be in the four blanks in each of the following for the code? 回答以下每个代码的四个空格中应该包含什么?

  1. _ _ A _ _ _ _ A _ _
  2. _ _ B _ _ _ _ B _ _
  3. _ _ C _ _ _ _ C _ _
  4. _ _ D _ _ _ _ D _ _
int index;
for (index = __A__; __B__ ; __C__ ) {
    System.out.printf(__D__, index);
}

If you see the output, the number (index) starts with 4. 如果看到输出,则数字(索引)以4开头。

Therefore, A : 4 因此,A:4

It ends at 36. 结束于36。

Therefore, B : index<=36 因此,B:index <= 36

It increases by 4. 它增加4。

Therefore, C : index+=4 因此,C:index + = 4

Printed line's format is D : "%2d:\\n" 印刷行的格式为D:“%2d:\\ n”

This for loop will give you the answer: 这个for循环将为您提供答案:

for (index=4; index<=36; index+=4) {
    System.out.printf(":\n"+ index);
}

A is index = 4 A是指数= 4

B is index<=36 B是指数<= 36

C is index+=4 C是index + = 4

D is ":\\n" D是“:\\ n”

Make the print statement as: 使打印语句为:

System.out.printf(index + ":\n");

Now you will get the exact answer you expect. 现在,您将获得期望的确切答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM