如果在程序的2个部分中从未达到过和否则语句

时间:2013-10-17 02:01:46

标签: java class constructor

这是我整个计划的一小部分,由4个班级组成。类的目标是打印出具有数值和适合度的卡,用户比继续猜测下一张卡具有更高的数值。如果卡片相同,则套装方法按顺序确定最大套装// SPADES>>>> HEARTS>>>> CLUBS>>>> DIAMONDS(如卡片方法所示)。然而,问题出现在我的HighLowrev类中,在do while循环中,因为要求用户再次播放,如果用户以'y'响应,则程序继续,即使用户以'n'响应,程序也会继续。我已经尝试了查看布尔的进一步用法,但已经意识到我很确定它们是这样工作的。任何帮助将非常感激。

CARD CLASS

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;
  }  
} 

3 个答案:

答案 0 :(得分:4)

Java中的{p> String比较不是==,而是String#equals

即代替

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

你应该使用更像......的东西。

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

<强>更新...

trueplayAgain

也有永无止境的分配
} while (playAgain=true); 

这会将true分配回playAgain,这意味着永远不会退出循环。尝试使用像...这样的东西。

} while (playAgain); 

...代替

答案 1 :(得分:2)

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

无法使用==来区分字符串,您必须使用String#equals()

==运算符检查对象的引用是否相等。 See this post

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

同样如上所述,您需要修复while (playAgain=true);这是真正的变量playAgain,并且始终为true,在这里您要使用==或仅使用变量本身(不需要)比较布尔值)

答案 2 :(得分:0)

while(playAgain = true);

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

比较运算符不正确。