2014-11-02 4 views
-2

코드/문제가 있지만 사용자가 4 개의 정답을 얻을 때까지 Hi/Lo 카드 게임을 실행하려고하는 게임을 설계하고 있습니다. 사용자가 "Higher", "Lower"또는 "Equal"이라고 말하면 카드 번호는 마지막 'cardGenerated'번호를 비교 한 번호입니다.자바에서 이름을 숫자로 설정하는 방법

지금은 사용자가 보지 못하거나 알지 못하는 숫자와 비교하여 옳고 그름을 알지 못합니다. 난 내가 한 수있는 출력을 선호 거라고하지만 난 그냥 showOptionDialog 출력에 'nextCard'변수를 추가 할 수 있습니다 알고, 그래서 프로그램의 인쇄 경우 :

"The Card pulled is the 9 
Is the next card Higher, Lower or Equal?" 

출력 다음 번호/카드 번호입니다 사용자가 이전 숫자 (9)와 (과) 비교했다는 의미입니다. 또한

,

내가 상수를 설정,하지만 난 그게 잭, 퀸, 킹, ACE, 무엇을 인쇄, 그것을 확인하는 방법을 잘 그래서 대신, 13, 12, 11 일 인쇄 아니에요 아니.

import java.util.Random; 
import javax.swing.JOptionPane; 

public class HiLo { 

public static final int JACK = 11; 
public static final int QUEEN = 12; 
public static final int KING = 13; 
public static final int ACE = 1; 

    public static void main(String[] args) { 

     int correctGuesses = 0; 

     Random generator = new Random(); 
     int currentCard; 
     int nextCard = generator.nextInt(KING+1); 

     while (correctGuesses < 4) 
     {   
      currentCard = nextCard; 
      nextCard = generator.nextInt(KING+1); 

      Object[] options = {"Higher", 
       "Lower", 
       "Equal"}; 
      int Input = JOptionPane.showOptionDialog(null, 
       "The Card pulled is the " + currentCard + 
       " \nis the next card Higher, Lower or Equal?", 
       "HiLo Card Game", 
      JOptionPane.YES_NO_CANCEL_OPTION, 
      JOptionPane.QUESTION_MESSAGE, 
      null, options, options[0]); 

      if (nextCard > currentCard && Input == JOptionPane.YES_OPTION) 
      { 
       correctGuesses++; 
      } 
      else if (nextCard > currentCard && Input == JOptionPane.NO_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard > currentCard && Input == JOptionPane.CANCEL_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard < currentCard && Input == JOptionPane.YES_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard < currentCard && Input == JOptionPane.NO_OPTION) 
      { 
       correctGuesses++; 
      } 

      else if (nextCard < currentCard && Input == JOptionPane.CANCEL_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard == currentCard && Input == JOptionPane.YES_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard == currentCard && Input == JOptionPane.NO_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard == currentCard && Input == JOptionPane.CANCEL_OPTION) 
      { 
       correctGuesses++; 
      }  
     } 

     JOptionPane.showMessageDialog(null, "Congratulations, You guessed correctly 4 times" 
       + "\nthe Last Card was the " + nextCard + " resart to play again"); 
    } 

} 
+1

http://stackoverflow.com/help/how-to-ask –

+0

correctGuesses '보다'0 '쓸 수있는 쉬운 방법이 있습니다 - correctGuesses' – Deltharis

+0

제가 알고있는 바로 그게 제가 생각한 최초의 수학적 방법이었습니다. 나는 그것을 correctGuesses = 0으로 만들 수 있다는 것을 압니다. 하지만 난 그걸 바꾸지 않았어. 그 순간에 내 큰 문제 중 하나가 아니기 때문에. – roughosing

답변

-1

당신은 변수가 단일 루프 반복에 포함되지하기 위해 (그리고 문제가 두 반복에 nextCard 값을 사용하고자하는) 당신은 루프 내를 선언하지합니다. 또한 반복 할 때마다 새 생성자 또는 옵션 객체가 필요하지 않습니다. 카드 인쇄에 관해서는

Random generator = new Random(); 
int currentCard; 
int nextCard = generator.nextInt(KING+1); 
while (correctGuesses < 4) 
{ 
    currentCard = nextCard; 
    nextCard = generator.nextInt(KING+1); 
    ... 
} 

- 당신은 아마 카드 열거를 만들어야합니다, 인쇄를 돌봐 관련 정보 (값, 스위트)과 오버라이드 (override) toString 메소드를 포함하는 하나. 쓰기는 충분히 간단해야합니다.

+0

감사합니다. 내 생각 엔 그 루프에서 그 일을하려고했지만 내 문제 : P – roughosing

+0

@ GregPenrose 질문의 핵심을 전혀 다른 것으로, 특히 답변을 얻은 후에 절대로 변경하지 마십시오! 그랬듯이 당신의 주된 문제를 해결 한 정답은 부적절한 것 같습니다. StackOverflow에는 한 가지 질문으로 한 가지 문제가 있습니다. – Deltharis

+0

오 죄송합니다. 지금 질문을 삭제하거나 변경 사항을 취소해야합니까? – roughosing

-1

지저분한 경우 중첩됩니다. 같은 것을 다시 반복해서 평가하지 않도록 단순화해야합니다. 예를 들어 처음 세 개의 레이어는 nextCard > cardGenerated입니다. 이 코드를 자신의 if으로 추출하면 코드를보다 쉽게 ​​읽을 수 있습니다. 또한 switch()으로 평가 (Input == JOptionPane.XX_OPTION)의 다른 부분을 대체 할 수

 if(nextCard > cardGenerated) 
     { 
      switch(input) 
      { 
       case JOptionPane.YES_OPTION: 
        correctGuesses++; 
        break; 
       case JOptionPane.NO_OPTION: 
       case JOptionPane.CANCEL_OPTION: 
        correctGuesses = 0; 
        break; 
       default: 
        System.out.println("Should never happen, but default case should always be included"); 
      } 
     } 
     else if(nextCard < cardGenerated) 
     { 
      switch(input) 
      { 
       case JOptionPane.NO_OPTION: 
        correctGuesses++; 
        break; 
       case JOptionPane.YES_OPTION: 
       case JOptionPane.CANCEL_OPTION: 
        correctGuesses = 0; 
        break; 
       default: 
        System.out.println("Should never happen, but default case should always be included"); 
      } 
     } 
     else 
     { 
      switch(input) 
      { 
       case JOptionPane.CANCEL_OPTION: 
        correctGuesses++; 
        break; 
       case JOptionPane.YES_OPTION: 
       case JOptionPane.NO_OPTION: 
        correctGuesses = 0; 
        break; 
       default: 
        System.out.println("Should never happen, but default case should always be included"); 
      } 
     } 
+0

좋은 조언이지만, 답변이 아니라는 의견을 크게 들려줍니다. – Deltharis

관련 문제