Warning: fopen(/www/wwwroot/www.wendadaohang.com/data/md5_content_title/80/80b70978f827a2a7c33164780b08bf8c.txt): failed to open stream: No space left on device in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2468

Warning: flock() expects parameter 1 to be resource, bool given in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2469

Warning: fclose() expects parameter 1 to be resource, bool given in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2475
java题目:创建一个一维数组,每个元素的值是100-999之间的随机整数.输出该数组的元素,求帮忙,急 - 00问答网

java题目:创建一个一维数组,每个元素的值是100-999之间的随机整数.输出该数组的元素,求帮忙,急

创建一个一维数组,每个元素的值是100-999之间的随机整数。输出该数组的元素,要求每行10个元素;输出数组中元素的最大值和下标。

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Date;


public class Solution {
int[] rlist;
Random r ;
Integer max = null;
int length;
int cursor;

public Solution(int length) {
this.length = length;
}

public int getLength() {
return length;
}

public void setLength(int length) {
this.length = length;
}

private int getRnum(){
return r.nextInt(900) + 100;
}

private void checkIfMax(){
max = cursor == 0 ? 0 : ( rlist[max] >= rlist[cursor] ? max : cursor );
}

public void run(){
rlist = new int[length];
max = null;
r = new Random((new Date()).hashCode());
for(cursor = 0; cursor < length; cursor++){
rlist[cursor] = getRnum();
checkIfMax();
System.out.print(rlist[cursor]);
if(cursor == length - 1){
System.out.print("\n");
}else if((cursor+1)%5 == 0){
System.out.print(",\n");
}else{
System.out.print(", ");
}
}
System.out.println("MAX: " + rlist[max] + ", Location: " + max);
}

public static void main(String[] args) {
Solution s = new Solution(123);
s.run();
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-20
随便写了下将就看看

public class Test{

private static void function6() {
int[] a=new int[10];
int b;
for(int i=0;i<a.length;i++){
c:while(true){
b=(int)(Math.random()*1000);
if(b>=100){
a[i]=b;
break;
}else{
continue c;
}
}
}
int max=a[0];
List<Integer> list=new ArrayList<Integer>();

for(int i=0;i<a.length;i++){
System.out.print(a[i]+"--");
if(a[i]>max){
max=a[i];
}
list.add(a[i]);
}
int i=list.indexOf(max);
System.out.println();
System.out.println("最大值:"+max);
System.out.println("下标:"+i);
}本回答被网友采纳