JAVA面向对象编程[双色球案例]…
面向对象双色球案例说明:
根据福利彩票标准中奖规则:
代码如下:
//工具类
package TwoColorBall;
import java.util.*;
/**
* 双色球
* 1,用户选择机选还是首选
* 2,接收用户选号(6红1蓝)
* 3,生成机选(6红1蓝)
* 4,比较系统代码和用户代码
* 5,验证是否中奖
* 7,公布结果
* @author Fang
*/
public class toolMethod {
//变量
int[] userRedBall = new int[6];//用户选择的红球
int[] sysRedBall = new int[6];//机选红球
int userBlueBall = 0;//用户选择的篮球
int sysBlueBall = 0;//机选篮球
int redCount = 0;//记录正确
int blueCount = 0;//
int[] redBall = new int[33];//存储红球1-33
//随机生成6个1-33不同的数字
public void Redballrandom() {
for (int i = 0; i < redBall.length; i++) {
redBall[i] = i + 1;
}
}
public static void computerSelection(int[] redBall, int[] useerredBall) {
Random r = new Random();
int index = -1;
for (int i = 0; i < useerredBall.length; i++) {
index = r.nextInt(redBall.length - i);
useerredBall[i] = redBall[index];
int temp = redBall[index];
redBall[index] = redBall[redBall.length - 1 - i];
redBall[redBall.length - 1 - i] = temp;
}
}
public static void sort(int[] ball){//冒泡排序
for (int i = 0; i < ball.length; i++) {
for (int j = 0; j < ball.length-1-i; j++) {
if (ball[j] > ball[j+1]) {
ball[j]=ball[j]+ball[j+1];
ball[j+1]=ball[j]-ball[j+1];
ball[j]=ball[j]-ball[j+1];
}
}
}
}
}
## 测试类:
package TwoColorBall;
import java.util.*;
public class TwoColorBallTese {
public static void main(String[] args) {
Random r = new Random();
toolMethod io = new toolMethod();
Scanner input = new Scanner(System.in);
for (int i = 0; i < io.redBall.length; i++) {
io.redBall[i] = i + 1;
}
System.out.println("欢迎购买双色球");
System.out.println("1.机选");
System.out.println("2.手动选择");
int numshuru = input.nextInt();
boolean pp = true;
while (pp) {
switch (numshuru) {
case 1:
io.computerSelection(io.redBall, io.userRedBall);
io.userBlueBall = r.nextInt(16) + 1;
pp=false;
break;
case 2:
System.out.println("请选择6个红球号码(1-33):");
for (int i = 0; i < io.userRedBall.length; i++) {
io.userRedBall[i] = input.nextInt();
}
System.out.println("请选择一个篮球号码(1—16):");
io.userBlueBall = input.nextInt();
pp = false;
break;
default:
System.out.println("欢迎购买双色球");
System.out.println("1.机选");
System.out.println("2.手动选择");
break;
}
}
io.computerSelection(io.redBall, io.sysRedBall);
io.sysBlueBall = r.nextInt(16) + 1;
//结果
//统计红球
for (int i = 0; i < io.userRedBall.length; i++) {
for (int j = 0; j < io.sysRedBall.length; j++) {
if (io.userRedBall[i] == io.sysRedBall[j]) {
int temp = io.sysRedBall[j];
io.sysRedBall[j] = io.sysRedBall[io.sysRedBall.length - 1 - io.redCount];
io.sysRedBall[io.sysRedBall.length - 1 - io.redCount] = temp;
io.redCount++;
break;
}
}
}
if (io.userBlueBall == io.sysBlueBall) {
io.blueCount = 1;
}
//验证是否中奖
if (io.blueCount == 0 && io.redCount <= 3) {
System.out.println("未中奖");
} else if (io.blueCount == 1 && io.redCount < 3) {
System.out.println("六等奖:五元钱");
} else if (io.blueCount == 1 && io.redCount == 3 || io.blueCount == 0 && io.redCount == 4) {
System.out.println("五等奖:十元钱");
} else if (io.blueCount == 1 && io.redCount == 4 || io.blueCount == 0 && io.redCount == 5) {
System.out.println("四等奖:二百元");
} else if (io.blueCount == 1 && io.redCount == 5) {
System.out.println("三等奖:3000元");
} else if (io.blueCount == 0 && io.redCount == 6) {
System.out.println("二等奖:150w");
}else if (io.blueCount == 1 && io.redCount == 6) {
System.out.println("一等奖:500w");
}else {
System.out.println("系统有误,白玩");
}
System.out.println("本期中奖红球号码为:");
io.sort(io.sysRedBall);
System.out.println(Arrays.toString(io.sysRedBall));
System.out.println("本期中奖的篮球号码为:"+io.sysBlueBall);
//用户号码
System.out.println("您选择的红球号码为:");
io.sort(io.userRedBall);
System.out.println(Arrays.toString(io.userRedBall));
System.out.println("您选择的篮球号码为:"+io.userBlueBall);`在这里插入代码片`
System.out.println("感谢购买");
}
}