码迷,mamicode.com
首页 > 编程语言 > 详细

04_数组

时间:2020-07-07 10:16:26      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:false   冒泡排序   字符   i+1   自动生成   print   public   void   stat   

 1 /**
 2  * 冒泡排序
 3  * @author jliu.l
 4  * @2020年7月6日
 5  * 
 6  */
 7 public class Demo01 {
 8 
 9     /**
10      * @param args
11      */
12     public static void main(String[] args) {
13         // TODO 自动生成的方法存根
14         int[] arr = {2,5,8,3,6,7};
15         System.out.print("排序前:");
16         for (int i : arr) {
17             System.out.print(i+" ");
18         }
19         System.out.println();
20 
21         for (int i = 0; i < arr.length; i++) {
22             boolean flag = true;
23             for (int j = 0; j < arr.length-1-i; j++) {
24                 if(arr[j]>arr[j+1]) {
25                     int temp = arr[j];
26                     arr[j] = arr[j+1];
27                     arr[j+1] = temp;
28                     
29                     flag = false;
30                 }
31             }
32             if (flag) {
33                 break;
34             }    
35         }
36         System.out.print("排序后:");
37         for (int i : arr) {
38             System.out.print(i+" ");
39         }
40     }
41 }

 

 1 /**
 2  * 功能:定义一个方法实现任意字符串反向输出功能,比如:“abcde”,结果为:“edcba”
 3  * @author jliu.l
 4  * @2020年7月6日
 5  * 
 6  */
 7 public class Demo02 {
 8     public static void main(String[] args) {
 9         String str = "abcdef";
10         Tools.reverser(str);
11         
12         int[] num = {1,6,5,10,2,0,9};
13         Tools.getIndex(num);
14     }
15 }
 1 /**
 2  * @author jliu.l
 3  * @2020年7月6日
 4  * 
 5  */
 6 public class Poker {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         // TODO 自动生成的方法存根
13         String[] num = {"2","3","4","5","6","7","8","9","10","J","Q","k","A"};
14         String[] str = {"♠","♥","♣","♦"};
15         
16         String[] poker = new String[str.length*num.length];
17         
18         //组合54张牌
19         poker = Tools.combinationPoker(str,num);
20         //Tools.printPoker(poker);
21         //打乱牌序
22         poker = Tools.breakPoker(poker);
23         
24         //玩家
25         String[] player = {"小鹿","小明","小杨"};
26         int flag = Tools.flag();
27         
28         //发牌
29         Tools.handoutPoker(poker,player,flag);
30     }
31 }
  1 /**
  2  * @author jliu.l
  3  * @2020年7月6日
  4  * 
  5  */
  6 public class Tools {
  7     /**
  8       * 实现任意字符串反向输出功能
  9      * @param str 
 10      * 
 11      */
 12     public static void reverser(String str) {
 13         // TODO 自动生成的方法存根
 14         System.out.println("反向输出前:"+str);
 15         String  str1 = "";
 16         for (int i = 0; i < str.length(); i++) {
 17             str1 = str.charAt(i) + str1;
 18         }
 19         System.out.println("反向输出后:"+str1);
 20     }
 21     
 22     /*j = 0        index = 1
 23      * 1    1
 24      * 2    3
 25      * 3 4
 26      * 
 27      * 
 28       * 定义一个整数数组,找到其中的最大值和最小值的索引并输出
 29      */
 30     public static void getIndex(int[] num) {
 31         int max = num[0];
 32         int min = num[0];
 33         int maxdex = 0;
 34         int mindex = 0;
 35         for (int j=0; j < num.length-1; j++) {
 36             if(max < num[j+1]) {
 37                 max = num[j+1];
 38                 maxdex = j+1;
 39             }
 40             
 41             if(min > num[j+1]) {
 42                 min= num[j+1];
 43                 mindex = j+1;
 44             }
 45         }
 46         System.out.println(Arrays.toString(num)+"中最大值"+max+"的索引是:"+(maxdex));
 47         System.out.println(Arrays.toString(num)+"中最小值"+min+"的索引是:"+(mindex));
 48     }
 49     
 50     //binarySearch 二分查找
 51     public static void binarySearch() {
 52         int[] arr = {1,3,5,9,6,7,8};
 53         
 54         if(Arrays.binarySearch(arr,3) >= 0) {
 55             System.out.println("找到了");
 56         }else {
 57             System.out.println("没找到");
 58         }
 59     }
 60     
 61     /**
 62      * 组合牌
 63      * alt+shift+J
 64      * @param str
 65      * @param num
 66      */
 67     public static String[] combinationPoker(String[] str, String[] num) {
 68         // TODO 自动生成的方法存根
 69         String[] poker = new String[str.length*num.length+2];
 70         int index = 0;
 71         poker[str.length*num.length] = "大王";
 72         poker[str.length*num.length+1] = "小王";
 73 
 74         for (int j = 0; j < num.length; j++) {
 75             for (int i = 0; i < str.length; i++) {
 76             poker[index++] = str[i]+num[j];
 77             }
 78         }
 79         
 80         System.out.println("组合牌:");
 81         printPoker(poker);
 82         return poker;
 83     }
 84 
 85     /**
 86      * 输出牌
 87      * @param poker
 88      */
 89     public static void printPoker(String[] poker) {
 90         for (int i = 0; i < poker.length; i++) {
 91             System.out.print(poker[i]+" ");
 92             if((i+1)%6 ==0 ) {
 93             System.out.println();
 94             }
 95         }
 96         System.out.println();
 97         System.out.println("----------------------------");
 98     }
 99 
100     
101     
102     /**
103      * 打乱牌序
104      * @param str
105      * @return
106      */
107     public static String[] breakPoker(String[] str) {
108         String[] poker = str;
109         for (int i = 0; i < poker.length; i++) {
110             int index = (int) (Math.random()*54);
111             int index1 = (int) (Math.random()*54);
112             if(index != index1) {
113                 String temp = poker[index];
114                 poker[index] = poker[index1];
115                 poker[index1] = temp;
116             }
117         }
118         
119         System.out.println("打散后的牌:");
120         printPoker(poker);
121         return poker;
122     }
123     
124     public static int flag() {
125         int index = (int) (Math.random()*3+1);
126         int flag = 0;
127         if(index == 1) {
128             flag = 1;
129         }
130         if(index == 2) {
131             flag = 2;
132         }
133         if(index == 3) {
134             flag = 3;
135         }
136         return flag;
137     }
138     
139 
140     
141     /**
142      * 输出地主多余的3张牌
143      * @param str
144      */
145     public static void printDz(String[] str) {
146         System.out.print(str[str.length-3]+" "+str[str.length-2]+" "+str[str.length-1]);
147     }
148     
149     /**
150      * 发牌(地主多三张)
151      * @param str
152      * @param player
153      */
154     public static void handoutPoker(String[] str,String[] player,int flag) {
155         
156         //String[] player1Poker = new String[(str.length-3)/3];
157         
158         if(flag == 1) {
159             System.out.print("地主"+player[0]+"的牌是:");
160         }else {
161             System.out.print(player[0]+"的牌是:");
162             }
163         for (int i = 0; i < str.length-3; i++) {
164             
165             if(i%3==0) {
166                 System.out.print(str[i]+" ");
167             }
168         }
169         if(flag == 1) {
170             printDz(str);
171         }
172     
173         
174         System.out.println();
175         if(flag == 2) {
176             System.out.print("地主"+player[1]+"的牌是:");
177         }else {
178             System.out.print(player[1]+"的牌是:");
179             }
180         for (int i = 0; i < str.length-3; i++) {
181             
182             if(i%3==01) {
183                 System.out.print(str[i]+" ");
184             }
185         }
186         if(flag == 2) {
187             printDz(str);
188         }
189         
190         
191         System.out.println();
192         if(flag == 3) {
193             System.out.print("地主"+player[2]+"的牌是:");
194         }else {
195             System.out.print(player[2]+"的牌是:");
196             }
197         for (int i = 0; i < str.length-3; i++) {
198             
199             if(i%3==2) {
200                 System.out.print(str[i]+" ");
201             }
202         }
203         if(flag == 3) {
204             printDz(str);
205         }
206     }
207 }

 

/** * @author jliu.l * @2020年7月6日 *  */public class Tools {/**  * 实现任意字符串反向输出功能 * @param str  *  */public static void reverser(String str) {// TODO 自动生成的方法存根System.out.println("反向输出前:"+str);String  str1 = "";for (int i = 0; i < str.length(); i++) {str1 = str.charAt(i) + str1;}System.out.println("反向输出后:"+str1);}/*j = 0index = 1 * 11 * 23 * 3 4 *  *   * 定义一个整数数组,找到其中的最大值和最小值的索引并输出 */public static void getIndex(int[] num) {int max = num[0];int min = num[0];int maxdex = 0;int mindex = 0;for (int j=0; j < num.length-1; j++) {if(max < num[j+1]) {max = num[j+1];maxdex = j+1;}if(min > num[j+1]) {min= num[j+1];mindex = j+1;}}System.out.println(Arrays.toString(num)+"中最大值"+max+"的索引是:"+(maxdex));System.out.println(Arrays.toString(num)+"中最小值"+min+"的索引是:"+(mindex));}//binarySearch 二分查找public static void binarySearch() {int[] arr = {1,3,5,9,6,7,8};if(Arrays.binarySearch(arr,3) >= 0) {System.out.println("找到了");}else {System.out.println("没找到");}}/** * 组合牌 * alt+shift+J * @param str * @param num */public static String[] combinationPoker(String[] str, String[] num) {// TODO 自动生成的方法存根String[] poker = new String[str.length*num.length+2];int index = 0;poker[str.length*num.length] = "大王";poker[str.length*num.length+1] = "小王";
for (int j = 0; j < num.length; j++) {for (int i = 0; i < str.length; i++) {poker[index++] = str[i]+num[j];}}System.out.println("组合牌:");printPoker(poker);return poker;}
/** * 输出牌 * @param poker */public static void printPoker(String[] poker) {for (int i = 0; i < poker.length; i++) {System.out.print(poker[i]+" ");if((i+1)%6 ==0 ) {System.out.println();}}System.out.println();System.out.println("----------------------------");}
/** * 打乱牌序 * @param str * @return */public static String[] breakPoker(String[] str) {String[] poker = str;for (int i = 0; i < poker.length; i++) {int index = (int) (Math.random()*54);int index1 = (int) (Math.random()*54);if(index != index1) {String temp = poker[index];poker[index] = poker[index1];poker[index1] = temp;}}System.out.println("打散后的牌:");printPoker(poker);return poker;}public static int flag() {int index = (int) (Math.random()*3+1);int flag = 0;if(index == 1) {flag = 1;}if(index == 2) {flag = 2;}if(index == 3) {flag = 3;}return flag;}
/** * 输出地主多余的3张牌 * @param str */public static void printDz(String[] str) {System.out.print(str[str.length-3]+" "+str[str.length-2]+" "+str[str.length-1]);}/** * 发牌(地主多三张) * @param str * @param player */public static void handoutPoker(String[] str,String[] player,int flag) {//String[] player1Poker = new String[(str.length-3)/3];if(flag == 1) {System.out.print("地主"+player[0]+"的牌是:");}else {System.out.print(player[0]+"的牌是:");}for (int i = 0; i < str.length-3; i++) {if(i%3==0) {System.out.print(str[i]+" ");}}if(flag == 1) {printDz(str);}System.out.println();if(flag == 2) {System.out.print("地主"+player[1]+"的牌是:");}else {System.out.print(player[1]+"的牌是:");}for (int i = 0; i < str.length-3; i++) {if(i%3==01) {System.out.print(str[i]+" ");}}if(flag == 2) {printDz(str);}System.out.println();if(flag == 3) {System.out.print("地主"+player[2]+"的牌是:");}else {System.out.print(player[2]+"的牌是:");}for (int i = 0; i < str.length-3; i++) {if(i%3==2) {System.out.print(str[i]+" ");}}if(flag == 3) {printDz(str);}}}

04_数组

标签:false   冒泡排序   字符   i+1   自动生成   print   public   void   stat   

原文地址:https://www.cnblogs.com/jliu-l/p/13258898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!