用JAVA程序编辑随机生成10个[100,1000]的整数 10
5个回答
展开全部
import java.util.Random;
public class Main {
public static void main(String[] args){
int [] num = randNum(10, 100, 1000);
//冒泡排序
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
int temp;
if (num[i] > num[j]) {
temp = num[j];
num[j] = num[i];
num[i] = temp;
}
}
}
//打印
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
}
/**
* 产生min - max 范围内的 num 个随机数
* @param num 要产生的随机数的个数
* @param min 下边界
* @param max 上边界
* @return int[] 产生的随机数组
*/
public static int[] randNum(int num ,int min ,int max){
int result[] = new int[num];
Random rand = new Random();
for(int i = 0;i<result.length;i++){
int rm=(rand.nextInt(max-min)+min);
result[i] = rm;
}
return result;
}
}
********打印出的降序结果***********
945 877 829 694 617 606 478 464 213 172
特点:
可以灵活的传入下边界、上边界和生成随机数的个数
希望采纳!
public class Main {
public static void main(String[] args){
int [] num = randNum(10, 100, 1000);
//冒泡排序
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
int temp;
if (num[i] > num[j]) {
temp = num[j];
num[j] = num[i];
num[i] = temp;
}
}
}
//打印
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
}
/**
* 产生min - max 范围内的 num 个随机数
* @param num 要产生的随机数的个数
* @param min 下边界
* @param max 上边界
* @return int[] 产生的随机数组
*/
public static int[] randNum(int num ,int min ,int max){
int result[] = new int[num];
Random rand = new Random();
for(int i = 0;i<result.length;i++){
int rm=(rand.nextInt(max-min)+min);
result[i] = rm;
}
return result;
}
}
********打印出的降序结果***********
945 877 829 694 617 606 478 464 213 172
特点:
可以灵活的传入下边界、上边界和生成随机数的个数
希望采纳!
展开全部
package test;
import java.util.ArrayList;
import java.util.Collections;
public class demo1 {
public static void main(String[] args) {
// 定义集合
ArrayList<Integer> list = new ArrayList<Integer>();
// 生成10个随机数
for (int i = 0; i < 10; i++) {
list.add((int) (Math.random() * 900 + 100));
}
// 排序
Collections.sort(list);
// 逆转集合,以为是按升序排的
Collections.reverse(list);
// 输出
for (Integer n : list) {
System.out.print(n + ",");
}
}
}
import java.util.ArrayList;
import java.util.Collections;
public class demo1 {
public static void main(String[] args) {
// 定义集合
ArrayList<Integer> list = new ArrayList<Integer>();
// 生成10个随机数
for (int i = 0; i < 10; i++) {
list.add((int) (Math.random() * 900 + 100));
}
// 排序
Collections.sort(list);
// 逆转集合,以为是按升序排的
Collections.reverse(list);
// 输出
for (Integer n : list) {
System.out.print(n + ",");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.ArrayList;
public class test {
public static void main(String[] args) {
//生成随机数
ArrayList<Integer> randomNums = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
randomNums.add((int)(Math.random() * 900 + 100));
}
//排序输出
for (int i = 0; i < 10; i++) {
Integer max = 0;
for (Integer num : randomNums) {
if (num > max) {
max = num;
}
}
System.out.println(max);
randomNums.remove(max);
}
}
}
public class test {
public static void main(String[] args) {
//生成随机数
ArrayList<Integer> randomNums = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
randomNums.add((int)(Math.random() * 900 + 100));
}
//排序输出
for (int i = 0; i < 10; i++) {
Integer max = 0;
for (Integer num : randomNums) {
if (num > max) {
max = num;
}
}
System.out.println(max);
randomNums.remove(max);
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Random;
public class RandomTest {
public static void main(String[] args) {
Random rand = new Random();
final int[] nums = new int[10];
for(int i = 0; i < nums.length; i++){
nums[i] = 100 + rand.nextInt(900);
}
System.out.println("Before sorrting, the array is: ");
printAry(nums);
//sort
for(int i = 0; i < nums.length; i++){
for(int j = i; j < nums.length; j++){
if(nums[j] > nums[i]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
System.out.println("Sortted array is: ");
printAry(nums);
}
public static void printAry(int[] ary){
for(int i = 0; i < ary.length; i++){
System.out.print(ary[i] + " ");
}
System.out.println();
}
}
---------测试
Before sorrting, the array is:
814 805 333 753 617 745 751 649 803 949
Sortted array is:
949 814 805 803 753 751 745 649 617 333
public class RandomTest {
public static void main(String[] args) {
Random rand = new Random();
final int[] nums = new int[10];
for(int i = 0; i < nums.length; i++){
nums[i] = 100 + rand.nextInt(900);
}
System.out.println("Before sorrting, the array is: ");
printAry(nums);
//sort
for(int i = 0; i < nums.length; i++){
for(int j = i; j < nums.length; j++){
if(nums[j] > nums[i]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
System.out.println("Sortted array is: ");
printAry(nums);
}
public static void printAry(int[] ary){
for(int i = 0; i < ary.length; i++){
System.out.print(ary[i] + " ");
}
System.out.println();
}
}
---------测试
Before sorrting, the array is:
814 805 333 753 617 745 751 649 803 949
Sortted array is:
949 814 805 803 753 751 745 649 617 333
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Random;
public class RandomTest {
public static void main(String[] args) {
Random rand = new Random();
final int[] nums = new int[10];
for(int i = 0; i < nums.length; i++){
nums[i] = 100 + rand.nextInt(900);
}
System.out.println("Before sorrting, the array is: ");
printAry(nums);
//sort
for(int i = 0; i < nums.length; i++){
for(int j = i; j < nums.length; j++){
if(nums[j] > nums[i]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
System.out.println("Sortted array is: ");
printAry(nums);
}
public static void printAry(int[] ary){
for(int i = 0; i < ary.length; i++){
System.out.print(ary[i] + " ");
}
System.out.println();
}
}
public class RandomTest {
public static void main(String[] args) {
Random rand = new Random();
final int[] nums = new int[10];
for(int i = 0; i < nums.length; i++){
nums[i] = 100 + rand.nextInt(900);
}
System.out.println("Before sorrting, the array is: ");
printAry(nums);
//sort
for(int i = 0; i < nums.length; i++){
for(int j = i; j < nums.length; j++){
if(nums[j] > nums[i]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
System.out.println("Sortted array is: ");
printAry(nums);
}
public static void printAry(int[] ary){
for(int i = 0; i < ary.length; i++){
System.out.print(ary[i] + " ");
}
System.out.println();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询