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;
}
}
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
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.