- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
不确定我是否需要编辑此类,因为卡片正在更改。只是不按顺序
import java.lang.Integer;
/**
* This class is used to keep track of playing cards.
*
* @author (name)
* @version (date)
*/
public class Card implements Comparable<Card>
{
public int compareTo(Card otherCard) {
// instance variables - replace the example below with your own
private String denom, suit;
/**
* Card is to be passed in as a denomination and suit
*/
public Card(String description)
{
description = description.toUpperCase();
if( description.length() == 2)
{
suit = description.substring(1);
denom = description.substring(0,1);
} else if(description.length() == 3) {
suit = description.substring(2);
denom = description.substring(0,2);
} else {
System.out.print("Error: An invalid card code was given.");
}
}
/**
* This will give a string that states a description of the card.
* @return Card description as a string.
*/
public String getDis()
{
//get the description
String denomMessage = denom;
if(denom.equals("A"))
denomMessage = "Ace";
else if(denom.equals("K"))
denomMessage = "King";
else if(denom.equals("Q"))
denomMessage = "Queen";
else if(denom.equals("J"))
denomMessage = "Jack";
//get the suit
String suitMessage = suit;
if(suit.equals("S"))
suitMessage = "Spades";
else if(suit.equals("D"))
suitMessage = "Dimonds";
else if(suit.equals("C"))
suitMessage = "Clubs";
else if(suit.equals("H"))
suitMessage = "Hearts";
else
suitMessage = "There was a problem";
return denomMessage + " of " + suitMessage;
}
/**
* This was written for the purpose of helping to select a card image.
* @return clubs are 1, hearts are 2, spades are 3, diamonds are 4
*/
public int numSuit()
{
int value = 0;
if(suit.equals("C"))
value = 1;
else if(suit.equals("H"))
value = 2;
else if(suit.equals("S"))
value = 3;
else if(suit.equals("D"))
value = 4;
return value;
}
/**
* This class was written for the purpose of selecting a card image
* @return ace is a 1, jack is a 11, queen is a 12, king is a 13
*/
public int numDenom()
{
int value = 0;
if(denom.equals("A"))
value = 1;
else if(denom.equals("J"))
value = 11;
else if(denom.equals("Q"))
value = 12;
else if(denom.equals("K"))
value = 13;
else
value = Integer.parseInt(denom);
return value;
}
/**
* Are the two cards the same suit and denomination?
* @return true or false
*/
public boolean equals(Card a)
{
if(denom.equals(a.denom) && suit.equals(a.suit))
return true;
else
return false;
}
/**
* I would suggest that you write this method
*/
public int denomCompareTo(Card a)
{
return a.numDenom() - numDenom();
}
}
它所做的只是将其卡更改为输入的最低卡。我必须按照从小到大的顺序排列。不管什么脸。
import java.awt.*;
import java.io.*;
import java.applet.*;
import java.awt.image.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* This applet is currently working. It will run in a webpage. It will take in card codes such as
* 2h for the two of heart and "paint" the image of five cards when all five text fields each have a
* card coding entered into them. Your assignment will be to write the part of the code
* that will sort the cards once they have been entered so that the will be "painted" from smallest
* to largest denomination regardless of suit as long "None" is selected in the drop down box. In
* the event one of the suits is selected that suit will be placed in order first then followed by
* the rest of the cards in order. To complete the assignment you should read through this class' code
* but you will only need to change the section that state that you should change it and possibly
* the card class.
*
* @author (Put your name here.)
* @version (Put the date here.)
*/
public class CardApplet extends Applet {
Image ic1,ic2,ic3,ic4,ic5;
Card c1,c2,c3,c4,c5;
private TextField cardIn1,cardIn2,cardIn3,cardIn4,cardIn5;
private String message;
private Button enter,sort;
private ButtonListener buttonListen;
private Choice trump;
static final int CARD_WIDTH = 73;
static final int CARD_HEIGHT = 98;
/**
* This is called an inner class as it is a class writen inside of another class. It is here so
* that the buttons will be able to trigger an event.
*/
class ButtonListener implements ActionListener
{
/**
* The name of this method is important and should not be changed. This will take in the
* "action" of a button being pushed and store reference to it in the object variable e.
*/
public void actionPerformed(ActionEvent e)
{
String action = e.paramString();
if( action.indexOf("Enter") >= 0)
{
//get text
String text1 = cardIn1.getText();
String text2 = cardIn2.getText();
String text3 = cardIn3.getText();
String text4 = cardIn4.getText();
String text5 = cardIn5.getText();
//Get rid of whitespace before and after string
text1 = text1.trim();
text2 = text2.trim();
text3 = text3.trim();
text4 = text4.trim();
text5 = text5.trim();
message = "Cards Entered";
//setup cards and card images
c1 = new Card(text1);
ic1 = getCardImage(c1);
c2 = new Card(text2);
ic2 = getCardImage(c2);
c3 = new Card(text3);
ic3 = getCardImage(c3);
c4 = new Card(text4);
ic4 = getCardImage(c4);
c5 = new Card(text5);
ic5 = getCardImage(c5);
//this method call is to this class and tells the applet to follow the code of paint again.
repaint();
}
else if( action.indexOf("Sort") >= 0)
{
//ADD YOUR CODE HERE.
if(c1.denomCompareTo(c2) < 0 )
ic1 = ic2;
c1 = c2;
if(c1.denomCompareTo(c3) < 0 )
ic1 = ic3;
c1 = c3;
if(c1.denomCompareTo(c4) < 0 )
ic1 = ic4;
c1 = c4;
if(c1.denomCompareTo(c5) < 0 )
ic1 = ic5;
c1 = c5;
if(c2.denomCompareTo(c1) < 0 )
ic2 = ic1;
c2 = c1;
if(c2.denomCompareTo(c3) < 0 )
ic2 = ic3;
c2 = c3;
if(c2.denomCompareTo(c4) < 0 )
ic2 = ic4;
c2 = c4;
if(c2.denomCompareTo(c5) < 0 )
ic2 = ic5;
c2 = c5;
if(c3.denomCompareTo(c1) < 0 )
ic3 = ic1;
c3 = c1;
if(c3.denomCompareTo(c2) < 0 )
ic3 = ic2;
c3 = c2;
if(c3.denomCompareTo(c4) < 0 )
ic3 = ic4;
c3 = c4;
if(c3.denomCompareTo(c5) < 0 )
ic3 = ic5;
c3 = c5;
if(c4.denomCompareTo(c1) < 0 )
ic4 = ic1;
c4 = c1;
if(c4.denomCompareTo(c2) < 0 )
ic4 = ic2;
c4 = c2;
if(c4.denomCompareTo(c3) < 0 )
ic4 = ic3;
c4= c3;
if(c4.denomCompareTo(c5) < 0 )
ic4 = ic5;
c4 = c5;
if(c5.denomCompareTo(c1) < 0 )
ic5 = ic1;
c5 = c1;
if(c5.denomCompareTo(c2) < 0 )
ic5 = ic2;
c5 = c2;
if(c5.denomCompareTo(c3) < 0 )
ic5 = ic3;
c5 = c3;
if(c5.denomCompareTo(c4) < 0 )
ic5 = ic4;
c5 = c4;
//DO NOT CHANGE CODE PAST THIS LINE.
message = "Sorted";
repaint();
}
}
} //end of inner class.
/**
* This method is called when the applet is first started. It will setup the layout of the applet.
*/
public void init() {
//This is the text that prints in the gray box towards the bottem of the applet.
message="Let us get started ";
//Sets the back ground color of the the applet
setBackground(Color.GREEN);
// Set default layout manager
setLayout(new FlowLayout() );
//setup textboxes for entering in cards
cardIn1 = new TextField("Enter",4);
add(cardIn1);
cardIn2 = new TextField("cards ",4);
add(cardIn2);
cardIn3 = new TextField("using",4);
add(cardIn3);
cardIn4 = new TextField("Chap 5",4);
add(cardIn4);
cardIn5 = new TextField("coding",4);
add(cardIn5);
//place buttons
buttonListen = new ButtonListener();
enter = new Button("Enter");
enter.addActionListener(buttonListen);
add(enter);
sort = new Button("Sort");
sort.addActionListener(buttonListen);
add(sort);
//setup dropdown
trump = new Choice();
trump.addItem("None");
trump.addItem("Hearts");
trump.addItem("Diamonds");
trump.addItem("Spades");
trump.addItem("Clubs");
add(trump);
//Since the card object variables are null each image object variable
//will hold reference to a card back image.
ic1 = getCardImage(c1);
ic2 = getCardImage(c2);
ic3 = getCardImage(c3);
ic4 = getCardImage(c4);
ic5 = getCardImage(c5);
}
/*
* This class is used to place graphics on an applet.
*/
public void paint(Graphics g) {
//places cards on applet
int linePos = 70;
g.drawImage(ic1,19,linePos,this);
g.drawImage(ic2,19+CARD_WIDTH,linePos,this);
g.drawImage(ic3,19+2*CARD_WIDTH,linePos,this);
g.drawImage(ic4,19+3*CARD_WIDTH,linePos,this);
g.drawImage(ic5,19+4*CARD_WIDTH,linePos,this);
// simple text displayed on applet
g.setColor(Color.lightGray);
g.draw3DRect(2, 175, 200, 20, true);
g.fillRect(2, 175, 200, 20);
g.setColor(Color.black);
g.drawString(message, 4, 190);
}
/**
* This will select either the correct portion of the cards image based on the suit and denomination or
* the card back image.
* @param a The card object holds the suit and denomination in state.
* @return It returns an image object variable with holds reference to a image that was created for a card that was passed in.
* @throws MalformedURLException
*/
public Image getCardImage(final Card a)
{
int cardDenom,cardSuit;
Image playingCards = null;
ImageFilter cardFilter;
ImageProducer cardProducer;
if( a == null)
{
playingCards = getImage(getCodeBase(), "cardBack.png");
}else{
playingCards = getImage(getCodeBase(),"cards.png");
cardDenom = (a.numDenom()*CARD_WIDTH)- CARD_WIDTH;
cardSuit = (a.numSuit()*CARD_HEIGHT) - CARD_HEIGHT;
cardFilter = new CropImageFilter(cardDenom,cardSuit,CARD_WIDTH,CARD_HEIGHT);
cardProducer = new FilteredImageSource(playingCards.getSource(),cardFilter);
playingCards = createImage(cardProducer);
}
return playingCards;
}
}
最佳答案
nIcE cOw 提到“可比较”是正确的
来自http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html
将此对象与指定对象进行比较以了解顺序。当此对象小于、等于或大于指定对象时,返回负整数、零或正整数。
这意味着您的对象(卡)的 Comparable 实现应该遵循该约定。
在您的顶部代码部分
public int denomCompareTo(Card a)
{
if ( a.numDenom() < this.numDenom() ) //the value of card a is less
{ return -1; }
else if ( a.numDenom() == this.numDenom() ) //equality
{ return 0; }
else //otherwise.. whatever's left (a must be greater)
{ return 1;}
}
仔细阅读,直到您理解为止。比较是你想要的绝对有效的方法。它将通过调用
来调用FirstCard.CompareTo( SecondCard );
该行代码将返回 -1、0 或 1 的值。具体取决于哪个值较大,如果相等则返回 0。这就是排序算法真正/需要/能够确定排序的全部内容。
这就是 Comparable 所需的代码。请仔细阅读直至理解为止。
第二部分的代码,
else if( action.indexOf("Sort") >= 0)
{
//ADD YOUR CODE HERE.
...
}
需要更新以反射(reflect) Comparable 的工作原理。
所以我猜这是一个按钮或操作,如果您指定排序,那么我们要对卡片进行排序。很公平。那么,卡片存放在哪里?它看起来像是图像的一堆名为 c(number) 和 ic(number) 的变量。如c1和ic1,c2和ic2。将来,考虑使用数组。
所以您的代码对于这部分来说几乎是正确的!只是你需要一个临时值来存储旧卡。否则你每次都会覆盖它。
例如:第一个篮子是苹果,第二个篮子是葡萄。
如果你说“嘿,把第二个篮子里的东西放进第一个篮子里”,那么第一个篮子就变成了葡萄。第二个也是(仍然)葡萄。苹果去哪儿了?我们失去了它,因为计算机无情地忠实于你的指令。我们需要将旧卡存储在新变量中。
所以当你说
if(c1.denomCompareTo(c2) < 0 ) { //for the love of jobe please use braces for multiline if statements
ic1 = ic2;
c1 = c2;
}
原来的ic1和c1被覆盖并丢失。
您可以通过使用临时变量或交换变量来解决此问题,或者当您非常聪明时,使用 XOR(询问您的 Nerd friend )
Image tempImg;
Card tempCard;
if(c1.denomCompareTo(c2) < 0 ) { //meaning if card1 is less than card2
tempImg = ic1; //store card1 temporarily
tempCard = c1;
ic1 = ic2; //copy vals of card2 to card1 slots
c1 = c2;
ic2 = tempImg; //slide the original val of ic1 here
c2 = tempCard;
}
当然,您的排序算法是准确的,但如果卡片的顺序很奇怪,则需要多次通过。这就是为什么您可能必须多次循环这些指令的原因。您正在做一些通常称为“冒泡排序”的增量操作...如果您需要循环排序方面的帮助,请告诉我们。
以下是一些示例:
我正在发布一个非常快速的引用示例供您查看。不过理解起来并不难,一旦理解了,事情就会变得容易一些。
导入java.util.*;
enum Suit {
HEART,
DIAMOND,
CLUB,
SPADE
}
class Card implements Comparable<Card> {
private int rank;
private Suit suit;
public Card ( int rank, Suit suit ) {
this.rank = rank;
this.suit = suit;
}
public int getRank () {
return rank;
}
public void setRank ( int rank ) {
this.rank = rank;
}
public Suit getSuit () {
return suit;
}
public void setSuit ( Suit suit ) {
this.suit = suit;
}
@Override
public int compareTo ( Card anotherCard ) {
return this.rank - anotherCard.rank;
}
@Override
public String toString () {
return String.format ("Suit: %8s Rank: %8s", suit, rank);
}
}
public class CardsExample {
private static final int TOTAL_CARDS = 52;
private static final int CARDS_PER_SUIT = 13;
private static final int TOTAL_SUITS = 4;
private List<Card> deck;
public CardsExample () {
deck = new ArrayList<Card> ();
}
private void createDeck () {
for ( Suit suit : Suit.values () ) {
for ( int i = 0; i < CARDS_PER_SUIT; ++i ) {
deck.add ( new Card ( i, suit ) );
}
}
}
private void displayDeck () {
for ( Card card : deck ) {
System.out.println ( card );
}
}
private void performTask () {
createDeck ();
Collections.shuffle ( deck );
System.out.println ( "Before SORTING" );
displayDeck ();
Collections.sort ( deck );
System.out.println ( "After SORTING" );
displayDeck ();
}
public static void main ( String[] args ) {
new CardsExample ().performTask ();
}
}
编辑:
如果有人想对卡片进行排序,请按照其 Suit
进行排序。首先,然后可以使用Comparator
同样,在此示例中,不需要进行太多更改,只需更改 enum
部分,如下所示并提供 Comparator < Card >
的实现,在Card
内类,让 Collections.sort ( deck, suitComparator )
完成工作,如本例所示。
import java.util.*;
enum Suit {
HEART ( 0 ),
DIAMOND ( 1 ),
CLUB ( 2 ),
SPADE ( 3 );
private int value;
private Suit ( int value ) {
this.value = value;
}
public int retrieveValue () {
return value;
}
}
class Card implements Comparable < Card > {
private int rank;
private Suit suit;
public static Comparator < Card > suitComparator =
new Comparator < Card > () {
@Override
public int compare ( Card someCard, Card anotherCard ) {
return someCard.suit.retrieveValue () - anotherCard.suit.retrieveValue ();
}
};
public Card ( int rank, Suit suit ) {
this.rank = rank;
this.suit = suit;
}
public int getRank () {
return rank;
}
public void setRank ( int rank ) {
this.rank = rank;
}
public Suit getSuit () {
return suit;
}
public void setSuit ( Suit suit ) {
this.suit = suit;
}
@Override
public int compareTo ( Card anotherCard ) {
return this.rank - anotherCard.rank;
}
@Override
public String toString () {
return String.format ( "Suit: %8s Rank: %8s", suit, rank );
}
}
public class CardsExample {
private static final int TOTAL_CARDS = 52;
private static final int CARDS_PER_SUIT = 13;
private static final int TOTAL_SUITS = 4;
private List < Card > deck;
public CardsExample () {
deck = new ArrayList < Card > ();
}
private void createDeck () {
for ( Suit suit : Suit.values () ) {
for ( int i = 0; i < CARDS_PER_SUIT; ++i ) {
deck.add ( new Card ( i, suit ) );
}
}
}
private void displayDeck () {
for ( Card card : deck ) {
System.out.println ( card );
}
}
private void performTask () {
createDeck ();
Collections.shuffle ( deck );
System.out.println ( "Before SORTING" );
displayDeck ();
Collections.sort ( deck );
Collections.sort ( deck, Card.suitComparator );
System.out.println ( "After SORTING" );
displayDeck ();
}
public static void main ( String[] args ) {
new CardsExample ().performTask ();
}
}
祝你好运
关于java - 如何对卡片进行排序。 (我的代码有错吗?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28910405/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!