Java数组学习练习3冒泡排序数组代码

/**
* @author HP-Developer
* 8-31号的数组练习
* 实验任务
实验任务
将数组中的一组数据,从小到大依次输出

实验要求
数组必须是数字类型
随机定义一组数组
 */ 


package testof8_31;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author HP-Developer
 *  8-31号的数组练习
 *  实验任务
    实验任务
    将数组中的一组数据,从小到大依次输出

    实验要求
    数组必须是数字类型
    随机定义一组数组
     */
public class Test3_1{

    public static void main(String[] args){

        int [] num={2,3,456,78,32,6,8,4,87,345};

        int temp;
        for(int i=0;i<num.length;i++){
            for(int j=0;j<num.length-1;j++){
            if(num[j]>=num[j+1]){
                temp=num[j];
                num[j]=num[j+1];
                num[j+1]=temp;
            }
            }

        }
        for(int i:num){
            System.out.print(i+"\t");
        }
        System.out.print("\n");
        int [] num1={2,3,456,78,32,6,8,4,87,345};
            Arrays.sort(num1);
        for(int i:num1){
            System.out.print(i+"\t");
        }
        System.out.println("\n是否 转换顺序输出?(Y/N):");
        Scanner input=new Scanner(System.in);
        char choice=input.next().charAt(0);
        switch(choice){
        case 'Y':
            System.out.println("大小反序排列为:");
            for(int i=0;i<num.length;i++){
                for(int j=0;j<num.length-1;j++){
                if(num[j]<=num[j+1]){
                    temp=num[j];
                    num[j]=num[j+1];
                    num[j+1]=temp;
                }
                }

            }
            for(int i:num){
                System.out.print(i+"\t");
            }
            break;
        case 'N':
            System.out.println("排序结束!");
            break;
        }
    }
}
/*运行结果:
2   3   4   6   8   32  78  87  345 456 
2   3   4   6   8   32  78  87  345 456 
是否 转换顺序输出?(Y/N):
Y
大小反序排列为:
456 345 87  78  32  8   6   4   3   2   
*
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦思冥想行则将至

穷,有钱的大爷上个两分钱吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值