A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张雄 中级黑马   /  2016-4-10 13:27  /  718 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class MaoPaoDemo{
        public static void main(String[]args){
                int [] arr = {12,56,34,87,23,45};
               
                maoPao(arr);
                bianLi(arr);
        }
       
        public static void maoPao(int [] arr){
                for(int x = 0;x<arr.length-1;x++){
                        for (int y = 0 ;y<arr.length-1-x;y++){
                                if (arr[y]>arr[y+1]){
                                        int tem = arr[y];
                                        arr[y]= arr[y+1];
                                        arr[y+1]=tem;
                                }
                        }
                }
        }
       
        public static void bianLi(int [] arr){
                for (int x=0;x<arr.length;x++){
                        System.out.println(arr[x]);
                }
        }
}

5 个回复

倒序浏览
本帖最后由 grandsun 于 2016-4-10 13:46 编辑

恩,小伙子很棒...再送你一个打印星星的
import java.util.Scanner;
class 星星 {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int m = 10;                                //m没被传递到排列()方法中
                int n = 0;
               
               
                while (true){
                        n = sc.nextInt();                //确定几行星星0
                        排列(n);                        //确定传递参数为n。
                        continue;                       //执行一次就暂停,等待下一次录入行数后再执行打印星星。
                        }        
        }

        public static void  排列(int m) {
//      *                            第一行1个         2*行数-1       总行数-i(行数)
//     ***                           第二行3个                        总行数-i
//    *****                          第三行5个
//   *******                        第四行7个
//  *********                       第五行9个
// ***********                       第六行11个

        /*int n = 41;
                int num;
                num = 2*n-1;*/        

        for (int i = 1;i<=m ;i++ ){       //定义行数
                //定义每行的空格数
                for (int j = 1;j<=(m-i) ;j++ ){        
                        System.out.print(" ");
                }
                                for (int x = 1; x<=(2*i-1); x++){
                                        System.out.print("*");
                                }
                                                System.out.println("");
        }
               
        }

}
回复 使用道具 举报
再来一个
/*
        点击功能键1、查找数组元素;2、找出最大值;3、找出最小值;4、反转数组;5、求数组元素的和*/
import java.util.Scanner;

class ArrayWork {
        public static void main(String[] args) {
               
                int count = 1;                //用count来控制操作次数。
                System.out.println("请指定数组长度");
                Scanner sc = new Scanner(System.in);
                int n = 0;                                                        //变量n在fun4中被调用。所以先赋值。
                n = sc.nextInt();
                int[] arr = new int [n];                        //给出数组的长度
                System.out.println("请录入数组元素");

                for (int i = 0;i<= (n-1) ;i++ ){
                        arr[i] = sc.nextInt();                        //使用该循环将键盘录入的数字赋值进数组中
                        }

                while (true){                                //无限循环,可以一直选择操作。如果没有限制的话。

                System.out.println("请选择您要使用的功能:");
                System.out.println("1、遍历数组;2、反转数组;3、找出最大值;4、将数组中的元素求和");
                int x = sc.nextInt();

                //用switch语句来选择要操作的功能。
                switch (x){
                case 1 :
                        fun1(arr);      //功能1:遍历数组
                break;

                case 2 :
                        fun2(arr);                //功能2:反转数组
                break;

                case 3 :
                        fun3(arr);                //功能:找出最大值
                break;

                case 4 :
                        fun4(arr,n);                //功能:求和
                break;

                default :
                        System.out.println("请您录入正确的数字,谢谢。");
                break;
                        }
                count = count+1;
                if (count ==6){                        //限制操作次数,超过次数,就退出应用。
                System.out.println("---$$$$$$$$$$------$$$$$$$$$$$$$------");
                System.out.println("您的操作次数已超过限定的次数,请下次再试,谢谢您的使用。");
                        break;
                }
                System.out.println("");
                System.out.println("******************************");
                System.out.println("这是您的第"+count+"次操作");
                System.out.println("");
                }
        }
//遍历数组
        public static void fun1(int [] arr){
                for (int i = 0;i<arr.length ;i++ ){
                System.out.println("您创建的数组如下:"+arr[i]);
                }
        }
//反转数组
        public static void fun2(int [] arr){
                int temp = 0;
        for (int i = 0;i<arr.length/2 ;i++ ){
                temp = arr[i];
                arr[i] = arr[arr.length-1-i];
                arr[arr.length-1-i] = temp;               
                }
        for (int i = 0;i<arr.length ;i++ ){
                System.out.println("这是将您创建的数组反转之后的新的数组"+arr[i]);
                }
        }
//找出最大值
        public static void fun3(int [] arr){
                int max = arr[0];
                for (int i = 0;i<arr.length ;i++ ){
                        if (arr[i]>max){
                                max = arr[i];               
                        }
                }System.out.println("最大的数值是:"+max);
        }
//求和
        public static void fun4(int [] arr,int n) {
                int sum = 0;
                for (int i = 0; i<arr.length ;i++ ){
                        sum += arr[i];
                }
                System.out.println("您创建的数组共有"+n+"个数字,它们的和是:"+sum);
        }
}
回复 使用道具 举报
打印星星的觉得有点复杂
回复 使用道具 举报
Dencent 发表于 2016-4-10 15:48
打印星星的觉得有点复杂

不复杂啊
回复 使用道具 举报
不错不错
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马