Java 从键入的BufferedReader中查找arrayList中最大和最小的元素
我不知道我为什么不把最小的数字记下来。如果你试着说:129,2,3,那么这个大号是正确的Java 从键入的BufferedReader中查找arrayList中最大和最小的元素,java,arrays,bufferedreader,Java,Arrays,Bufferedreader,我不知道我为什么不把最小的数字记下来。如果你试着说:129,2,3,那么这个大号是正确的 public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int list[] = new int[3]; int min = list[0]; int ma
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int list[] = new int[3];
int min = list[0];
int max = list[0];
int input;
for (input = 0; input < list.length; input++) {
String s = reader.readLine();
list[input] = Integer.parseInt(s);
if (list[input] < min) {
min = list[input];
} else if (list[input] > max) {
max = list[input];
}
}
System.out.println("Smallet nummber: " + min);
System.out.println("Biggest nummber: " + max);
}
publicstaticvoidmain(字符串[]args)引发IOException{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
整数列表[]=新整数[3];
int min=列表[0];
int max=列表[0];
int输入;
用于(输入=0;输入最大值){
最大值=列表[输入];
}
}
System.out.println(“最小值:+min”);
System.out.println(“最大数量:+max”);
}
只需如下初始化最小值和最大值:
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int[] list = new int[3];
同时将条件部分更改为:
if (list[input] < min) {
min = list[input];
}
if (list[input] > max) {
max = list[input];
}
if(列表[输入]最大值){
最大值=列表[输入];
}
使用列表[0]初始化时,最小值和最大值都将使用0初始化。现在,如果输入值小于0,则只更新min值。否则,将永远不会更新最小值 只需像这样初始化最小值和最大值:
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int[] list = new int[3];
同时将条件部分更改为:
if (list[input] < min) {
min = list[input];
}
if (list[input] > max) {
max = list[input];
}
if(列表[输入]最大值){
最大值=列表[输入];
}
使用列表[0]初始化时,最小值和最大值都将使用0初始化。现在,如果输入值小于0,则只更新min值。否则,将永远不会更新最小值 min为0且始终小于列表[输入]。本案:
if (list[input] < min) {
if(列表[输入]
永远不会发生。
在arrayList中使用负数;)最小值为0,且始终小于列表[Input]。大小写为:
if (list[input] < min) {
if(列表[输入]
永远不会发生。
在数组列表中使用负数;)初始化如下数组时:
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int[] list = new int[3];
其所有项目均为0
因此,在初始化最小值和最大值时:
int min = list[0];
int max = list[0];
您分配给两个0
如果您只想保留一个循环来获取输入和
min
和max
,您可以这样做:
if (input == 0) {
min = list[input];
max = list[input];
} else if (list[input] < min) {
min = list[input];
} else if (list[input] > max) {
max = list[input];
}
if(输入==0){
最小值=列表[输入];
最大值=列表[输入];
}否则如果(列出[输入]最大值){
最大值=列表[输入];
}
初始化如下数组时:
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int[] list = new int[3];
其所有项目均为0
因此,在初始化最小值和最大值时:
int min = list[0];
int max = list[0];
您分配给两个0
如果您只想保留一个循环来获取输入和
min
和max
,您可以这样做:
if (input == 0) {
min = list[input];
max = list[input];
} else if (list[input] < min) {
min = list[input];
} else if (list[input] > max) {
max = list[input];
}
if(输入==0){
最小值=列表[输入];
最大值=列表[输入];
}否则如果(列出[输入]最大值){
最大值=列表[输入];
}
@tushar\u lokare OP可能使用或不使用的IDE与此完全无关。@tushar\u lokare OP可能使用或不使用的IDE与此完全无关。感谢您的进一步解释。感谢您的进一步解释。