今天我写一个双色球计算奖金的小算法,如果有什么地方有bug呢,欢迎大家及时纠正。首先我们先来了解一下需求。
我选购一组号码,分别是6个红球1个蓝球。计算奖金的规则如下:
六等奖:中一个蓝号或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("您未中奖");
}
}
}
这样呢一个简单的小程序就写好了。欢迎大家纠错。