Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
BCM
User Panel

Site Notices
Posted: 2/21/2014 11:45:39 AM EDT
Ok, so I have to write a java program that works as a simple slot machine.


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

public class Hw3pr62
{
public static void main(String[] args)
{

Random randomNumbers = new Random();
Scanner keyboard = new Scanner(System.in);

int cherry=0, orange=1, plum=2, bell=3, melon=4, bar=5;
int count, firstPick, secondPick, thirdPick;

double bet, newBet=0, amountWon=0;

char playAgain = 'y';

//Get bet from user

System.out.println("Welcome to the Slot Machine.");
System.out.println("How many dollars would you like to bet?");
bet = keyboard.nextDouble();
while (bet<0)
{
System.out.println("Only positive bets please.");
bet = keyboard.nextDouble();
}

// Create loops

while(playAgain == 'y')
{
firstPick = randomNumbers.nextInt(6);
secondPick = randomNumbers.nextInt(6);
thirdPick = randomNumbers.nextInt(6);

System.out.println(firstPick + " " + secondPick + " "
+ thirdPick);

if (firstPick == secondPick || firstPick == thirdPick || secondPick == thirdPick)
{
amountWon = bet * 2;
System.out.println("Congrats! You got 2 matches! You won $"
+ amountWon);
newBet += amountWon;
System.out.println("Play again? (y = yes / n = no)");
          playAgain = keyboard.next().charAt(0);

if (playAgain == 'n')
{
System.out.println("Thank you for playing.");
System.out.println("You initailly bet $" + bet + ".");
System.out.print("You won $" + (newBet - bet) + "!");
}
}

else if (firstPick == secondPick && firstPick == thirdPick)
{
amountWon = bet * 3;
System.out.println("Jackpot!! You got 3 matches! You won $"
+ amountWon);
newBet += amountWon;
System.out.println("Play again? (y = yes / n = no)");
          playAgain = keyboard.next().charAt(0);

if (playAgain == 'n')
{
System.out.println("Thank you for playing.");
System.out.println("You initailly bet $" + bet + ".");
System.out.print("You won $" + (newBet - bet) + "!");
}
}

else
{
System.out.println("Sorry. You did not get a match.");
System.out.println("Play again? (y = yes / n = no)");
          playAgain = keyboard.next().charAt(0);

if (playAgain == 'n')
{
System.out.println("Thank you for playing.");
System.out.println("You initailly bet $" + bet + ".");
System.out.print("You won $" + (newBet - bet) + "!");
}
}

}



}

}


I know there are better ways to get it to do the same stuff, but Im using what we've learned so far in the course, which is the extent of my programming knowledge.

I have it compiled to do exactly what I want it to do, the only thing I need to change is instead of printing out the random numbers that are generated, I need the strings I assigned to each random number to be printed, instead of the number.

So instead of printing ,say, 0 3 1, it would print cherry bell orange.
Link Posted: 2/21/2014 1:09:41 PM EDT
[#1]
getting what I need from GD, mods can lock this down or whatnot
Close Join Our Mail List to Stay Up To Date! Win a FREE Membership!

Sign up for the ARFCOM weekly newsletter and be entered to win a free ARFCOM membership. One new winner* is announced every week!

You will receive an email every Friday morning featuring the latest chatter from the hottest topics, breaking news surrounding legislation, as well as exclusive deals only available to ARFCOM email subscribers.


By signing up you agree to our User Agreement. *Must have a registered ARFCOM account to win.
Top Top