所以我坚持这个程序,生成6个整数,介于1-49之间,然后将它们存储在一个数组中,并按升序显示它们,但它们必须是唯一的数字。在此之后,程序必须要求用户提供他们的6个数字,然后将随机数组与用户进行比较,以确定是否有3次或更多的匹配,从而提示他们中了彩票(假设他们输入了唯一的数字)。我必须包括一个名为checkTicket的方法,它要求用户提供他们的数字,并返回与随机数匹配的数目。顺便说一句,我对java非常陌生,所以请原谅我的编码技巧。我似乎不知道如何将随机数组与checkTicket方法中的用户数组进行比较,并计算相同的数量。这就是我到目前为止所拥有的。
import java.util.*;
public class lottery
{
public static void main (String [] args) {
Random rand = new Random ();
int [] lotto= new int [6];
int count=0;
checkTicket(lotto, count);
int num =0;
for (int i = 0; i < 6; i++) {
num = (int) (Math.random() * 49 + 1);
for ( int j = 0; j < i; j++) {
if (lotto[j] == num)
{
num = (int) (Math.random() * 49 + 1);
}
}
lotto[i] = num;
}
System.out.println("\n The lottery numbers:");
System.out.println("\n--------------");
for (int i=0; i< lotto.length; i++){
System.out.print("\n"+lotto[i]);
}
System.out.println("\n--------------");
if (count>3){
System.out.println("\nYou have the winning ticket and this was the number of matches: "
+count);
}
else
System.out.println("\nBetter luck next time! This was how many you got right: "+count);
}
public static int checkTicket (int [] lotto, int count){
System.out.println("Enter 6 numbers between 1 to 49");
Scanner kb = new Scanner (System.in);
count=0;
int [] user =new int [6];
for ( int i=0;i<user.length;i++)
{
System.out.println("Number: ");
user[i]= kb.nextInt();
System.out.println(user[i]+" ");
for (int j= 0; j<lotto.length; j++){
if (user[i] == lotto[j])
count++;
}
}
return count;
}
}发布于 2020-01-17 19:38:50
我在您的代码中可以看到以下几个问题:
在将checkTicket.
count调用为checkTicket之前,需要初始化lotto数组的count=0; it checkTicket)再次初始化它这样做如下:
import java.util.Random;
import java.util.Scanner;
public class lottery {
public static void main(String[] args) {
Random rand = new Random();
int[] lotto = new int[6];
int num;
for (int i = 0; i < 6; i++) {
num = (int) (Math.random() * 49 + 1);
for (int j = 0; j < i; j++) {
if (lotto[j] == num) {
num = (int) (Math.random() * 49 + 1);
}
}
lotto[i] = num;
}
int count = checkTicket(lotto);
System.out.println("The lottery numbers:");
System.out.println("--------------");
for (int i = 0; i < lotto.length; i++) {
System.out.print(lotto[i] + " ");
}
System.out.println("\n--------------");
if (count > 3) {
System.out.println("You have the winning ticket and this was the number of matches: " + count);
} else
System.out.println("Better luck next time! This was how many you got right: " + count);
}
public static int checkTicket(int[] lotto) {
System.out.println("Enter 6 numbers between 1 to 49");
Scanner kb = new Scanner(System.in);
int count = 0;
int[] user = new int[6];
for (int i = 0; i < user.length; i++) {
System.out.print("Number: ");
user[i] = kb.nextInt();
for (int j = 0; j < lotto.length; j++) {
if (user[i] == lotto[j])
count++;
}
}
return count;
}
}示例运行:
Enter 6 numbers between 1 to 49
Number: 10
Number: 20
Number: 30
Number: 40
Number: 15
Number: 25
The lottery numbers:
--------------
15 38 10 27 13 30
--------------
Better luck next time! This was how many you got right: 3发布于 2020-01-17 19:15:10
在初始化lotto数组之前,您似乎正在调用checkTicket。
https://stackoverflow.com/questions/59792304
复制相似问题