Can't find this error in my program
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)
开发者_Go百科 {
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;
}
}
}
I have an an error that says randomNum cannot be resolved
. I thought I declared this variable and initialized it. Why is it keeping giving me errors?
The second part of my problem is how do I figure this? This program is slot machine. Here is a sample of the output:
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.
How do I determine two numbers that are the same in the random generated number to determine if someone wins like 433 or 111 or 224?
While I don't understand the second part of your question, the first one is simple:
//================================================
//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)
you have declared randomNum in a for scope. You should have done this:
//================================================
//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)
UPDATE: I think I figured out the second question as well. You need to determine if there are at least two equal digits in the random number. Here's how you can do it (I'm sure there's some nice mathematical way as well :) ):
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
}
PLEASE NOTE, because I see you limit the numbers between 100 and 999, I'll assume three digits only, and help you solve this for this case alone, you should figure out the rest YOURSELF! I don't want to solve your homework, but I do want to point you in the right direction.
*I've updated the answer to reflect what yi_H suggested.*
I think there's something conceptually off about the way you're trying to do this. Your code loops and assigns a random number three times, but you're assigning to the same variable, so the first two values will be discarded, except to be printed to the screen.
Why not do something like this:
int random1 = randomGen.nextInt(10);
int random2 = randomGen.nextInt(10);
int random3 = randomGen.nextInt(10);
Then print these and do logic to determine if they are equal, etc.
Your error is in the scope of your randomNum
variable. You're declaring and initializing it in a for loop and that's the scope of it. Declare it further up (like at the top of main
) then you can use it throughout the life of the method.
As to your second question, instead of generating a random number between 001-999 why not generate 3 numbers between 0 and 9? If you have to have them between 001-999 then convert to a string and check how many are the same.
It seems like you want a number between 1 and 999 with :
for(int i = 0; i < 3; i++)
{
int randomNum = randomGen.nextInt(10);
System.out.print(randomNum);
}
This do the same but with the good number in randomNum
at the end :
/* 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));
For the second part of your question, something like :
if(randomF0==randomF2==randomF3) "3 same figures";
else if(randomF0==randomF2||randomF0==randomF1||randomF2==randomF1) "2 same figures";
should be enough.
精彩评论