最近呢做了一个奖金计算的小算法,页面不好看,但是能实现功能。首先呢我们要购买6个红号一个蓝号。接下来要了解一下中奖的规则:

六等奖:中一个蓝号或1-2个红号加一个蓝号为:5元;
五等奖:中3个红号加1个蓝号或中4个红号为:10元;
四等奖:中4个红号加1个蓝号或中5个红号为:200元; 
三等奖:中5个红号加1个蓝号为:3000元; 
二等奖:中6个红号为二等奖(当奖池资金低于1亿元时,奖金总额为当期高等奖金的30%,当奖池资金高于1亿元时,奖金总额为当期高等奖金的70%) 
一等奖:中6个红号加1个蓝号,为一等奖(当奖池资金低于1亿元时,奖金总额为当期高等奖金的70%加奖池累积奖金之和,当奖池资金高于1亿元时,奖金总额为当期高等奖金的30%加奖池累积奖金之和,单注最高封顶为500万元)

现在对需求都明确了,我就可以编代码了。

首先有一个简单的布局 我只放了一个button和一个textview。点击计算奖金的按钮,textView就会显示结果。当然我在程序里的中奖号码和购买号码都是写的固定的值。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="${relativePackage}.${activityClass}" 
     android:orientation="vertical">


     <Button
         android:id="@+id/bouns_count_btn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="奖金计算" />


     <TextView
         android:id="@+id/bouns_count_result"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />


 </LinearLayout>
然后呢就是一个简单的小算法。
public class BounsCountActivity extends Activity {
private Button bouns_count_btn;  //奖金计算按钮
private TextView bouns_count_result;   //奖金金额
private String[] bouns_red={"1","3","10","23","25","33"}; //红球中奖号码数组
private String bouns_blue_str = "1";//蓝球的中奖数字
private String select_red_str = "1 3 10 23 25 8 7";//选购的所有数字 ,最后一个数字为蓝球数字
private String[] select_red;  // 选购的红球数组

private int numRed = 0,numBlue = 0;//中了几个号码


     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_bouns_count);
         bouns_count_result = (TextView)findViewById(R.id.bouns_count_result);  //初始化奖金金额
         bouns_count_btn = (Button)findViewById(R.id.bouns_count_btn);  //初始化奖金按钮
         
         //监听计算奖金按钮
         bouns_count_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
count(bouns_red,select_red_str); //调用计算奖金的函数
}
});
      
     }


protected void count(String[] bouns_red,String select_red_str) {
  select_red = select_red_str.split(" ");  //分割字符串存放到数组里
       for (int i = 0; i < select_red.length-1; i++) {
            //如果我选择的号码和中奖号码一致,则num加1
if (select_red[i].equals(bouns_red[i])) {
numRed++;
}
}
       //如果蓝球的中奖号码与我选择的号码一致,则num=1
       if (select_red[select_red.length-1].equals(bouns_blue_str)) {
         numBlue = 1;

}
      
       if (numRed < 3 && numBlue == 1) {
         //中一个蓝号或1-2个红号加一个蓝号为:5元
         bouns_count_result.setText("5"+"元");
}else if ((numRed == 4 && numBlue == 0) || (numRed == 3 && numBlue == 1)) {
//中3个红号加1个蓝号或中4个红号为:10元
bouns_count_result.setText("10"+"元");
}else if ((numRed == 5 && numBlue == 0) || (numRed == 4 && numBlue == 1)) {
//中4个红号加1个蓝号或中5个红号为:200元
bouns_count_result.setText("200"+"元");
}else if (numRed == 5 && numBlue == 1) {
//中5个红号加1个蓝号为:3000元
bouns_count_result.setText("3000"+"元");
}else if (numRed == 6 && numBlue == 1) {
//中6个红号为二等奖
bouns_count_result.setText("1"+"百万元");
} else if (numRed == 6 && numBlue == 1) {
//中6个红号加1个蓝号,为一等奖
bouns_count_result.setText("5"+"百万元");
}else{
bouns_count_result.setText("您未中奖");
}
         
}
 }

这样呢一个简单的小程序就做好了。如果有什么bug欢迎大家纠正。