0

How do you input a list of numbers into a scanner for a loop to process them in many ways?

I am trying to get the scanner to allow me to input an undefined list of numbers but when I try to use an array, it throws an error saying that you can't use int [] with int. I have that same issue with the rest of the code as well, when I attempt to write in odd and even counts, averages....etc. Here is the first part that I am trying to use now. I have five variables listed but would like to be able to list an undefined amount if possible. Thank you. int [] x = new int [5]; int values == x; System.out.println("Enter a series of values (0 to quit):"); values = in.nextInt(); while (values != 0) { System.out.println(values);

1st Apr 2018, 4:16 AM
Mathew Henson
5 Respostas
+ 1
Thank you so much
1st Apr 2018, 5:08 AM
Mathew Henson
0
int [] x = new int [5]; for(int i = 0; i < x.length; i++){ Scanner in = new Scanner(System.in); System.out.println("Enter a series of values (0 to quit):"); int values = in.nextInt(); x[i] = values; } for(int y: x){ System.out.println("x: " + y); }
1st Apr 2018, 4:32 AM
Aidos Zhakupov
Aidos Zhakupov - avatar
0
The phone has, for example, Java N-IDE and AIDE. I write all the code there, and the data recording takes place in stages. And not as here all at once. For example, in the Java N-IDE asks 5 times when there will be data entry and there will be no errors in the code. Here it is not known initially how much data you need to enter
1st Apr 2018, 4:53 AM
Aidos Zhakupov
Aidos Zhakupov - avatar
0
I have one more question. Is there a way to be able to run the five numbers against each other with the later parameters? Right now when I run the program, it does each number individually and does not compare them to each other. This is what I have right now and am not sure what I need to do. int [] x = new int [5]; for(int i = 0; i < x.length; i++){ System.out.println("Enter a series of values (0 to quit):"); int values = in.nextInt(); x[i] = values; for(int y: x) System.out.println("x: " + y); int largestNum = values; if (values > largestNum) largestNum = values; System.out.println("The largest value is: " + largestNum); int smallestNum = values; if (values < smallestNum) smallestNum = values; System.out.println("The smallest value is: " + smallestNum); if (values % 2 == 0) evenNum ++; else oddNum ++; System.out.println(evenNum + "even" + " " + oddNum + "odd"); } } }
1st Apr 2018, 5:29 AM
Mathew Henson
0
I wrote a wonderful working version: public static void main(String args[]){ ArrayList<Integer> arr = new ArrayList<Integer> (); int start = 1; int var = 0; Scanner in = new Scanner(System.in); while(start<=5){ var = in.nextInt(); System.out.println("var: " + (start++) + " = "+ var); arr.add(var); } Random r = new Random(); if(arr.get(r.nextInt(5)) >= arr.get(r.nextInt(5))){ System.out.println(arr.get(r.nextInt(5)) + " >= " + arr.get(r.nextInt(5))); }else{ System.out.println(arr.get(r.nextInt(5)) + " <= " + arr.get(r.nextInt(5))); } }
1st Apr 2018, 6:19 AM
Aidos Zhakupov
Aidos Zhakupov - avatar