Back to Problem Solutions forum
Hi, I have a problem with task 9 in Java.
import java.util.Scanner;
public class Solutuion
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int counter = input.nextInt();
int arr[] = new int[3];
while(counter>0)
{
for(int x:arr)
{
x = input.nextInt();
System.out.print(x + " ");
}
System.out.print("| ");
for(int y:arr)
{
System.out.print(y + " ");
}
System.out.print("||| ");
counter--;
}
}
}
This code results in output looking something like this: 449 996 822 | 0 0 0 ||| 1397 855 1885 | 0 0 0 ... My question is why my array gets zeroe'd? What am I doing wrong? Am I stupid?:(
Marcin, in the loop:
for(int x:arr)
{
x = input.nextInt();
System.out.print(x + " ");
}
variable x is set to the input value. Nothing is assigned to arr so in the second loop the initial array values of 0 are printed.
Hope that helps.
Thanks, that helps. I got confused how this enhanced loop works.