gpt4 book ai didi

java - 在我的程序中找不到这个错误

转载 作者:行者123 更新时间:2023-11-29 07:19:09 25 4
gpt4 key购买 nike

import java.util.Scanner;
import java.util.Random;

public class SlotMachine
{
public static void main(String []args)
{
Scanner input = new Scanner(System.in);
Random randomGen = new Random();

int start = 10;

System.out.println("Welcome to the Slot machine!");
System.out.println("You have "+ start);
System.out.println("Choose one of the following menu options:");
//==============================
// MENU
//==============================
int menu = 2;

while (menu >= 1 && menu <= 2)
{
System.out.println("1) Play the slot machine.");
System.out.println("2) Cash out.");
menu = input.nextInt();

if (menu == 1)
{

System.out.print("The slot machine shows: ");

//================================================
//GENERATE A RANDOM NUMBER BTWEEN 0-10 INCLUSIVE
//================================================
for(int i = 0; i < 3; i++)
{

int randomNum = randomGen.nextInt(10);
System.out.print(randomNum);
}

if (randomNum < 100)
{
System.out.println("Sorry you don't win anything!!");
}


else if ( randomNum >= 1 && randomNum < 1000)
{

}

//===========================================
// KEEPING COUNT OF HOW MANY TIMES PLAYED
// ==========================================
int count = 0; // keeps count of how many times you play
int amountLeft = 0;
while (count <= menu)
{
count += (menu*0.25);
amountLeft = start - count;
}

System.out.println("You have" + amountLeft);

}
if (menu == 2)
{
System.out.println("Thank you for playing ");
}

if (menu < 1 || menu > 2)
{
System.out.println("invalid range of numbers.... PLEASE ENTER 1 OR 2!!!");
}
break;
}
}
}

我有一个错误提示 randomNum cannot be resolved。我以为我声明了这个变量并初始化了它。为什么它一直给我错误?

我的问题的第二部分是如何解决这个问题?这个程序是老虎机。这是输出示例:

Welcome to the slot machine! 
You have $10.00.
Choose one of the following menu options:
1) Play the slot machine.
2) Cash out.

1

The slot machine shows 046.
Sorry you don't win anything.

You have $9.75.
Choose one of the following menu options:
1) Play the slot machine.
2) Cash out.

1

The slot machine shows 933.
You win 50 cents!

You have $10.00.
Choose one of the following menu options:
1) Play the slot machine.
2) Cash out.

如何确定随机生成的数字中相同的两个数字来确定某人是否中奖,例如 433 或 111 或 224?

最佳答案

虽然我不明白你问题的第二部分,但第一部分很简单:

    //================================================
//GENERATE A RANDOM NUMBER BTWEEN 0-10 INCLUSIVE
//================================================
for(int i = 0; i < 3; i++)
{

int randomNum = randomGen.nextInt(10);
System.out.print(randomNum);
}

if (randomNum < 100)

您已在 for 范围内声明了 randomNum。你应该这样做:

    //================================================
//GENERATE A RANDOM NUMBER BTWEEN 0-10 INCLUSIVE
//================================================

int randonNum = 0; // declare the variable & initialize it
for(int i = 0; i < 3; i++)
{
randomNum = (randomNum * 10) + randomGen.nextInt(10);
System.out.print(randomNum);
}

if (randomNum < 100)

更新:我想我也想出了第二个问题。您需要确定随机数中是否至少有两个相等的数字。以下是您的操作方法(我相信还有一些不错的数学方法 :)):

int digitA = randomNum % 10;
int digitB = (randomNum / 10) % 10;
int digitc = (randomNum / 100) % 10;

if(digitA == digitB || digitA == digitC || digitB == digitC) {
// matches
} else {
// no match
}

请注意,因为我看到您将数字限制在 100 到 999 之间,所以我只假设三位数,并帮助您单独解决这个问题,您应该自己解决剩下的问题!我不想解决你的作业,但我确实想为你指明正确的方向。

*我更新了答案以反射(reflect) yi_H 的建议。*

关于java - 在我的程序中找不到这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6803954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com