简体   繁体   English

使用 for 循环打印数组元素

[英]Printing array elements with a for loop

This is a challenge question from my online textbook I can only get the numbers to prin forward... :(这是我的在线教科书中的一个挑战问题,我只能得到要打印的数字...:(

Write a for loop to print all elements in courseGrades, following each element with a space (including the last).编写一个 for 循环以打印 courseGrades 中的所有元素,每个元素后跟一个空格(包括最后一个)。 Print forwards, then backwards.向前打印,然后向后打印。 End each loop with a newline.以换行符结束每个循环。 Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7例如:如果 courseGrades = {7, 9, 11, 10},打印:7 9 11 10 10 11 9 7

Hint: Use two for loops.提示:使用两个 for 循环。 Second loop starts with i = NUM_VALS - 1.第二个循环以 i = NUM_VALS - 1 开始。

Note: These activities may test code with different test values.注意:这些活动可能会测试具有不同测试值的代码。 This activity will perform two tests, the first with a 4-element array (int courseGrades[4]), the second with a 2-element array (int courseGrades[2]).此活动将执行两个测试,第一个使用 4 元素数组 (int courseGrades[4]),第二个使用 2 元素数组 (int courseGrades[2])。

import java.util.Scanner;

 public class CourseGradePrinter {
 public static void main (String [] args) {
  final int NUM_VALS = 4;
  int[] courseGrades = new int[NUM_VALS];
  int i = 0;

  courseGrades[0] = 7;
  courseGrades[1] = 9;
  courseGrades[2] = 11;
  courseGrades[3] = 10;

  /* Your solution goes here  */

  for(i=0; i<NUM_VALS; i++){
     System.out.print(courseGrades[i] + " ");
  }

  for(i=NUM_VALS -1; i>3; i++){
     System.out.print(courseGrades[i]+ " ");
     }

  return;
}
}

This is the code to answer the question from zyBooks, 6.2.3: Printing array elements with a for loop. 这是回答zyBooks 6.2.3中的问题的代码:使用for循环打印数组元素。

 for (i = 0; i < NUM_VALS; i++) {
     System.out.print(courseGrades[i] + " ");
 }
 System.out.println("");

 for (i = NUM_VALS - 1; i >= 0; i--) {
    System.out.print(courseGrades[i] + " ");
 }

 System.out.println(""); 

Your two loops almost were correct. 您的两个循环几乎是正确的。 Try using this code: 尝试使用以下代码:

for (int i=0; i < NUM_VALS; i++) {
    // this if statement avoids printing a trailing space at the end.
    if (i > 0) {
        System.out.print(" ");
    }
    System.out.print(courseGrades[i]);
}

for (int i=NUM_VALS-1; i >= 0; i--) {
    if (i > 0) {
        System.out.print(" ");
    }
    System.out.print(courseGrades[i] + " ");
}

To print backwards you want: 要向后打印:

for(i = NUM_VALS - 1; i >= 0; i--) {
    System.out.print(courseGrades[i] + " ");
}
// To end with a newline
System.out.println("");

I also have been working on the this textbook question.我也一直在研究这个教科书问题。 The issue with the above code is that i has already been assigned, so trying using int in the for loop will cause an error.上面代码的问题是 i 已经被赋值了,所以尝试在 for 循环中使用 int 会导致错误。 Below is the code I used to successfully achieve the desired outcome.下面是我用来成功实现预期结果的代码。

for ( i = 0 ; i <NUM_VALS; ++i) {
     if (i > 0) {
        System.out.print(""); 
     }
     System.out.print(courseGrades[i] + " ");
  }
  System.out.println("");
  
  for ( i = NUM_VALS -1; i >=0; --i) {
     if (i > 0) {
        System.out.print(""); 
     }
     System.out.print( courseGrades[i] + " ");
  }
     System.out.println();

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

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