2013年9月28日星期六

Array reverse output

I just started to learn java, please help look randomly generated integers less than 10 100 , respectively, according to their positive sequence and reverse sequence output
------ Solution ------------ --------------------------------
int []arr=new int[10];
System.out.println("随机数的顺序是:");
for(int a = 0 ;a<10;a++){
int random = (int) (Math.random()*100);
arr[a]=random;
System.out.print(" "+random);
}
System.out.println();
System.out.println("正序是");
for(int b :arr){
System.out.print(" "+b);
}
System.out.println();
System.out.println("逆序是:");
for(int c = 9;c>=0;c--){
System.out.print(" "+arr[c]);
}

------ Solution - ------------------------------------------
public class Test {

public static void main(String[] args) {
int a[] = new int[10];

for(int i=0;i<a.length;i++){
a[i] = (int)(Math.random()*100);
}

Arrays.sort(a);
//升序
System.out.println("升序");
for(int i=0;i<a.length;i++){
System.out.print(a[i] + " ");
}
System.out.println();

//降序
System.out.println("降序");
for(int i=a.length-1;i>=0;i--){
System.out.print(a[i] + " ");
}
System.out.println();
}
}

Arrays.sort (a); own sorting algorithm in Java , you can also write your own sorting algorithm to sort
Math.random (); is a randomly generated number between 0.0 and 1.0 * 100 is a number between 0 and 100 a
------ Solution ------- -------------------------------------
for (int i = 0; i<arr.length;i++){
 arr[i] = (int)(Math.random() * 100);
 }
 
 System.out.println(Arrays.toString(arr));

reverse method and two upstairs same, do not write
------ For reference only ------------------- --------------------
upstairs can !
------ For reference only -------------------------------------- -
ArrayList<Integer> list = new ArrayList<>(100);
ThreadLocalRandom generator = ThreadLocalRandom.current();
for(int i = 0; i < 10; ++i){
    list.add(generator.nextInt(0,100)); //  [0, 100)
}
Collections.sort(list);
System.out.println(list);
Collections.reverse(list);
System.out.println(list):

------ For reference only ------------------------------- --------
let you say
------ For reference only ---------------------- -----------------

Mmm
------ For reference only ------------ ---------------------------

------ For reference only ------------------- --------------------
Thank you , too thanked

没有评论:

发表评论