为什么我收到此错误? “二元运算符的错误操作数类型’>’”
我想知道它导致“二元运算符’>’的错误操作数类型错误的原因下面我有我的手和卡类的代码。 我还指定了导致错误的行。 谢谢您的帮助。 这是一个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 = 0 && position 0) { int pos = 0; // Position of minimal card. PlayingCard c = (PlayingCard)hand.elementAt(0); // Minumal card. for (int i = 1; i c.getCardFace() || (c1.getCardFace().equals(c.getCardFace()) && c1.getFaceValue() 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.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.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); } }
getCardFace()
返回一个String。 <
和>
运算符仅适用于数字类型。
你可以使用c1.getCardFace().compareTo(c.getCardFace()) < 0
或c1.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) ) {
getCardFace()
返回String值,但不能使用< , > , <= or >=
来比较String。
不要使用这些运算符<
, >
和==
来比较两个字符串,而是使用compareTo
方法。
来自Javadoc :
public int compareTo(String anotherString)
按字典顺序比较两个字符串。 比较基于字符串中每个字符的
Unicode
值。 此String对象表示的字符序列按字典顺序与参数字符串表示的字符序列进行比较。 如果此String对象按字典顺序排在参数字符串之前,则结果为负整数。 如果此String对象按字典顺序跟随参数字符串,则结果为正整数。 如果字符串相等,结果为零;compareTo
恰好在equals(Object)
方法返回true
时返回0
。
比较两个字符串的示例
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
方法区分大小写,例如,如果使用compareTo
方法,“java”和“Java”是两个不同的字符串。 字符串“java”大于“Java”,因为'j'的ASCII值大于'J'。 如果您希望比较字符串但忽略大小写,则使用compareToIgnoreCase
方法。
public int compareToIgnoreCase(String str)
按字典顺序比较两个字符串,忽略大小写差异。 此方法返回一个整数,其符号是调用
compareTo
的符号,其中字符串的规范化版本通过在每个字符上调用Character.toLowerCase(Character.toUpperCase(character))
来消除大小写差异。