老虎机代码问题

时间:2011-02-15 19:33:50

标签: java if-statement

我已经为该程序完成了所有工作,但是它告诉我“else if”不起作用,因为我没有“if”但是我的“if”就在它上方。我试着让它们全部“如果”看看它是否有效但是它只是让玩家每次都能赢,而不能退出。我不知道我做错了什么。我感谢任何帮助。谢谢。

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."); 
        }


    }
}

1 个答案:

答案 0 :(得分:6)

你错过了大括号!使用方法:

if( boolean equation ) {
    many_statements;
    many_statements;
    many_statements;
}

OR

if( boolean equation ) 
    on_single_statement;