I am wring a program that askes the user to give any value they want as long as it is a whole number, and that value will be placed in a array. They can do this asmany times as they would like, until they add -1 to the array. I am having a hard time figurig out how to get the loop to end. My code:
int i = 0; while (i != -1) { System.out.print("Enter a positive whole number, enter '-1' to stop:"); s = scan.next(); if (i ! = -1) intElements.add(i);
I think the issue is in my if conditional. Not sure what the correct way to write out the logic would be.
Advertisement
Answer
Just change:
s = scan.next();
To:
i = scan.nextInt();
If you only want POSITIVE ints to be added, then also change your if
statement to:
if (i > 0)