我这里有一个程序真的开始让我感到困惑(主要是因为我尝试了不同的方法让它工作并且每次都遇到某种错误)。我应该通过随机生成6个数字并让用户输入6个他们自己选择的数字来创建一个模拟真实生活彩票的程序。现在程序应该对两个数组进行线性搜索,看看是否有任何用户的数字等于随机生成的数字,但是我被教导使用线性搜索的方式不包括一次搜索多个数字(即因此我很困惑如何返回一个整数,如果大于零,则打印“你赢了”语句,如果小于零则打印“你丢失了语句”。
import java.util.*;
import java.text.*;
public class Lotto649 {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int [] playernums = new int [6];
int [] usernums = new int [6];
int [] result = new int [6];
int verdict;
int index;
System.out.printf ("Play Lotto 649 %n%n");
// Get randomly generated numbers from method and add them to array 'result'
result = GenerateLotto (playernums);
// get 6 numbers from user
for (int j = 0; j < usernums.length; j++){
System.out.printf ("Please enter a number between 1 and 49: ");
usernums[j] = s.nextInt();
}
// Print the winning numbers that were randomly generated
for (int i = 0; i < result.length; i++){
System.out.printf ("The winning Lotto 649 numbers are: " + result[i] + ", ");
}
}
// Method to fill array with winning lottery numbers
public static int [] GenerateLotto (int [] a){
Random g = new Random();
// fill array with 6 randomly generated numbers
for (int i = 0; i < a.length; i++){
a[i] = g.nextInt (49) + 1;
}
return a;
}
index = find (playernums,results);
// int [] A is player nums (player chosen numbers)
// int [] B is results array (RNG numbers)
public static int find (int[] A, int[] B){
for (int index = 0; index < A.length; index++){
for (int index2 = 0; index2 < B.length; index2++){
if (A[index] == B[index2])
return index;
}
}
}
}
我现在已经停止搞乱代码,因为我觉得我会进一步打破它。
答案 0 :(得分:0)
你有一些编译时错误,请你试试下面的代码谢谢
import java.util.*;
public class Lotto649 {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int [] playernums = new int [6];
int [] usernums = new int [6];
int [] result = new int [6];
int verdict;
int index;
System.out.printf ("Play Lotto 649 %n%n");
// Get randomly generated numbers from method and add them to array 'result'
result = GenerateLotto(playernums);
// get 6 numbers from user
for (int j = 0; j < usernums.length; j++){
System.out.printf ("Please enter a number between 1 and 49: ");
usernums[j] = s.nextInt();
}
// Print the winning numbers that were randomly generated
for (int i = 0; i < result.length; i++){
System.out.printf ("The winning Lotto 649 numbers are: " + result[i] + ", ");
}
int wonOrLost = find (usernums,result);
if(wonOrLost > 0)
System.out.println("You've won");
else
System.out.println("You've lost statement");
}
// Method to fill array with winning lottery numbers
public static int [] GenerateLotto (int [] a){
Random g = new Random();
// fill array with 6 randomly generated numbers
for (int i = 0; i < a.length; i++){
a[i] = g.nextInt (49) + 1;
}
return a;
}
// int [] A is player nums (player chosen numbers)
// int [] B is results array (RNG numbers)
public static int find (int[] A, int[] B){
for (int index = 0; index < A.length; index++){
for (int index2 = 0; index2 < B.length; index2++){
if (A[index] == B[index2])
return 1;
}
}
return -1;
}
}
答案 1 :(得分:0)
此解决方案效果很好:
import java.util.*;
import java.text.*;
public class Lotto649 {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int[] playernums = new int[6];
int [] usernums = new int [6];
int [] result = new int [6];
int input;
int verdict;
System.out.printf ("Play Lotto 649 %n%n");
// Get randomly generated numbers from method and add them to array 'result'
result = GenerateLotto (playernums);
// get 6 numbers from user
for (int j = 0; j < usernums.length; j++){
System.out.printf ("Please enter a number between 1 and 49: ");
input = Integer.parseInt(s.nextLine());
if (input < 1 || input > 49) { // Check if input is less than 1 or 49
System.out.println("Sorry, you must enter a number between 1 and 49.");
j--; // Decrement j so it asks for the number again (without decrementing there wouldn't be 6 total numbwrs).
} else {
usernums[j] = input; // Store user input into usernums
}
}
// Print the winning numbers that were randomly generated
System.out.printf ("The winning Lotto 649 numbers are: ");
for (int i = 0; i < result.length; i++) {
if (i < result.length - 1)
System.out.print(result[i] + ", ");
else
System.out.print(result[i]);
}
verdict = find(result, usernums);
if (verdict == 0) {
System.out.println("\nYou had no winning numbers. Thanks for wasting your money and making us rich!");
} else {
System.out.println("\nYou had " + verdict + " matches. Looks like we need to cough up the prize money.");
}
}
// Method to fill array with winning lottery numbers
public static int [] GenerateLotto (int [] a){
Random g = new Random();
// fill array with 6 randomly generated numbers
for (int i = 0; i < a.length; i++){
a[i] = g.nextInt (49) + 1;
}
return a;
}
// index = find (playernums,results);
// int [] A is player nums (player chosen numbers)
// int [] B is results array (RNG numbers)
public static int find (int[] A, int[] B) {
int matches = 0;
for (int index = 0; index < A.length; index++){
for (int index2 = 0; index2 < B.length; index2++) {
if (A[index] == B[index2])
matches++;
}
}
return matches;
}
}
如果你喜欢这个解决方案,请给我一个好评。