gpt4 book ai didi

java - 老虎机代码问题

转载 作者:行者123 更新时间:2023-11-30 05:57:54 24 4
gpt4 key购买 nike

我已经完成了该程序的所有内容,但它告诉我“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com