如何结束java中整数收集数组的while循环?
我正在编写一个程序,要求用户提供他们想要的任何值,只要它是整数,并且该值将被放置在一个数组中。他们可以按照自己的意愿多次执行此操作,直到将 -1 添加到数组中。我很难弄清楚如何结束循环。 我的代码:
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);
我认为问题出在我的 if 条件中。不确定写出逻辑的正确方法是什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将:更改
为:
如果您只想添加正整数,请将
if
语句更改为:Just change:
To:
If you only want POSITIVE ints to be added, then also change your
if
statement to: