With ArrayLists (Cleanest version):
ArrayList<Integer> itemAmounts = new ArrayList<Integer>();
int c = 1;
int x = 0;
while(x!=-1){
System.out.print("Enter the number of Item "+c+" you would like to purchase: ");
int x = input.nextInt();
itemAmounts.add(x);
c++;
}
With Arrays (limited to exactly four items)
final int NUMB_ITEMS = 4;
int[] itemAmounts = new int[NUMB_ITEMS];
int c = 0;
int x = 0;
while(x!=-1 && c < NUMB_ITEMS){
System.out.print("Enter the number of Item "+(c+1)+" you would like to purchase: ");
int x = input.nextInt();
itemAmounts[c] = x;
c++;
}
With no data structure whatsoever:
int item1Amount;
int item2Amount;
int item3Amount;
int item4Amount;
int c = 1;
int a = 0;
while(x!=-1 && c<= 4){
System.out.print("Enter the number of Item "+c+" you would like to purchase: ");
a=input.nextInt();
switch(c){
case 1:
item1Amount = a;
break;
case 2:
item2Amount = a;
break;
case 3:
item3Amount = a;
break;
case 4:
item4Amount = a;
break;
default:
break;
}
c++;
}
With no data structure whatsoever AND no switches (ughhh... Chapter 4 land sucks):
int item1Amount;
int item2Amount;
int item3Amount;
int item4Amount;
int c = 1;
int a = 0;
while(x!=-1 && c<= 4){
System.out.print("Enter the number of Item "+c+" you would like to purchase: ");
a=input.nextInt();
if(c == 1){
item1Amount = a;
} else if(c == 2){
item2Amount = a;
} else if (c == 3){
item3Amount = a;
} else if (c == 4) {
item4Amount = a;
}
c++;
}
switch. It will work but kinda ridiculous.