java取数组最大值最小值

编程入门 行业动态 更新时间:2024-10-09 17:21:20

java取数组<a href=https://www.elefans.com/category/jswz/34/1768932.html style=最大值最小值"/>

java取数组最大值最小值

参见英文答案 >

Finding the max/min value in an array of primitives using Java                                    15个

这是我的代码.我需要得到我的数组的最小值,最大值,以便能够得到范围,每当我输入数字时,最小值为0.请帮助我.谢谢:)

final AutoCompleteTextView inputValues = (AutoCompleteTextView) findViewById(R.id.txt_input);

final TextView txtMinimum = (TextView) findViewById(R.id.txtMinimum);

final TextView txtMaximum = (TextView) findViewById(R.id.txtMaximum);

final TextView txtRange = (TextView) findViewById(R.id.txtRange);

Button btncalculate = (Button)findViewById(R.id.btncalculate);

btncalculate.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

String []values = ( inputValues.getText().toString().split(","));

int[] convertedValues = new int[values.length];

// calculate for the minimum and maximum number

int min = 0;

int max=0;

min = max = convertedValues[0];

for (int i = 0; i < convertedValues.length; ++i) {

convertedValues[i] =Integer.parseInt(values[i]);

min = Math.min(min, convertedValues[i]);

max = Math.max(max, convertedValues[i]);

}

txtMinimum.setText(Integer.toString(min));

txtMaximum.setText(Integer.toString(max));

// calculate for the range

int range=max - min;

txtRange.setText(Integer.toString(range));

}});

更多推荐

java取数组最大值最小值

本文发布于:2024-03-04 14:09:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1709476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   数组   最小值   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!