【发布时间】:2015-09-14 19:04:51
【问题描述】:
我正在编写一个程序,它接受用户输入来打印卡片的完整描述。 (即 4S = 黑桃四) 完成 Card 类后,我尝试使用它通过 getDescription 方法返回卡片的描述,但它不会编译。
import java.util.*;
public class CardDescription {
public static void main(String[] args) {
Card c1 = new Card("KS");
System.out.println("Your card is" + c1.getDescription());
}}
public class Card {
private String value;
private String suite;
private String string1;
private String string2;
public Card(String s1){
value = s1.substring(0, 1);
suite = s1.substring(1);
}
public String getDescription(){
if (value.equalsIgnoreCase("A")){
string1 = "Ace";
}
else if(value.equalsIgnoreCase("K")){
string1 = "King";
}
else if(value.equalsIgnoreCase("Q")){
string1 = "Queen";
}
else if(value.equalsIgnoreCase("J")){
string1 = "Jack";
}
else if(value.equals("10")){
string1 = "Ten";
}
else if(value.equals("9")){
string1 = "Nine";
}
else if(value.equals("8")){
string1 = "Eight";
}
else if(value.equals("7")){
string1 = "Seven";
}
else if(value.equals("6")){
string1 = "Six";
}
else if(value.equals("5")){
string1 = "Five";
}
else if(value.equals("4")){
string1 = "Four";
}
else if(value.equals("3")){
string1 = "Three";
}
else if(value.equals("2")){
string1 = "Two";
}
if(suite.equalsIgnoreCase("D")){
string2 = "Diamonds";
}
else if(suite.equalsIgnoreCase("H")){
string2 = "Hearts";
}
else if(suite.equalsIgnoreCase("S")){
string2 = "Spades";
}
else if(suite.equalsIgnoreCase("C")){
string2 = "Clubs";
}
return string1 + " of " + string2;
}
}
感谢您的帮助。
【问题讨论】:
-
从“公共类卡”中删除公共关键字
-
另外看看使用
switch语句而不是多个else if。