1 package com.lixu.caipiao; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 import android.widget.EditText; 9 10 public class MainActivity extends Activity implements OnClickListener { 11 Button btn1; 12 Button btn2; 13 Button btn3; 14 EditText et1; 15 EditText et2; 16 EditText et3; 17 StringBuffer sb; 18 StringBuffer sb2; 19 StringBuffer sb3; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 26 btn1 = (Button) findViewById(R.id.btn1); 27 btn2 = (Button) findViewById(R.id.btn2); 28 btn3 = (Button) findViewById(R.id.btn3); 29 et1 = (EditText) findViewById(R.id.et1); 30 et2 = (EditText) findViewById(R.id.et2); 31 et3 = (EditText) findViewById(R.id.et3); 32 btn1.setOnClickListener(this); 33 btn2.setOnClickListener(this); 34 btn3.setOnClickListener(this); 35 } 36 37 @Override 38 public void onClick(View v) { 39 switch (v.getId()) { 40 case R.id.btn1: 41 sb = new StringBuffer(); 42 shuang(); 43 et1.setText(sb); 44 break; 45 case R.id.btn2: 46 sb2 = new StringBuffer(); 47 daletou(); 48 et2.setText(sb2); 49 break; 50 case R.id.btn3: 51 sb3 = new StringBuffer(); 52 qixing(); 53 et3.setText(sb3); 54 break; 55 56 default: 57 break; 58 } 59 60 } 61 62 public void shuang() { 63 int[] a = new int[7]; 64 int i = 0; 65 while (i < 6) { 66 int b = (int) (Math.random() * 33) + 1; 67 boolean temp = true; 68 for (int m = 0; m < a.length - 1; m++) { 69 if (a[m] == b) { 70 temp = false; 71 } 72 } 73 74 if (!temp) { 75 continue; 76 } 77 a[i] = b; 78 i++; 79 } 80 for (int l = 0; l < a.length - 1; l++) { 81 for (int h = 0; h < a.length - 1; h++) { 82 if (a[l] < a[h]) { 83 int temp = a[l]; 84 a[l] = a[h]; 85 a[h] = temp; 86 } 87 } 88 } 89 int k = (int) (Math.random() * 16) + 1; 90 a[6] = k; 91 for (int n : a) { 92 sb.append(n + ","); 93 } 94 } 95 96 public void daletou() { 97 int[] a = new int[7]; 98 int i = 0; 99 while (i < 5) {100 int b = (int) (Math.random() * 35) + 1;101 boolean temp = true;102 for (int e = 0; e < a.length - 2; e++) {103 if (a[e] == b) {104 temp = false;105 }106 }107 if (!temp) {108 continue;109 }110 a[i] = b;111 i++;112 }113 for (int m = 0; m < a.length - 2; m++) {114 for (int n = m + 1; n < a.length - 2; n++) {115 if (a[m] > a[n]) {116 int temp1 = a[m];117 a[m] = a[n];118 a[n] = temp1;119 }120 }121 }122 while (i > 4 && i < 7) {123 int k = (int) (Math.random() * 12) + 1;124 boolean temp2 = true;125 for (int e = 5; e < 7; e++) {126 if (a[e] == k) {127 temp2 = false;128 }129 }130 if (!temp2) {131 continue;132 }133 a[i] = k;134 i++;135 }136 for (int m = 5; m < 7; m++) {137 for (int n = m + 1; n < 7; n++) {138 if (a[m] > a[n]) {139 int temp1 = a[m];140 a[m] = a[n];141 a[n] = temp1;142 }143 }144 }145 for (int n : a) {146 sb2.append(n + ",");147 }148 }149 150 public void qixing() {151 int[] a = new int[7];152 for (int j = 0; j < 7; j++) {153 int b = (int) (Math.random() * 10);154 a[j] = b;155 }156 for (int n : a) {157 sb3.append(n + ",");158 }159 160 }161 }
xml文件:
18 9 15 16 24 25 32 33 34 42 43 52 53 61
运行效果图: