我必须创建一个彩票游戏,你随机生成六个模拟彩票的中奖号码。要求用户输入六个数字,看看他们是否赢了彩票!
我做了很多,但现在我卡住了。我是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!");
}
}
}
答案 0 :(得分:1)
我在你的程序中看到了多个问题。
首先,可以通过将print语句从内部循环移动到外部循环来解决打印输出问题
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!");
}
并且,&#34;如果随机数相同,则生成另一个数字。&#34;可能不正确,因为它可能会再次产生相同的数字。您需要一个while循环来生成一个新数字,直到它不等于任何生成的数字。
关于编码风格的另一个问题是,对于任何&#34; for循环&#34;
这:
int i=0;
for (i = 0; i < length; i++)
应该替换为:
for (int i = 0; i < length; i++)
答案 1 :(得分:0)
正如我在评论中已经说过的那样,你应该将print语句从内部循环移动到外部循环。
您的代码应如下所示:
for (i = 0; i < lottery.length; i++) { // outer loop
System.out.print(lottery[i] + " ");
for (x = 0; x < j; x++) { // inner loop
if (user[i] == lottery[x]) {
counter++;
}
}
}