老虎机程序

时间:2014-11-10 04:23:12

标签: java eclipse oop syntax

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

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!");
}


}

1 个答案:

答案 0 :(得分:1)

我同意评论说你可能遗漏了一条错误消息(Eclipse非常适合直接标记这些消息)。然而,这是一个完全不同的问题(尽管你应该付出一些努力来解决)。

playGame()中,您有以下一行:

if(won=true){

问题是你使用一个=,意味着分配。你想要一个双==这是一个比较。您不能在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.

请注意,==会比较对大多数应用程序而言都很好的引用,但对于类似于字符串的certian数据类型,您应该string.equals()