我必须创建一个彩票游戏,您在其中随机生成六个模拟彩票的中奖号码。要求用户输入六个数字,看看他们是否中奖了!
我已经做了很多,但现在卡住了。我对Java真的很陌生,请原谅我。当它提示您询问另一个号码时,它会执行该操作,但仍会为显示的自制彩票挑选显示它。同样,当显示计算机抽奖选择的数字时,它们又是重复的相同数字,而不是6个数字。计数器不能正常工作,这也许是一件小事,但我不知道。谢谢
package arraysIntroduction;
import java.util.Scanner;
public class sizeQuestion {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int [] user =new int [6];
int i;
//Fill user array
System.err.println("Welcome to Miwand's Lottery");
System.out.println("Please enter your 6 desiered number to enter the lottery");
System.out.println("Pick from 1 to 49");
for ( i=0;i<user.length;i++) // gets numbers until it equals 6
{
System.out.println("Number:");
user[i]= in.nextInt(); // Gets numbers from user
while (user[i] < 0 ) // if its a negative number tell user to enter again
{
System.err.println("Negative number, please enter again:");
user[i]=in.nextInt();
}
if (user[i] > 49) // if the number goes past 49 prompt again
{
System.err.println("Please enter numbers from 1 - 49");
}
}
//print out the numbers generated
for ( i=0;i < user.length; i++){
System.out.print(+user[i]+ " ");
}
System.out.println(" ");
int[] lottery = new int[6];
int guesses;
int counter=0;
int j;
int x;
{
for (j = 0; j < 6; j++) {
int randomNum = (int) (Math.random() *49 +1); // Random number created here.
for ( x = 0; x < j; x++) {
if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
{
randomNum = (int) (Math.random() *49 +1);// If random number is same, another number generated.
}
lottery[j] = randomNum;
}
}
//prints out computer generated numbers
for (i = 0; i < lottery.length; i++){
for (x = 0; x< j; x++){
System.out.print(lottery[i] + " ");
if (user[i] == lottery[x] ){
counter++;
}
}
}
}
if (counter < 2){
System.out.println("Try again!");
}
if (counter == 3){
System.out.println("3 numbers matched! You won $300!");
}
if (counter == 4){
System.out.println("4 numbers matched! You won 500!");
}
if (counter == 5){
System.out.println(" 5 numbers matched! You won $1000!");
}
else if (counter == 6){
System.out.println("JACCKKKPOOOOTTTTTTT!!!!!! YOU WIN 1 MILLION DOLLARS!");
}
}
}
最佳答案
我在您的程序中看到了多个问题。
首先,可以通过将打印语句从内部循环移动到外部循环来解决打印输出问题
for (i = 0; i < lottery.length; i++) {
System.out.print(lottery[i] + " ");
for (x = 0; x < j; x++) {
if (user[i] == lottery[x]) {
counter++;
}
}
}
另外,您没有正确分配第一个随机数,它始终使用默认值0
解:
for (j = 0; j < 6; j++) {
int randomNum = (int) (Math.random() * 49 + 1); // Random number created here.
for (x = 0; x < j; x++) {
if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
{
randomNum = (int) (Math.random() * 49 + 1);// If random number is same, another number generated.
}
}
lottery[j] = randomNum;
}
另外,您没有检查计数器是否等于2
解:
if (counter <= 2) {
System.out.println("Try again!");
}
并且,“如果随机数相同,则生成另一个数字”中的逻辑。可能不正确,因为它可能会再次生成相同的数字。您需要一个while循环来生成一个新数字,直到它不等于任何生成的数字。
关于编码风格的另一件事是,对于任何“ for循环”
这个 :
int i=0;
for (i = 0; i < length; i++)
应替换为:
for (int i = 0; i < length; i++)
关于java - 使用数组的Java彩票,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43719727/