2015-05-21 95 views
1

我想知道它是什麼導致了」二元運算符的壞操作數類型「錯誤」>「」下面我有我的代碼類。我還指定了導致錯誤的行。 感謝您的幫助。這是一個BlackJack項目。爲什麼我得到這個錯誤? 「二元運算符的錯誤操作數類型」>'「

import java.util.Vector; 

public class Hand { 

private Vector hand; // The cards in the hand. 

public Hand() { 
     // Create a Hand object that is initially empty. 
    hand = new Vector(); 
} 

public void clear() { 
    // Discard all the cards from the hand. 
    hand.removeAllElements(); 
} 

public void addCard(PlayingCard c) { 
    // Add the card c to the hand. c should be non-null. (If c is 
    // null, nothing is added to the hand.) 
    if (c != null) 
    hand.addElement(c); 
} 

public void removeCard(PlayingCard c) { 
    // If the specified card is in the hand, it is removed. 
    hand.removeElement(c); 
} 

public void removeCard(int position) { 
    // If the specified position is a valid position in the hand, 
    // then the card in that position is removed. 
    if (position >= 0 && position < hand.size()) 
    hand.removeElementAt(position); 
} 

public int getCardCount() { 
    // Return the number of cards in the hand. 
    return hand.size(); 
} 

public PlayingCard getCard(int position) { 
     // Get the card from the hand in given position, where positions 
     // are numbered starting from 0. If the specified position is 
     // not the position number of a card in the hand, then null 
     // is returned. 
    if (position >= 0 && position < hand.size()) 
    return (PlayingCard)hand.elementAt(position); 
    else 
    return null; 
} 

public void sortBySuit() { 
    // Sorts the cards in the hand so that cards of the same suit are 
    // grouped together, and within a suit the cards are sorted by value. 
    // Note that aces are considered to have the lowest value, 1. 
    Vector newHand = new Vector(); 
    while (hand.size() > 0) { 
    int pos = 0; // Position of minimal card. 
    PlayingCard c = (PlayingCard)hand.elementAt(0); // Minumal card. 


    for (int i = 1; i < hand.size(); i++) { 
     PlayingCard c1 = (PlayingCard)hand.elementAt(i); 

     *if (c1.getCardFace() > c.getCardFace() || 
       (c1.getCardFace().equals(c.getCardFace()) && c1.getFaceValue() < c.getFaceValue())) {* 
      pos = i; 
      c = c1; 
     } 
    } 
    hand.removeElementAt(pos); 
    newHand.addElement(c); 
    } 
    hand = newHand; 
} 

public void sortByValue() { 
    // Sorts the cards in the hand so that cards of the same value are 
    // grouped together. Cards with the same value are sorted by suit. 
    // Note that aces are considered to have the lowest value, 1. 
    Vector newHand = new Vector(); 
    while (hand.size() > 0) { 
    int pos = 0; // Position of minimal card. 
    PlayingCard c = (PlayingCard)hand.elementAt(0); // Minumal card. 
    for (int i = 1; i < hand.size(); i++) { 
     PlayingCard c1 = (PlayingCard)hand.elementAt(i); 

     *if (c1.getFaceValue() < c.getFaceValue() || 
       (c1.getFaceValue() == c.getFaceValue() && c1.getCardFace() > c.getCardFace())) {* 
      pos = i; 
      c = c1; 
     } 
    } 
    hand.removeElementAt(pos); 
    newHand.addElement(c); 
    } 
    hand = newHand; 
} 

} 

的錯誤是在線

if (c1.getCardFace() > c.getCardFace() || 
       (c1.getCardFace().equals(c.getCardFace()) &&  c1.getFaceValue() < c.getFaceValue())) { 

if (c1.getFaceValue() < c.getFaceValue() || 
       (c1.getFaceValue() == c.getFaceValue() && c1.getCardFace() > c.getCardFace())) { 

類這是

public class PlayingCard 
{ 
// Instance Data - all things common to all cards 
private String cardFace; // king, q, j, 10 - 2, A 
private int faceValue; // numberic value of the card 
private char cardSuit; // hold suit of the card 
private char suits[] = {(char)(003), (char)(004), (char)(005), (char)(006)}; 

// Constructor 
public PlayingCard(int value, int suit) 
{ 
    faceValue = value; 
    setFace(); 
    setSuit(suit); 
} 

// helper setFace() 
public void setFace() 
{ 
    switch(faceValue) 
    { 
     case 1: 
      cardFace = "A"; 
      faceValue = 14; 
      break; 
     case 11: 
      cardFace = "J"; 
      break; 
     case 12: 
      cardFace = "Q"; 
      break; 
     case 0: 
      cardFace = "K"; 
      faceValue = 13; 
      break; 
     default: 
      cardFace = ("" + faceValue); 
    } 
} 

public void setSuit(int suit) // suit num between 0 and 3 
{ 
    cardSuit = suits[suit]; 
} 

// other helpers 
public int getFaceValue() 
{ 
    return faceValue; 
} 
public String getCardFace() 
{ 
    return cardFace; 
} 

public String toString() 
{ 
    return (cardFace + cardSuit); 
} 
} 

回答

3

getCardFace()返回一個字符串。 <>運算符僅適用於數值類型。

您可以使用c1.getCardFace().compareTo(c.getCardFace()) < 0c1.getCardFace().compareTo(c.getCardFace()) > 0來根據字符串的自然順序來比較字符串。

if (c1.getCardFace() > c.getCardFace() || 
       (c1.getCardFace().equals(c.getCardFace()) &&  c1.getFaceValue() < c.getFaceValue())) { 

將成爲

if (c1.getCardFace().compareTo(c.getCardFace()) > 0 || 
       (c1.getCardFace().equals(c.getCardFace()) &&  c1.getFaceValue() < c.getFaceValue())) { 

if (c1.getFaceValue() < c.getFaceValue() || 
       (c1.getFaceValue() == c.getFaceValue() && c1.getCardFace() > c.getCardFace())) { 

將成爲

if (c1.getFaceValue() < c.getFaceValue() || 
       (c1.getFaceValue() == c.getFaceValue() && c1.getCardFace().compareTo(c.getCardFace()) > 0)) { 
0

getCardFace()將返回字符串值,但你不能compari使用< , > , <= or >= ng字符串。

0

請勿使用這些運算符<,>==來比較兩個字符串,而使用compareTo方法。

Javadoc

公衆詮釋的compareTo(字符串anotherString)

比較兩個字符串字典順序。該比較是基於字符串中每個字符的值的 Unicode。由此String對象表示的字符序列 按字典順序與參數字符串表示的 字符序列進行比較。如果此字符串對象按照字典順序排列在參數字符串 之前,結果爲 負整數。如果此字符串 對象按字典順序跟隨參數字符串,則結果爲正整數。如果字符串相等,結果爲 ; compareTo返回0恰好當 equals(Object)方法將返回true

比較兩個字符串

String s1="example1", s2="example2"; 
if (s1.compareTo(s2) > 0) 
    System.out.println("First string is greater than second."); 
else if (s1.compareTo(s2) < 0) 
     System.out.println("First string is smaller than second."); 
else 
     System.out.println("Both strings are equal."); 

注意的一個例子:compareTo方法是區分大小寫即的「Java」和「Java」是兩個不同的字符串,如果你使用compareTo方法。由於'j'的ASCII值大於'J',因此字符串「java」大於「Java」。如果你想比較字符串,但忽略情況,然後使用compareToIgnoreCase方法。

公衆詮釋與compareToIgnoreCase(字符串str)

比較兩個字符串字典順序,忽略大小寫差異。 此方法返回一個整數,其標誌是主叫compareTo 與在殼體差異具有 串的歸一化版本的通過在每個 字符調用 Character.toLowerCase(Character.toUpperCase(character))被淘汰。

相關問題