Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read i

ID: 3789026 • Letter: W

Question

Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using Scanner. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.

Something is wrong with my code..Please help

import java.util.Scanner;

public class StoreGuesses {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_GUESSES = 3;
int[] userGuesses = new int[NUM_GUESSES];
int i = 0;

/* Your solution goes here */
for (i = 0; i < NUM_GUESSES; ++i){
userGuesses[i] = scnr.nextInt();

}

for (i = 0; i < NUM_GUESSES; ++i){
System.out.print(userGuesses[i] + " ");

}

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

return;
}
}

Challenge 7.4.3: Populating an array with a for loop Activity Write a for loop to populate array userGuesses with NUM GUESSES integers. Read integers using Scanner. Ex If NUM GUESSES is 3 and user enters 9 5 2, then userGuesses is 19, 5, 2) 1 import java til. Sca nner 3 public class Store Guesses 4 public static void main (string args) Scanner SCnr new Scanner (System in final int NUM GUESSES int[] user Guesses new int[NUM. GUESSES] int 0; Your solution goes here 10 for (i 03 i NUM GUESSES ++i){ 11 user Guesses [i] scnr.nextInt 12 13 14 15 for (i 0; i NUM GUESSES ++i) 16 System.out print (u 17 18 19 for (i 0; i NUM GUESSES ++i)t 20 System Ou print (userGuesses[i] 21 Run X Testing for 12, 4, 6) 2 4 6 Expected output Your output 2 4 6 2 4 6 Tests aborted. Feedback?

Explanation / Answer

Hi, You are printing array two times.

I have fixed the issue.

import java.util.Scanner;

public class StoreGuesses {

   public static void main (String [] args) {

       Scanner scnr = new Scanner(System.in);

       final int NUM_GUESSES = 3;

       int[] userGuesses = new int[NUM_GUESSES];

       int i = 0;

       /* Your solution goes here */

       for (i = 0; i < NUM_GUESSES; ++i){

           userGuesses[i] = scnr.nextInt();

       }

       for (i = 0; i < NUM_GUESSES; ++i){

           System.out.print(userGuesses[i] + " ");

       }

   }

}

/*

2 4 6

2 4 6

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote