首先让计算机随机产生出10个两位正整数,然后按照从小到大的顺序显示出来
java 编写 多谢
1、源代码
package BaiDdu;import java.util.*;
public class test_2_24
{
public static void main(String[] args) {
int num[]=new int[10];
Random random=new Random();
for (int i = 0; i < 10; i++) {
int n=random.nextInt(100);
while(n<10||n>99) //判断是不是两位数
n=random.nextInt(100);//不是就重新生成
num[i]=n;//放进数组里
}
System.out.print("生成数组:");
for (int i : num) {
System.out.print(i+" ");
}
Arrays.sort(num);//数组排序
System.out.print("排序后:");
for (int i : num) {
System.out.print(i+" ");
}
}
}
2、运行效果
温馨提示:答案为网友推荐,仅供参考
第1个回答 2017-06-27
/**
* 产生10 个 2位数的正整数,可能有重复的数据.
*
*
* @return
*/
public static int[] randomArray() {
int[] array = new int[10];
int randomNum;
Random random = new Random();
for (int i = 0; i < array.length; i++) {
randomNum = random.nextInt(90) + 10;
array[i] = randomNum;
}
return array;
}
//排序的话,可以使用:Arrays.sort()
//或者使用其他方法,提供一种
/**
* 排序,冒泡排序.
*
* @param array
*/
public static void sort(int[] array) {
int temp;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
* 产生10 个 2位数的正整数,可能有重复的数据.
*
*
* @return
*/
public static int[] randomArray() {
int[] array = new int[10];
int randomNum;
Random random = new Random();
for (int i = 0; i < array.length; i++) {
randomNum = random.nextInt(90) + 10;
array[i] = randomNum;
}
return array;
}
//排序的话,可以使用:Arrays.sort()
//或者使用其他方法,提供一种
/**
* 排序,冒泡排序.
*
* @param array
*/
public static void sort(int[] array) {
int temp;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
第2个回答 2017-06-27
import java.util.Arrays;
import java.util.Random;
public class Sort {
public static void main(String[] args) {
int [] a = new int[10];
Random random = new Random();
for(int i = 0; i < 10; i++) {
a[i] = random.nextInt(90) +10;
}
for(int i = 0; i < 10; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
Arrays.sort(a);
for(int i = 0; i < 10; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
import java.util.Random;
public class Sort {
public static void main(String[] args) {
int [] a = new int[10];
Random random = new Random();
for(int i = 0; i < 10; i++) {
a[i] = random.nextInt(90) +10;
}
for(int i = 0; i < 10; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
Arrays.sort(a);
for(int i = 0; i < 10; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
第3个回答 2017-06-27
Random random = new Random();
int max = 99, min = 10;
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(random.nextInt(max - min + 1) + min);
}
System.out.println(list.toString());
Collections.sort(list);
System.out.println(list.toString());
int max = 99, min = 10;
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(random.nextInt(max - min + 1) + min);
}
System.out.println(list.toString());
Collections.sort(list);
System.out.println(list.toString());
第4个回答 2017-06-27
public static void main(String[] args){
List<Comparable> al = new ArrayList<Comparable>();
for (int i = 0; i < 10; i++) {
al.add((int)(Math.random()*90)+10);
}
Collections.sort(al);//对数组排序
System.out.println(al);
}
List<Comparable> al = new ArrayList<Comparable>();
for (int i = 0; i < 10; i++) {
al.add((int)(Math.random()*90)+10);
}
Collections.sort(al);//对数组排序
System.out.println(al);
}