我必须检查我在数组中给出的所有测试用例,但我没有得到完美答案。请帮助我解决这个问题。创建一个包含以下内容的PowerBall
类:
int
数组的字段。checkMatch
的方法,它将int
数组作为参数,并返回与类int
数组匹配的数量。要匹配,相同的数字必须位于相同的位置。写一个主要课程,要求用户输入6个数字作为他们的彩票并将其存储在一个数组中。然后创建一个PowerBall
对象,并将用户的票证提供给checkMatch
方法。然后根据返回值计算出赢得的金额。
奖金的确定如下:
输出用户的彩票,强力球数量,匹配数量以及赢得的金额。
class PowerBall {
/*
* ALL PRIVATE DATA BELOW
*/
private int[] winningNumber;
private int[] ticketNumber;
private long cash;
static private IntUtil u = new IntUtil();
int matchBalCount ;
int powerBallMatchCount;
public int cash() {
for (int i = 0; i < winningNumber.length; i++) {
for (int j = 0; j < ticketNumber.length; j++) {
if (i == winningNumber.length-1 && ticketNumber[i] == winningNumber[j]) {
powerBallMatchCount=1;
} else if (ticketNumber[i] == winningNumber[j]) {
matchBalCount++;
}
}
}
return 100;
}
public void check(int matchBalCount,int powerBalCount){
System.out.println("prize---matchBalCount::"+matchBalCount+" ,powerBallMatchCount::"+powerBallMatchCount);
if (matchBalCount == 0 && powerBallMatchCount>0) {
System.out.println("4");
}else if (matchBalCount == 1 && powerBallMatchCount>0) {
System.out.println("4");
}else if (matchBalCount == 2 && powerBallMatchCount>0) {
System.out.println("7");
}else if (matchBalCount == 3 && powerBallMatchCount<0) {
System.out.println("7");
}else if (matchBalCount == 3&& powerBallMatchCount>0) {
System.out.println("100");
}else if (matchBalCount == 4 && powerBallMatchCount<0) {
System.out.println("100");
}else if (matchBalCount == 4 && powerBallMatchCount>0) {
System.out.println("50000");
}else if (matchBalCount == 5 && powerBallMatchCount>0) {
System.out.println("1lakh");
}
}
PowerBall(int[] w, int[] t) {
winningNumber = w;
ticketNumber = t;
cash = 0;
check(matchBalCount,powerBallMatchCount);
}
private static void test1() {
int[] w = {4, 8, 19, 27, 24, 10};
{
int[] n = {4, 8, 19, 27, 24, 10};
PowerBall x = new PowerBall(w, n);
// x.cash();
}
{
int[] n = {24, 27, 19, 8, 4, 10};
PowerBall x = new PowerBall(w, n);
}
{
int[] n = {24, 27, 19, 8, 4, 5};
PowerBall x = new PowerBall(w, n);
}
{
int[] n = {124, 127, 119, 18, 14, 10};
PowerBall x = new PowerBall(w, n);
}
{
int[] n = {124, 127, 119, 18, 14, 5};
PowerBall x = new PowerBall(w, n);
}
{
int[] n = {124, 127, 119, 18, 14};
PowerBall x = new PowerBall(w, n);
}
{
int[] n = {124, 124, 19, 119, 18, 14};
PowerBall x = new PowerBall(w, n);
}
}
private static void testRandom() {
int[] w = {4, 8, 19, 27, 24, 10};
int max = 10;
long c = 0;
for (int i = 0; i < max; ++i) {
int[] n = u.generateRandomNumber(6, true, 1, 99);
PowerBall x = new PowerBall(w, n);
c = c + x.cash();
}
System.out.println("Out of " + max + " times you win " + c + "$");
}
private static void testBench() {
test1();
testRandom();
}
public static void main(String[] args) {
System.out.println("PowerBall.java");
testBench();
System.out.println("Done");
}
}
答案 0 :(得分:0)
这是计算比赛次数的好方法。
public int checkMatch(int [] userTicket)
{
int currIndex = 0;
int amountNumsMatching = 0;
if(userTicket.length == this.ticketNumber.length)
{
for (currIndex = 0; currIndex < this.ticketNumber.length; currIndex++)
{
if(this.ticketNumber[currIndex] == userTicket[currIndex])
{
amountNumsMatching++;
}
}
}
return amountNumsMatching;
}