+ 1

Im too stupid pls help

import java.util.Scanner; public class Program { public static void main(String[] args) { int[] a = new int[5]; for(int b: a) { a[b] = input.nextInt(); } int d = 0; for (int c: a) { if (a[c]%2==0) { d += a[c]; } } System.out.println(d); } } //some issue in java at line 9 (I think it was and Im too bad to find it and correct it Btw Im new to this forum soooo I dont reall know how it works

1st Feb 2020, 8:58 AM
Hanz Fenster
Hanz Fenster - avatar
8 Respuestas
+ 4
Hanz Fenster Make use of the basic for loop in your program because it will suit your code more. The for-each loop in Java is basically used for traversing the elements in an array or a collection. You cannot use it to assign values to the array that you have declared. Also there are a lot of errors and I recommend you to revise the course or topics again to understand what you actually trying to do.
1st Feb 2020, 9:05 AM
Avinesh
Avinesh - avatar
+ 2
First: You miss the Scanner input declaration Scanner input = new Scanner(System.in); Edit: Second What Avinesh said.
1st Feb 2020, 9:05 AM
Mihai Apostol
Mihai Apostol - avatar
+ 1
Why b>5 and c>5 ? It should be b<5 and c<5.
1st Feb 2020, 9:17 AM
Avinesh
Avinesh - avatar
+ 1
Thank you I still didnt get to the outcome I wanted to But you helped me a lot And if you enter the right numbers its even working now! xD
1st Feb 2020, 9:35 AM
Hanz Fenster
Hanz Fenster - avatar
0
Now Im always getting the output 0 import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input =new Scanner(System.in); int[] a = new int[5]; for(int b=0;b>5;b++) { a[b] = input.nextInt(); } int d = 0; for (int c=0;c>5;c++) { if (a[c]%2 == 0) { d += a[c]; } } System.out.println(d); } }
1st Feb 2020, 9:13 AM
Hanz Fenster
Hanz Fenster - avatar
0
Oops Ty
1st Feb 2020, 9:17 AM
Hanz Fenster
Hanz Fenster - avatar
0
I think you should rename your variable. What does a, b, c, d mean?
2nd Feb 2020, 4:35 PM
Lâm Kim Phú
Lâm Kim Phú - avatar
0
It was just for trying
2nd Feb 2020, 4:54 PM
Hanz Fenster
Hanz Fenster - avatar