*代码问题
问题描述:
我已经为程序完成了一切,但它告诉我,“其他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;
}
OR
if(boolean equation)
on_single_statement;
+8
+1这就是为什么新程序员应该总是使用大括号,即使是单个语句ifs。 – 2011-02-15 19:37:37
答
你必须把花括号中的每一个,如果条件。 不是因为它只有1行声明,您不必使用花括号。 你必须把它放在花括号里。
你已经使用过python了吗?; D – 2011-02-15 19:45:36