public void counterLoop() {
Scanner userNumber = new Scanner(System.in);
ArrayList<Integer> number = new ArrayList<>();
while (true) {
System.out.println("Enter a number: ");
int n = userNumber.nextInt();
if(n >= 0)
number.add(n);
else break;
}
}
You should add the number to the list iff it is >= 0. If a negative number is input, the loop should break. You can print your list to see the result,
for(int i : number)
System.out.print(i + " ");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…