在我的程序中找不到此错误

时间:2011-07-23 23:30:21

标签: java

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?

4 个答案:

答案 0 :(得分:4)

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

    //================================================
    //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 scope中声明了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之间的数字,我只假设三位数,并且只为这个案例帮你解决这个问题,你应该弄清楚其余的自己!我不想解决你的作业,但我确实想指出你正确的方向。

*我已经更新了答案以反映yi_H建议的内容。*

答案 1 :(得分:1)

您的错误属于randomNum变量的范围。你在for循环中声明并初始化它,这就是它的范围。进一步声明它(如main的顶部),然后你可以在方法的整个生命周期中使用它。

关于你的第二个问题,为什么不在0到9之间生成3个数字,而不是在001-999之间生成一个随机数?如果 将它们置于001-999之间,则转换为字符串并检查其数量是否相同。

答案 2 :(得分:1)

我认为从概念上讲,你正在试图做到这一点的方式。你的代码循环并分配一个随机数三次,但是你要分配给同一个变量,所以前两个值将被丢弃,除了打印到屏幕上。

为什么不这样做:

int random1 = randomGen.nextInt(10);
int random2 = randomGen.nextInt(10);
int random3 = randomGen.nextInt(10);

然后打印这些并做逻辑以确定它们是否相等等等。

答案 3 :(得分:0)

好像你想要一个1到999之间的数字:

for(int i = 0; i < 3; i++)
{

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

这样做也是如此,但最后randomNum中的数字很好:

/* get int from 0 to 999 */
int randomF0 = randomGen.nextInt(10);
int randomF1 = randomGen.nextInt(10);
int randomF2 = randomGen.nextInt(10);
int randomNum = (((randomF0*10)+randomF1)*10)+randomF2;

/* print the int with 3 figures */
System.out.print(String.format("%03d", randomNum));

对于问题的第二部分,例如:

if(randomF0==randomF2==randomF3) "3 same figures";
else if(randomF0==randomF2||randomF0==randomF1||randomF2==randomF1) "2 same figures";

应该足够了。