2014-11-10 2 views
-3

나는 슬롯 머신을 시뮬레이션하고 내가 성공적으로 게임을 만들었다 고 생각하는 세 가지 방법슬롯 머신 프로그램

public String getPrize(); 
public String equipmentNeeded(); 
public String rules(); 

와 인터페이스 게임을 구현하는 게임을 프로그래밍하려고하지만 컴파일 밤은 어느 쪽도 아니 일식 이봐 나 자바 구문에 대한 나의 현재 지식은 문제를 보여주고있다. (이클립스 상자의 직선을 그 마크 업에 대한 아주 좋은) 당신이 놓치고있는 오류 메시지가 아마도이 있음을

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


} 
+0

게임 클래스는 어디에 있습니까? –

+5

컴파일러는 왜 컴파일하지 않는지에 대해 __very__를 잘 알고 있습니다. 어딘가에 메시지가 있습니다. 그것을 찾아서 여기에 게시하십시오. –

+0

이것은 프로그램을 컴파일하지 않는 원인이 아니지만 브라케팅을 다시 확인해야합니다. 특히,'equipmentNeeded()'의'else' 문은 당신이 생각하는 것처럼 행동하지 않습니다. (https://stackoverflow.com/questions/8020228/is-it-ok-if-i-omit- curly-braces-in-java) – acattle

답변

1

나는이 의견에 동의 : 이것은 지금까지 코드입니다. 그러나 그것은 완전히 다른 문제입니다 (해결에 약간의 노력을 기울여야 만합니다).

if(won=true){ 

문제는 당신이 할당을 의미 하나 =을 사용할 수 있습니다 : playGame()에서

당신은 다음 줄이있다. 당신은 두 배의 ==을 비교하기를 원합니다. 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() 문자열과 같은 certian 데이터 유형에 대한.