简单的福彩双色球生成器

最近总有想买彩票的冲动,所以写了一个简单的彩票生成器。内容比较简单,求大牛不虐。真机上运行效果是这样的,比较丑得意

                             


主要功能如下:

1、从1-33,1-16中选号;

2、重置;

3、一键选号;

下面一一进行说明:

首先是选号的部分,都是有生成按钮+显示号码部分组成的。其中显示红色球部分,单独写了一个布局,在主布局中使用<include>标签引用,这样避免了主布局内容过于繁杂。

在Activity中可以直接使用findViewById来获得include布局中的内容。每一个红球的背景都是一个shape,形状为oval,设置了solid和size。红球初始时显示的问号(?)在Strings.xml中这样定义 \?,不然显示会不正确。

给按钮添加单击事件采用的是添加android:onClick属性的方法。对应onClick的方法必须满足三个条件:public(共有),void(返回值为空),有且只有一个View类型的参数,代表所点击的组件。

以创建蓝球为例:

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="createBlueValue"
            android:text="@string/btn_blue_value" />


public void createBlueValue(View view){
Random random = new Random();
int temp = random.nextInt(16) + 1; // 1-16

blue.setText(temp + "");

textHint.setText(getResources().getString(R.string.blue_hint_value) + temp);
}


Random的nextInt(index)方法会生成0到index之间的随机数,包含0但不包含index,所以我们对其加1,以便生成1到16之间的随机数。


生成红球的代码如下:

public void createRedValue(View view){
if(currentRedIndex == 6){
Toast.makeText(this, "red complete!", Toast.LENGTH_SHORT).show();
return;
}

Random random = new Random();
int temp = random.nextInt(33) + 1; // 1-33
setRedText(currentRedIndex, temp);
currentRedIndex++;
}

定义一个变量currentRedIndex来声明当前正在生成第几个红球。


这里我们有个问题,在生成红球时,有一种情况需要我们注意,生成红球时有可能会生成重复的号码,比如2,15,2。我们应该怎么来避免这个问题呢?

有人说把生成红球的号码放入一个List中,生成一个新号码,就去List中判断,如果存在了就重新生成一个,这样也有不妥,如果程序重复的生成,每次都生成同样的号码,那就会导致死循环了,虽然这样的概率比较低,但也存在这样的可能性。

所以,我们想了下面这种方法来解决冲突,这就类似数据结构中查找部分解决冲突似的,我们采用类似线性探测的方法。

如果新生成的号码包含在List中则新号码加1,一直循环的加下去,如果新号码大于33了,则置为1,这样我们在33个数中生成6个数,不会出现数据重叠的情况。

private void setRedText(int index, int value){

int finalValue = judgeValueValid(value);

selectedList.add(finalValue);
switch(index){
case 0:
red01.setText(finalValue + "");
break;
case 1:
red02.setText(finalValue + "");
break;
case 2:
red03.setText(finalValue + "");
break;
case 3:
red04.setText(finalValue + "");
break;
case 4:
red05.setText(finalValue + "");
break;
case 5:
red06.setText(finalValue + "");
break;
}
String str = String.format(getResources().getString(R.string.red_hint_value), (currentRedIndex + 1));
textHint.setText(str + finalValue);
}


private int judgeValueValid(int value){
while(selectedList.contains(value)){
value++;
if(value > 33){
value = 1;
}
}

return value;
}


接下来看重置部分和一键选号部分,

public void resetValues(View view){
currentRedIndex = 0;
selectedList.clear();

red01.setText(R.string.default_value);
red02.setText(R.string.default_value);
red03.setText(R.string.default_value);
red04.setText(R.string.default_value);
red05.setText(R.string.default_value);
red06.setText(R.string.default_value);

blue.setText(R.string.default_value);
textHint.setText("");
}


public void performShortCut(View view){
resetValues(view);

for(int i=0;i<6;i++){
createRedValue(view);
}

createBlueValue(view);
}


这样整个简单的小应用就完成了,内容比较简单,希望大家积极的给我指正和交流。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是福利彩票双色球Java的实现: ```java import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class DoubleColorBall { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入您要购买的彩票注数:"); int n = input.nextInt(); int[][] tickets = new int[n][7]; for (int i = 0; i < n; i++) { System.out.println("请输入第" + (i + 1) + "注彩票的号码:"); for (int j = 0; j < 6; j++) { System.out.print("请输入第" + (j + 1) + "个红球号码(1-33):"); int redBall = input.nextInt(); while (redBall < 1 || redBall > 33) { System.out.print("输入有误,请重新输入第" + (j + 1) + "个红球号码(1-33):"); redBall = input.nextInt(); } tickets[i][j] = redBall; } System.out.print("请输入蓝球号码(1-16):"); int blueBall = input.nextInt(); while (blueBall < 1 || blueBall > 16) { System.out.print("输入有误,请重新输入蓝球号码(1-16):"); blueBall = input.nextInt(); } tickets[i][6] = blueBall; } System.out.println("您购买的彩票号码为:"); for (int i = 0; i < n; i++) { System.out.print("第" + (i + 1) + "注:"); for (int j = 0; j < 6; j++) { System.out.print(tickets[i][j] + " "); } System.out.println("蓝球:" + tickets[i][6]); } int[] result = getResult(); System.out.println("本期开奖号码为:" + Arrays.toString(Arrays.copyOf(result, 6)) + " 蓝球:" + result[6]); int totalMoney = n * 2; int totalPrize = 0; for (int i = 0; i < n; i++) { int prize = getPrize(tickets[i], result); totalPrize += prize; if (prize > 0) { System.out.println("第" + (i + 1) + "注彩票中奖了,中奖金额为:" + prize + "元"); } } System.out.println("您一共下注" + totalMoney + "元,累计中奖" + totalPrize + "元"); } public static int[] getResult() { int[] result = new int[7]; Random random = new Random(); for (int i = 0; i < 6; i++) { int redBall = random.nextInt(33) + 1; while (contains(result, redBall, i)) { redBall = random.nextInt(33) + 1; } result[i] = redBall; } Arrays.sort(result, 0, 6); int blueBall = random.nextInt(16) + 1; result[6] = blueBall; return result; } public static boolean contains(int[] array, int value, int length) { for (int i = 0; i < length; i++) { if (array[i] == value) { return true; } } return false; } public static int getPrize(int[] ticket, int[] result) { int redCount = 0; int blueCount = 0; for (int i = 0; i < 6; i++) { if (contains(result, ticket[i], 6)) { redCount++; } } if (ticket[6] == result[6]) { blueCount = 1; } if (redCount == 6 && blueCount == 1) { return 10000000; } else if (redCount == 6) { return 500000; } else if (redCount == 5 && blueCount == 1) { return 3000; } else if (redCount == 5 || (redCount == 4 && blueCount == 1)) { return 200; } else if (redCount == 4 || (redCount == 3 && blueCount == 1)) { return 10; } else if (blueCount == 1) { return 5; } else { return 0; } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值