We Are Going To Discuss About Printing array elements with a for loop. So lets Start this Java Article.
Printing array elements with a for loop
- Printing array elements with a for loop
This is the code to answer the question from zyBooks, 6.2.3: Printing array elements with a for loop.
for (i = 0; i < NUM_VALS; i++) { System.out.print(courseGrades[i] + " "); }
- Printing array elements with a for loop
This is the code to answer the question from zyBooks, 6.2.3: Printing array elements with a for loop.
for (i = 0; i < NUM_VALS; i++) { System.out.print(courseGrades[i] + " "); }
Solution 1
This is the code to answer the question from zyBooks, 6.2.3: Printing array elements with a for loop.
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("");
Original Author A. Nony Mous Of This Content
Solution 2
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] + " ");
}
Original Author Tim Biegeleisen Of This Content
Solution 3
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("");
Original Author Joels Elf Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.