Question

Write a for loop to print all elements in courseGrades, following each element with a space...

Write a for loop to print all elements in courseGrades, following each element with a space (including the last). 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 

Hint: Use two for loops. Second loop starts with i = courseGrades.length - 1. (Notes)

IN JAVA:

import java.util.Scanner;

public class CourseGradePrinter {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_VALS = 4;
int [] courseGrades = new int[NUM_VALS];
int i;

for (i = 0; i < courseGrades.length; ++i) {
courseGrades[i] = scnr.nextInt();
}

/* Your solution goes here */

}
}

Homework Answers

Answer #1
import java.util.Scanner;

public class CourseGradePrinter {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        final int NUM_VALS = 4;
        int[] courseGrades = new int[NUM_VALS];
        int i;

        for (i = 0; i < NUM_VALS; i++) {
            courseGrades[i] = scnr.nextInt();
        }

        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();
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an if-else statement that checks patronAge. If 55 or greater, print "Senior citizen", otherwise print...
Write an if-else statement that checks patronAge. If 55 or greater, print "Senior citizen", otherwise print "Not senior citizen" (without quotes). End with newline. import java.util.Scanner; public class DetectSenior {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       int patronAge; //Solution goes here//       patronAge = scnr.nextInt();    } }
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Please use my template import java.util.Scanner; public class...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial Scores: 10, 20, 30, 40 Scores after loop: 30, 50, 70, 40 Import. java.util.Scanner; public class StudentScores { public static void main (String [] args){ Scanner scnr = new Scanner (System.in); final int SCORES_SIZE = 4; int [] bonusScores = new int[SCORES_SIZE]; int...
Write an expression that will cause "greater or equal to -10" to print if the value...
Write an expression that will cause "greater or equal to -10" to print if the value of userNum is greater than or equal to -10. import java.util.Scanner; public class EqualityAndRelational {    public static void main (String [] args) {       int userNum;       Scanner scnr = new Scanner(System.in);       userNum = scnr.nextInt(); // Program will be tested with values: -9, -10, -11, -12.       if (//solution goes here//) {          System.out.println("greater or equal to -10");       }       else...
Write an expression that will cause "Foot or more" to print if the value of numInches...
Write an expression that will cause "Foot or more" to print if the value of numInches is greater than or equal to 12. import java.util.Scanner; public class EqualityAndRelational {    public static void main (String [] args) {       int numInches;       Scanner scnr = new Scanner(System.in);       numInches = scnr.nextInt(); // Program will be tested with values: 10, 11, 12, 13.       if (/* Your solution goes here */) {          System.out.println("Foot or more");       }       else {...
Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total...
Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8. Java COde: import java.util.Scanner; public class SumOfExcess { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS = 4; int[] testGrades...
in JAVA Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If...
in JAVA Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If 'b' or 'B', print "Beta". For any other character, print "Unknown". Use fall-through as appropriate. End with newline. import java.util.Scanner; public class ConvertToGreek { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); char origLetter; origLetter = scnr.next().charAt(0); /* Your solution goes here */ } }
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
In Java ReWrite the for loop program code example 3, above as a while loop. Code...
In Java ReWrite the for loop program code example 3, above as a while loop. Code Example 3 – Try it import java.util.Scanner; public static void main(String args[]) { int count = 0; int maxNumber = 0; int enteredNumber = -9999999; System.out.println("This program determines which entered number is the largest "); System.out.println("Enter count of numbers to check: "); Scanner scanIn = new Scanner(System.in); double count = scanIn.nextDouble(); scanIn.close(); // Print out message – input number – compare to see if...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT