Slot Machine Code Problem
I've got everything finished for the program, but it's telling me that the "else if" won't work because I don't have an "if" but I have the "if" right above it. I tried making them all "if" to see if it worked but then it just makes the player win everytime without being able to exit. I don't know what I'm doing wrong. I appreciate any help. Thank you.
import java.util.Scanner;
import java.util.Random;
public class RayokovichD_Homework2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int Coins = 1000;
int Wager;
System.out.println("Slot Machine");
System.out.println("You have " + Coins + " coins.");
System.out.println("Press 0 to exit, any other number to play that many coins per spin.");
while (Coins > 0)
{
int x = new Random().nextInt(9);
int y = new Random().nextInt(9);
int z = new Random().nextInt(9);
开发者_运维技巧 Wager = input.nextInt();
if(Wager > Coins)
Wager = Coins;
System.out.println(x + " " + y + " " + z);
if(x == y && x == z)
Coins = Coins + (Wager * 100);
System.out.println("You won " + (Wager * 100) + "!" + " You now have " + Coins + " coins.");
System.out.println("Press 0 to exit, any other number to play that many coins per spin.");
else if((x == y && x != z) || (x != y && x == z) || (x != y && y == z))
Coins = Coins + (Wager * 10);
System.out.println("You won " + (Wager * 10) + "!" + " You now have " + Coins + " coins.");
System.out.println("Press 0 to exit, any other number to play that many coins per spin.");
else ((x != y && x != z) || (x != y && y != z))
Coins = Coins - Wager;
}
while (Wager == 0)
{
System.out.println("You ran out of coins. Thanks for playing.");
}
}
}
You are missing the braces! use:
if( boolean equation ) {
many_statements;
many_statements;
many_statements;
}
OR
if( boolean equation )
on_single_statement;
you must put curly braces in every if conditions. not because it's only 1 line of statement you didn't have to use curly braces. Curly braces is essential you must put it.
精彩评论