【发布时间】:2023-03-05 06:02:01
【问题描述】:
我正在为课堂开发一个程序。
我需要使用扫描器类读取用户的输入。
然后,我需要使用 while 循环将这些数据传递给数组列表。
在我的 while 循环中,我需要一个条件语句来检查输入的是 0 还是负数。
如果用户输入负数或 0,则循环结束,代码进入下一个进程...
我目前面临的问题是:
-我的所有输入值都没有被处理
-我必须输入值0 3次才能退出循环
-0 正在传递给我不想要的数组列表
到目前为止,这是我的代码:
import java.util.*;
public class questionAvg3
{
public static void main(String[]args)
{
Scanner input_into = new Scanner(System.in);
ArrayList<Integer> collector = new ArrayList<Integer>();
System.out.println("Enter 0 or a negative number to end input");
System.out.println("Enter a positive integer to populate the arraylist");
while ((input_into.nextInt() !=0) || (input_into.nextInt() < 0)){
System.out.println("Type another int or exit");
collector.add(input_into.nextInt());
}
int minValue = collector.get(0);
int maxValue = collector.get(0);
//int avgValue = collector.get(0);
//int total = 0;
for(Integer i: collector){
if( i < minValue) minValue = i;
if( i > maxValue) maxValue = i;
}
System.out.println("The max value int is: " + maxValue);
System.out.println("The min value int is: " + minValue);
}
}
【问题讨论】:
标签: java arrays conditional