我必须在小于或等于49的数组中生成6个随机数,不允许使用。要求用户输入任意6个数字。我需要我的程序来检查输入的每个数字是否等于生成的任何随机数。显示与正确猜测数量相关的消息。并使用循环允许玩家再次玩游戏。
除了比赛之外,我完成了所有的工作。我不知道如何让用户输入是或否。
import java.util.Arrays;
import javax.swing.*;
import java.util.Scanner;
class LuckyTask2{
public static void main(String args[]){
// TASK 2
//boolean play = true;
//
//while(play == true){
// Scanner input = new Scanner(System.in);
int lucky1,lucky2,lucky3,lucky4,lucky5,lucky6,number=0;
int j;
int Data[] = new int [6];
for(j=0; j<Data.length;++j){
Data[j] = (int) (Math.random() * 49);
//System.out.println(Data[j]);
int playagain=0;
}
do{
String input2 = JOptionPane.showInputDialog("Please enter your lucky number between 1 and 49.");
lucky1=Integer.parseInt(input2);
String input3 = JOptionPane.showInputDialog("Please enter your second lucky number between 1 and 49.");
lucky2=Integer.parseInt(input3);
String input4 = JOptionPane.showInputDialog("Please enter your third lucky number between 1 and 49.");
lucky3=Integer.parseInt(input4);
String input5 = JOptionPane.showInputDialog("Please enter your fourth lucky number between 1 and 49.");
lucky4=Integer.parseInt(input5);
String input6 = JOptionPane.showInputDialog("Please enter your fifth lucky number between 1 and 49.");
lucky5=Integer.parseInt(input6);
String input7 = JOptionPane.showInputDialog("Please enter your sixth lucky number between 1 and 49.");
lucky6=Integer.parseInt(input7);
for(j = 0; j< Data.length; j++){
if(Data[j]==lucky1){
number=number+1;
}
if(Data[j]==lucky2){
number=number+1;
}
if(Data[j]==lucky3){
number=number+1;
}
if(Data[j]==lucky4){
number=number+1;
}
if(Data[j]==lucky5){
number=number+1;
}
if(Data[j]==lucky6){
number=number+1;
} }
if(number==0){
System.out.println("Unfortunately, none of your numbers were winning numbers. Better luck next time!");
}
if(number==1){
System.out.println("One of your numbers was a winning number. You win a free play!");
}
if(number==2){
System.out.println("Two of your numbers were winning numbers. You win $10");
}
if(number==3){
System.out.println("Three of your numbers were winning numbers. You win a share of 4.25% of Pools Fund");
}
if(number==4){
System.out.println("4 of your numbers were winning numbers. You win a share of 3.35% of Pools Fund");
}
if(number==5){
System.out.println("5 of your numbers were winning numbers.You win Share of 3.15% of Pools Fund");
}
if(number==6){
System.out.println("WOW! Congrats! All of your numbers were winning numbers. You win win or share Jackpot of at least $10 Million (89.25% of Pools Fund)");
}
// System.out.println("Do you want to play again? (Yes/No)");
// String playagain = input.nextLine();
// if (playagain == "Yes")
// play = true;
// else
// play = false;
}}
答案 0 :(得分:0)
i1
范围/展示率仅限于您的for
循环,并且您的所有if
语句都在for循环外
答案 1 :(得分:0)
因为你在for循环中声明了i1。尝试在它之外声明它;例如
int i1 = 0;
for(i1=0;...
希望这有帮助!
编辑:
for(i1 = 0; i1 < Data1.length; i1++){
if(Data1[i1] = lucky1)
same++;
if(...
继续这个。