android 版 双色球号码生成

http://www.oschina.net/question/999790_102938 


一共3类 MainACtivity    History    Numbers

注意全局变量和局部变量的使用

package com.example.testdemo;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
Button btn, btnF;
Numbers ns = new Numbers();
TextView tvRed, tvBule;
File f = new File("/sdcard/History.txt");
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 100) {
tvRed.setText(ns.array[0] + " " + ns.array[1] + " "
+ ns.array[2] + " " + ns.array[3] + " " + ns.array[4]
+ " " + ns.array[5] + " ");
tvRed.setTextColor(Color.RED);
tvBule.setText("" + ns.array[6]);
tvBule.setTextColor(Color.BLUE);
}


};
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvRed = (TextView) findViewById(R.id.textRed);
tvBule = (TextView) findViewById(R.id.textBule);
btn = (Button) findViewById(R.id.buttonStart);
btn.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
new Thread(new Runnable() {


@Override
public void run() {
ns.init();  //初始化一下
// for (int i = 0; i < ns.array.length; i++) {
// if (i != 6) {
// if (i > 1) {
// do {
// ns.array[i] = ns.r.nextInt(33) + 1;
// } while (isEqual(ns.array[i], i));
// if (i < 5) {
// ns.oldArray[i] = ns.array[i];
// }
// } else {
// ns.array[i] = ns.r.nextInt(33) + 1;
// if (i < 5) {
// ns.oldArray[i] = ns.array[i];
// }
//
// }
//
// }
//
// else {
// ns.array[i] = ns.r.nextInt(16) + 1;
// }
//
// }
for (int i = 0; i < ns.array.length; i++) {
if (i < 6) {
int Location; // 位置
int n=33 - i;
Location = ns.r.nextInt(n);// 从剩余的球内随机抽取一个
ns.array[i]=getNumbeFromArray(Location);
Handle(Location,n);        //处理
} else {
ns.array[i] = ns.r.nextInt(16) + 1;
}
}


if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedWriter fw = new BufferedWriter(
new FileWriter(f, true));
fw.write(ns.array[0] + " " + ns.array[1] + " "
+ ns.array[2] + " " + ns.array[3] + " "
+ ns.array[4] + " " + ns.array[5] + " "
+ ns.array[6]);
fw.newLine();
fw.flush();
fw.close();
fw = null;
} catch (IOException e) {
e.printStackTrace();
}
mHandler.sendEmptyMessage(100);
}


}).start();
}
});
btnF = (Button) findViewById(R.id.buttonF);
btnF.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, History.class);
MainActivity.this.startActivity(intent);
}
});
}


private boolean isEqual(int i, int j) {
for (int j2 = 0; j2 < j; j2++) {
if (i == ns.oldArray[j2]) {
return true;
}
}
return false;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


private int getNumbeFromArray(int location) {
return ns.allNumbers[location];
}


private void Handle(int location, int i) {
//1.删除 
for (int j = location; j < i; j++) {
if(jns.allNumbers[j]=ns.allNumbers[j+1];
else{ns.allNumbers[j]=0;}
}


}
}


package com.example.testdemo;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;                         
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class History extends Activity {
TextView tvRed, tvBule;
private BufferedReader br;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
tvRed = (TextView) findViewById(R.id.textView1);
tvBule = (TextView) findViewById(R.id.textView2);
tvRed.setTextColor(Color.RED);
try {
tvRed.setText(getMyText(1));
tvBule.setTextColor(Color.BLUE);
tvBule.setText(getMyText(0));
} catch (FileNotFoundException e) {
e.printStackTrace();
}


}


private CharSequence getMyText(int i) throws FileNotFoundException {


File f = new File("/sdcard/History.txt");
if (!f.exists()) {
return "";
}
br = new BufferedReader(new FileReader(f));
String str = null;
StringBuffer sbRed = new StringBuffer(), sbBule = new StringBuffer();
try {
while ((str = br.readLine()) != null) {


System.out.println(str);
System.out.println("---------------------");
String subR = str.substring(0, str.lastIndexOf(" "));
System.out.println("subR" + subR);


String subB = str.substring(str.lastIndexOf(" "));
System.out.println("subB" + subB);
sbRed.append(subR + "\n");
sbBule.append(subB + "\n");


}
} catch (IOException e) {


e.printStackTrace();
} finally {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (i == 1) {
System.out.println("---i=" + i);
return sbRed;
}
return sbBule;


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 1, 1, "清除历史记录");


return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == 1) {
File file = new File("/sdcard/History.txt");
BufferedWriter bw = null;
if (!file.exists()) {
return true;
}
try {
bw = new BufferedWriter(new FileWriter(file));
bw.write("");


} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.flush();
bw.close();


bw = null;
}
System.out.println("==+++++==");
tvBule.setText("sdas");
tvRed.setText("dsda");


} catch (IOException e) {
e.printStackTrace();
}
}


}
return super.onMenuItemSelected(featureId, item);
}


}


package com.example.testdemo;


import java.util.Random;


public class Numbers {
public int[] oldArray = new int[5];  // 已经摇出的号码
public int[] allNumbers=new int[33]; //所有的号码
public int[] array = new int[7];     //所有摇出的号码
public Random r = new Random();
public Numbers() {
//init();
}
public void init() {
for (int i = 0; i < allNumbers.length; i++) {
allNumbers[i]=i+1;
}
}
}

你可能感兴趣的:(android)