在这个java代码中我需要比较什么变量? [英] What variables do I have to compare in this java code?

查看:150
本文介绍了在这个java代码中我需要比较什么变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作与卡有关的申请。



西装被列为俱乐部,钻石,黑桃,红心



我想像这样订购:



在牌内,Ace是最高的,2是最低的,所以标准的王牌王后杰克很好。



我想使用compareTo方法。我根本不理解它。



我是一个视觉学习者,所以代码示例,像一步一步演练如何使用该方法将真正帮助我。它不必与我的代码相关,任何我可以学习,并尝试通过视觉学习实现将有助于在这一点上。



这里是我的代码远。如果有人可以显示我 - - 我应该实现这一点,这也将有所帮助。

  import java.util.Arrays; 

public class PlayingCard implements Comparable {

//类常量
public static final int ACE = 1;
public static final int KING = 13;
public static final int QUEEN = 12;
public static final int JACK = 11;
public static final String SPADES =spades;
public static final String CLUBS =clubs;
public static final String HEARTS =hearts;
public static final String DIAMONDS =diamonds;

//实例变量
private int rank;
private String suit;

//构造方法
public PlayingCard(){
this.rank = PlayingCard.QUEEN;
this.suit = PlayingCard.SPADES;
}

public PlayingCard(int rank,String suit){
this.rank = rank;
this.suit = suit;
Arrays.sort();
}

//变量
public void setRank(int rank){
this.rank = rank;
}
public void setSuit(String suit){
this.suit = suit;
}

//访问器
public int getRank(){
return this.rank;
}
public String getSuit(){
return this.suit;
}

public String toString(){
return PlayingCard.rankToString(this.rank)+of+ this.suit;
}

//类方法
public static String rankToString(int rank){
switch(rank){
case(1):return高手;
case(2):returntwo;
case(3):returnthree;
case(4):returnfour;
case(5):returnfive;
case(6):returnsix;
case(7):returnseven;
case(8):returneight;
case(9):returnnine;
case(10):returnten;
case(11):returnJack;
case(12):returnQueen;
case(13):returnKing;
}
returnINVALID;
}

public static void main(String [] args){
//生成一张扑克牌数组
PlayingCard [] deck = new PlayingCard [52] ;

String [] suits = {PlayingCard.CLUBS,PlayingCard.DIAMONDS,PlayingCard.SPADES,PlayingCard.HEARTS};

for(int i = 0; i for(int j = 0; j <13; j ++) // Make each card
deck [i * 13 + j] = new PlayingCard(j + 1,suits [i]);
}
}

//随机播放卡
for(int i = 0; i int newPos = (int)(Math.random()* 52);
PlayingCard temp = deck [i];
deck [i] = deck [newPos];
deck [newPos] = temp;
}

//打印卡
System.out.println(Shuffled Deck);
for(int i = 0; i< deck.length; i ++){
System.out.println(deck [i]);
}

//排序甲板
Arrays.sort(deck);

//打印卡
System.out.println(\\\
\\\
Sorted Deck);
for(int i = 0; i< deck.length; i ++){
System.out.println(deck [i]);
}
}
}


解决方案>

下面是 compareTo 对于 Integer 类的示例:

  public int compareTo(Object o){
return this.value - ((Integer)o).value;
}

这不是实际的实现,因为如果你正在处理一个小的负数和一个大的正数,结果可能溢出。



这减去了 int 这个对象的值和另一个对象的 int 值。想想为什么这个工作:




  • 如果两个数字相等,减法得到0,所以数字被理解为相等。 / li>
  • 如果大于 o ,则减法得到正值,所以这个被理解为更大。

  • 如果 o ,减法产生负值,因此 o 被理解为更大。



为了帮助这个数值方法,你应该给出适当顺序的整数值,而不是字符串值,并定义 ACE 为 14 而不是 1 ,因为ace大于国王是 13 。有了这个,你可以写一个结合两个比较策略的方法:

  public int compareTo(Object o){
PlayingCard other =(PlayingCard)o;
int result = this.suit - other.suit;
if(result!= 0)
return result;
return this.rank - other.rank;
}

这将首先比较套装,如果相同,



您必须将 compareTo 方法放入 PlayingCard 类,或者如果实现 Comparable ,它将不会编译。


I am making a card related application.

Suits are ranked Clubs, Diamonds, Spades, Hearts

I'd like to order them like this:

Within the cards, Ace is the highest, and 2 is the lowest, so the standard Ace King Queen Jack order will be fine.

I'm trying to use the compareTo method.I don't understand it...at all.

I'm a visual learner, so code examples, like a step by step walkthrough of how to use the method would -really- help me out. It doesn't have to be related to my code, anything that I can study and try to implement by learning it visually will help at this point.

Here's my code so far. If someone could show me -where- exactly i should implement this, that would also help.

import java.util.Arrays;

public class PlayingCard implements Comparable {

// Class Constants
public static final int ACE = 1;
public static final int KING = 13;
public static final int QUEEN = 12;
public static final int JACK = 11;
public static final String SPADES = "spades";
public static final String CLUBS = "clubs";
public static final String HEARTS = "hearts";
public static final String DIAMONDS = "diamonds";

// Instance Variables
private int rank;
private String suit;

// Constructor
public PlayingCard () {
this.rank = PlayingCard.QUEEN;
this.suit = PlayingCard.SPADES;
}

public PlayingCard (int rank, String suit) {
this.rank = rank;
this.suit = suit;
Arrays.sort();
}

// Mutators
public void setRank (int rank) {
this.rank = rank;
}
public void setSuit (String suit) {
this.suit = suit;
}

// Accessors
public int getRank () {
return this.rank;
}
public String getSuit () {
return this.suit;
}

public String toString () {
return PlayingCard.rankToString(this.rank) + " of " + this.suit;
}

//Class Method
public static String rankToString(int rank) {
switch (rank) {
case(1): return "Ace";
case(2): return "two";
case(3): return "three";
case(4): return "four";
case(5): return "five";
case(6): return "six";
case(7): return "seven";
case(8): return "eight";
case(9): return "nine";
case(10): return "ten";
case(11): return "Jack";
case(12): return "Queen";
case(13): return "King";
}
return "INVALID";
}

public static void main(String [] args) {
// Generate an array of playing cards
PlayingCard [] deck = new PlayingCard[52];

String [] suits = {PlayingCard.CLUBS, PlayingCard.DIAMONDS, PlayingCard.SPADES, PlayingCard.HEARTS};

for(int i = 0; i < suits.length; i++) { // Run through each suit
for (int j = 0; j < 13; j++) { // Make each card
deck[i*13 + j] = new PlayingCard(j+1, suits[i]);
}
}

// Shuffle cards
for(int i = 0; i < deck.length; i++) {
int newPos = (int)(Math.random()*52);
PlayingCard temp = deck[i];
deck[i] = deck[newPos];
deck[newPos] = temp;
}

// Print out cards
System.out.println("Shuffled Deck");
for(int i = 0; i < deck.length; i++) {
System.out.println(deck[i]);
}

// Sort the deck
Arrays.sort(deck);

// Print out cards
System.out.println("\n\nSorted Deck");
for(int i = 0; i < deck.length; i++) {
System.out.println(deck[i]);
}
}
}

解决方案

Here's an example for compareTo for the Integer class:

public int compareTo(Object o) {
    return this.value - ((Integer)o).value;
}

This is not the actual implementation, because if you are dealing with a small negative number and a large positive number, the result could overflow. However, this method is sufficient in many cases, including the OP's.

This subtracts the int value of this object and the int value of the other object. Think about why this works:

  • If the two numbers are equal, the subtraction yields 0, so the numbers are understood to be equal.
  • If this is greater than o, the subtraction yields a positive value, so this is understood to be greater.
  • If this is less than o, the subtraction yields a negative value, so o is understood to be greater.

To help with this numeric method, you should probably give the suits integer values ranked in the appropriate order instead of string values, and define ACE to be 14 instead of 1, because the ace is greater than the king, which is 13. With this, you can write a method that combines two comparing strategies:

public int compareTo(Object o) {
    PlayingCard other = (PlayingCard) o;
    int result = this.suit - other.suit;
    if (result != 0) 
        return result;
    return this.rank - other.rank;
}

This will first compare the suits, and if they are the same, it will compare the ranks.

You must place the compareTo method in the PlayingCard class, or it will not compile if you implement Comparable.

这篇关于在这个java代码中我需要比较什么变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆