java - 老虎机代码问题

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


    }
}

最佳答案

你缺少大括号!使用:

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

或者

if( boolean equation ) 
    on_single_statement;

关于java - 老虎机代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5008611/

相关文章:

java - 这两种单例实现有什么区别呢?

java - javafx 对象放入数组后会丢失引用的问题

php - MySQL IF 语句动态更改值?

python - 具有不同条件语句的函数返回

java - 比较方法违反了排序方法中的一般契约

java - Java 中的进度条

java - android.content.ActivityNotFoundException : No Activity found to handle Intent { act=android.intent.action.GET_CONTENT

java - If ... Else 语句似乎返回错误的输出

php - 数组声明中的 if 语句...这可能吗?

java - 如何在 if/else 语句中检查整数且小于或等于 100