Java 为什么我收到这个错误?“二元运算符'>'的错误操作数类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30374296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:30:47  来源:igfitidea点击:

Why am I getting this error? "bad operand types for binary operator '>'"

javablackHyman

提问by Isaac Hollis

I would like to know what it causing the error of "bad operand types for binary operator '>'" down below I have the codes for both my Handand Cardclasses. I've also specified the lines that are causing the error. Thanks for the help. This is for a BlackHyman project.

我想知道是什么导致了“二元运算符'>'的错误操作数类型”的错误,下面我有我的HandCard类的代码。我还指定了导致错误的行。谢谢您的帮助。这是一个 BlackHyman 项目。

HandClass

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

 }

The error is in the handclass in the lines

错误在行中的类中

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

and

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

This is the CardClass

这是卡片

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

采纳答案by Eran

getCardFace()returns a String. <and >operators exist only for numeric types.

getCardFace()返回一个字符串。<>运算符仅适用于数字类型。

You can use c1.getCardFace().compareTo(c.getCardFace()) < 0or c1.getCardFace().compareTo(c.getCardFace()) > 0instead, to compare the Strings according to their natural ordering.

您可以使用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()) ) {

would become

会成为

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

and

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

would become

会成为

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

回答by Krishanthy Mohanachandran

getCardFace()is returning String value but you can't use < , > , <= or >=for comparing String.

getCardFace()正在返回字符串值,但不能< , > , <= or >=用于比较字符串。

回答by MChaker

Don't use these operators <,>and ==to compare two Strings, instead use compareTomethod.

不要使用这些运算符<>==比较两个字符串,而是使用compareTo方法。

From Javadoc:

Javadoc

public int compareTo(String anotherString)

Compares two strings lexicographically. The comparison is based on the Unicodevalue of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareToreturns 0exactly when the equals(Object)method would return true.

public int compareTo(String anotherString)

字典顺序比较两个字符串。比较基于Unicode字符串中每个字符的 值。此 String 对象表示的字符序列按字典顺序与参数字符串表示的字符序列进行比较。如果此 String 对象按字典顺序位于参数字符串之前,则结果为负整数。如果此 String 对象按字典顺序跟在参数字符串之后,则结果为正整数。如果字符串相等,则结果为零;恰好在方法compareTo返回0equals(Object)返回true

An example of comparing two Strings

比较两个字符串的示例

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.");

Note:The compareTomethod is case sensitive i.e "java" and "Java" are two different strings if you use compareTomethod. String "java" is greater than "Java" as ASCII value of 'j' is greater than 'J'. If you wish to compare strings but ignoring the case then use compareToIgnoreCasemethod.

注:compareTo方法是大小写敏感的,即“Java”的和“Java”是两个不同的字符串,如果你使用的compareTo方法。字符串“java”大于“Java”,因为“j”的 ASCII 值大于“J”。如果您想比较字符串但忽略大小写,请使用compareToIgnoreCase方法。

public int compareToIgnoreCase(String str)

Compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of calling compareTowith normalized versions of the strings where case differences have been eliminated by calling Character.toLowerCase(Character.toUpperCase(character))on each character.

public int compareToIgnoreCase(String str)

字典顺序比较两个字符串,忽略大小写差异。此方法返回一个整数,其符号是compareTo使用字符串的规范化版本调用的符号,其中通过调用Character.toLowerCase(Character.toUpperCase(character))每个字符消除了大小写差异 。