0

这是我整个课程的一小部分,由 4 个课程组成。该课程的目标是打印出一张带有数值和花色的卡片,然后用户继续猜测下一张卡片是否具有更高的数值。如果牌相同,则花色法确定最大花色的顺序 // 黑桃 >>>> 心形 >>>> 俱乐部 >>>> 钻石(如纸牌法所示)。然而,问题发生在我的 HighLowrev 类中,在 do while 循环中,因为要求用户再次播放,如果用户以“y”响应,则程序继续,即使用户以“n”响应,程序也会继续。我已经尝试查找布尔值的进一步用法,但我意识到我很确定它们以这种方式工作。任何帮助将非常感激。

卡类

public class Card {
  // card class initalize varibles (NOtice the FINAL (THEY NEVER CHANGE VALUE!!!))
  public final static int SPADES = 3;   // Codes for the 4 suits, plus Joker.
  public final static int HEARTS = 2;
  public final static int DIAMONDS = 0;
  public final static int CLUBS = 1;
  public final static int JOKER = 4;
  // SPADES >>>> HEARTS >>>> CLUBS >>>> DIAMONDS
  public final static int ACE = 1;      // Codes for the non-numeric cards.
  public final static int JACK = 11;    //   Cards 2 through 10 have their 
  public final static int QUEEN = 12;   //   numerical values for their codes.
  public final static int KING = 13;

  private final int suit; 

  private final int value;
  public static void main (String [] args){
  } // joker constructor
  public Card() {
    suit = JOKER;
    value = 1;
  }
  // incase an illegal field occurs
  public Card(int theValue, int theSuit) {
    if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS && 
        theSuit != CLUBS && theSuit != JOKER)
      throw new IllegalArgumentException("Illegal playing card suit");
    if (theSuit != JOKER && (theValue < 1 || theValue > 13))
      throw new IllegalArgumentException("Illegal playing card value");
    value = theValue;
    suit = theSuit;
  }

  public int getSuit() {
    return suit;
  }
  // getter methods
  public int getValue() {
    return value;
  }
  // cases for suits...
  public String getSuitAsString() {
    switch ( suit ) {
      case SPADES:   return "Spades";
      case HEARTS:   return "Hearts";
      case DIAMONDS: return "Diamonds";
      case CLUBS:    return "Clubs";
      default:       return "Joker";
    }
  }
  // cases for numerical values...
  public String getValueAsString() {
    if (suit == JOKER)
      return "" + value;
    else {
      switch ( value ) {
        case 1:   return "Ace";
        case 2:   return "2";
        case 3:   return "3";
        case 4:   return "4";
        case 5:   return "5";
        case 6:   return "6";
        case 7:   return "7";
        case 8:   return "8";
        case 9:   return "9";
        case 10:  return "10";
        case 11:  return "Jack";
        case 12:  return "Queen";
        default:  return "King";
      }
    }
  }   
  public String toString() {
    if (suit == JOKER) {
      if (value == 1)
        return "Joker"; // if the suit is the joker ....
      else
        return "Joker #" + value;
    }
    else { // return suit and number
        return getValueAsString() + " of " + getSuitAsString() ;
      }
    }

}

主程序类

import java.io.*;

public class HighLowrev {

  public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));  // allow input

    System.out.println("This program lets you play the simple card game,");
    System.out.println("HighLow.  A card is dealt from a deck of cards.");
    System.out.println("You have to predict whether the next card will be");
    System.out.println("higher or lower.  Your score in the game is the");
    System.out.println("number of correct predictions you make before");
    System.out.println("you guess wrong.");
    System.out.println();

    int gamesPlayed = 0;     // Number of games user has played.
    int sumOfScores = 0;     // The sum of all the scores from 
    //      all the games played.
    double averageScore;     // Average score, computed by dividing
    //      sumOfScores by gamesPlayed.
    boolean playAgain = true;;       // Record user's response when user is 
    //   asked whether he wants to play 
    //   another game.
    do {
         int scoreThisGame;        // Score for one game.
         scoreThisGame = play();   // Play the game and get the score.
         sumOfScores += scoreThisGame;
         gamesPlayed++;
         System.out.print("Play again? ");
         String input = br.readLine();
         if(input== "Y" || input =="y") {
           playAgain = true;
         }
         else {
          playAgain =false; 
         }
      } while (playAgain=true);

      averageScore = ((double)sumOfScores) / gamesPlayed;

      System.out.println();
      System.out.println("You played " + gamesPlayed + " games.");
      System.out.printf("Your average score was %1.3f.\n", averageScore);

   }  // end main()

  private static int play() throws IOException {
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));  // allow input
    Deck deck = new Deck();  // Get a new deck of cards, and 
    Card currentCard;  // The current card, which the user sees.
    Card nextCard;   // The next card in the deck.  The user tries
    int correctGuesses ;  // The number of correct predictions the
    char guess;   // The user's guess.  'H' if the user predicts that
    deck.shuffle();  // Shuffle the deck into a random order before
    correctGuesses = 0;
    currentCard = deck.dealCard();
    System.out.println("The first card is the " + currentCard);
    while (true) {  // Loop ends when user's prediction is wrong.

      /* Get the user's prediction, 'H' or 'L' (or 'h' or 'l'). */

      System.out.println("Will the next card be higher (H) or lower (L)?  ");
      do { /// THE SECTION HERE IS THE SPECIFIED PROBLEM, THE IF AND ELSE STATEMENTS DONT DO ANYTHING!
        guess = (char)br.read();
        guess = Character.toUpperCase(guess);
        if (guess != 'H' && guess != 'L') 
          System.out.println("Please respond with H or L:  ");
      } while (guess != 'H' && guess != 'L');

      nextCard = deck.dealCard();
      System.out.println("The next card is " + nextCard);

      if(nextCard.getValue() == currentCard.getValue()) {
        if(guess == 'H') {
        if(nextCard.getSuit() > currentCard.getSuit()) {
          System.out.println("Your prediction was correct.");
          correctGuesses++;
         }
        }
        else {
          System.out.println("Your prediction was incorrect.");
          break;  // End the game.
        }
        if(guess == 'L') {
        if(nextCard.getSuit() < currentCard.getSuit()) {
         System.out.println("Your prediction was correct.");
          correctGuesses++;
        }


        }
        else {
          System.out.println("Your prediction was incorrect.");
          break;

        }
     } 
      else if (nextCard.getValue() > currentCard.getValue()) {
        if (guess == 'H') {
          System.out.println("Your prediction was correct.");
          correctGuesses++;
        }
        else {
          System.out.println("Your prediction was incorrect.");
          break;  // End the game.
        }
      }
      else {  // nextCard is lower
        if (guess == 'L') {
          System.out.println("Your prediction was correct.");
          correctGuesses++;
        }
        else {
          System.out.println("Your prediction was incorrect.");
          break;  // End the game.
        }
      }
      currentCard = nextCard;
      System.out.println();
      System.out.println("The card is " + currentCard);

    } // end of while loop

    System.out.println();
    System.out.println("The game is over.");
    System.out.println("You made " + correctGuesses 
                         + " correct predictions.");
    System.out.println();    
    return correctGuesses;
  }  
} 
4

3 回答 3

4

StringJava中的比较不是用==而是用String#equals

也就是说,而不是

if(input== "Y" || input =="y") {

你应该使用更像...

if("Y".equalsIgnoreCase(input)) {

更新...

还有一个永无止境的任务truetoplayAgain

} while (playAgain=true); 

这将分配trueplayAgain,这意味着永远无法退出循环。尝试使用类似...

} while (playAgain); 

...反而

于 2013-10-17T02:06:06.033 回答
2

您要比较的变量input是一个字符串。

字符串不能等同于使用==,您必须使用String#equals()

运算符检查对对象的 ==引用是否相等。看到这个帖子

在这种情况下,您可能需要使用String#equalsIgnoreCase()

另外如前所述,您需要修复while (playAgain=true);这是将变量分配为 trueplayAgain并将始终为 true,在这里您要使用==或仅使用变量本身(无需比较布尔值)

于 2013-10-17T02:06:22.717 回答
0

而(再次播放=真);

应该是 while (playAgain == true); ?

比较运算符不正确。

于 2013-10-17T02:09:39.740 回答