Java 老虎机程序

Java 老虎机程序,java,eclipse,oop,syntax,Java,Eclipse,Oop,Syntax,嘿,我正在尝试编写一个模拟吃角子老虎机的游戏,并用三种方法实现一个界面游戏 public String getPrize(); public String equipmentNeeded(); public String rules(); 我以为我已经成功地创建了这个游戏,但它还没有编译,eclipse和我目前对java语法的了解都没有说明这个问题。这是迄今为止的代码: public class SlotMachine implements Game { private double

嘿,我正在尝试编写一个模拟吃角子老虎机的游戏,并用三种方法实现一个界面游戏

public String getPrize();
public String equipmentNeeded();
public String rules();
我以为我已经成功地创建了这个游戏,但它还没有编译,eclipse和我目前对java语法的了解都没有说明这个问题。这是迄今为止的代码:

public class SlotMachine implements Game {
    private double Balance=15;
    private boolean enoughMoney, won=false;



public String getPrize() {
    String s=""+Balance;
    return s;
}


public String equipmentNeeded() {
    if(Balance<5){
        enoughMoney=false;
        return "You need more money.";
    }
    else
        enoughMoney=true;
        return "Good luck... the game definetely isn't rigged";
}


public String rules() {
    return "The game costs five cents to play. If you win, you get ten cents. To start the game you must pull the lever that spins the wheels. If 3 out of the 5 wheels have cherries and the remaining wheels aren't lemons then you win!";

}
public boolean pullLever(){
    if(enoughMoney)
        return true;
    else{
        System.out.println("You have "+Balance+". You need at least five to play");
        return false;
    }
}
public void playGame(){
    String choices[]={"cherries", "oranges", "lemons", "wild card", "bananas"};
    String guess[]=new String[5];
    Balance=Balance-5;
    if(pullLever()){
        for(int i=0; i<choices.length; i++){
            guess[i]=choices[(int)(Math.random()*6)];
        }
        for(int x=0; x<guess.length-2; x++){
            if(guess[x].equals("cherries")==false){
                System.out.println(guess[x]);
                won=false;
            }
            else
                for(int w=4; w<=5; w++){
                    if(guess[w].equals("lemons")){
                        won=false;
                        System.out.println("guess[w]");
                    }
                    else
                        won=true;
                }

        }
    }
    if(won=true){
        Balance=Balance+10;
        System.out.println("You have won!");
    }
    else
        System.out.println("Try again!");
}


}

我同意这样的评论,即可能有一条错误消息您丢失了Eclipse非常适合直接标记这些消息。然而,这是一个完全不同的问题,尽管这是一个你应该努力解决的问题

在playGame中,您有以下行:

if(won=true){
问题是您使用了单个=,表示赋值。您需要一个double==作为比较。不能在if条件内指定值。这就是你犯错误的原因

x = 2;  //assigns the value of 2 to x.
x == 2; //compares the value 2 to x. The value of x does not change. Returns boolean.

请注意==比较引用,这对于大多数应用程序来说都很好,但是对于某些数据类型(如字符串),您应该使用string.equals。

您的游戏类在哪里?编译器非常擅长告诉您为什么不编译某些东西。某处有个消息。请找到它并发布在这里。这并不是导致你的程序无法编译的原因,但你应该仔细检查你的括号。具体来说,需要设备中的else语句