0

This is what I tried, but it doesn't work as I expected.

package javaCourseNumberArray.copy;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub 
        Scanner sc = new Scanner(System.in);
        int[] a = new int[5]; 
        
        System.out.println("6 numbers quick... i don't like big ones btw.");
        
        int x ;
        int y = 0;
        
        while (y < 5) {
            x = sc.nextInt();
            if(x>10) {
                a[y] = (int)x;
                y++;
            }
            else {
                System.out.println("Pick a number beetween 0 and 9");
            }
            
    
        }
        for (int m : a) {
        System.out.println(a[m]);
        }
    }

}
andrewJames
  • 19,570
  • 8
  • 19
  • 51
MaCha
  • 1
  • 1
  • First fix this the print statement. m is not the index. So you need to do System.out.prinln(m); As to the rest, I'm not sure what you're trying to do. – Stephan Branczyk Jul 27 '22 at 00:39
  • *but it doesn't work as I expected.* - what do you expect and what is the actual? – Scary Wombat Jul 27 '22 at 00:43
  • Possibly related: [Validating input using java.util.Scanner](https://stackoverflow.com/q/3059333), [How to use Scanner to accept only valid int as input](https://stackoverflow.com/q/2912817) – Pshemo Jul 27 '22 at 00:44
  • Please look at this tutorial on for-each loops. It should clear up your misunderstanding. https://www.programiz.com/java-programming/enhanced-for-loop Again, m is the element itself, it's not the index. – Stephan Branczyk Jul 27 '22 at 00:49
  • Thx ,for the first fix ,i didn't realize. I expected that the if statement could restrict the kind of answers from the user i can store in the array, like 'if the answer is >10 it'll be saved in the array',but instead it didnt work at all,and just end up in a infinite loop. – MaCha Jul 27 '22 at 04:18

0 Answers0