老虎机代码问题

Mar*_*ary 2 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."); 
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 6

你错过了大括号!使用:

if( boolean equation ) {
    many_statements;
    many_statements;
    many_statements;
}
Run Code Online (Sandbox Code Playgroud)

要么

if( boolean equation ) 
    on_single_statement;
Run Code Online (Sandbox Code Playgroud)

  • +1这就是为什么新程序员应该总是使用大括号,即使是单个语句ifs. (8认同)
  • 但他已经做了确切的错误,使第二个解决方案"邪恶":-) :-) (2认同)