您好我是java的新手,我一直在努力创建一个程序,让您可以从洗牌或订购的卡片中抽取卡片。
现在我想让用户选择套牌数量。我尝试了不同的方法,但无法让它发挥作用。
该计划(自从我用瑞典语撰写以来,我已将评论添加为翻译):
import java.util.Scanner;
public class KortSpel {
public static void main(String[] args) {
Boolean fortsatt = true;
while(fortsatt){
Scanner scan = new Scanner(System.in);
int[] deck = new int[52];
String[] suits = {"Spader", "Hjärter", "Ruter", "Köver"}; //the suits
String[] ranks = {"Ess", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Knäckt", "Drottning", "Kung"}; //the rank of the cards
for( int i = 0; i < deck.length; i++) deck[i] = i;
System.out.print("Skriv dra för att dra korten annars avsluta."); //ask the user if he want to keep playing or not
String svar2 = scan.nextLine();
if (svar2.equalsIgnoreCase("Avsluta")){
fortsatt = false;
System.out.println("Du har nu avslutat."); //tells the user he has exit
}
else {
System.out.print("Vill du bland korten? (ja/nej) "); //ask the user if he want to shuffle the cards Y/N
String svar = scan.nextLine();
if (svar.equalsIgnoreCase("ja")) { // if shuffled
for( int i = 0; i < deck.length; i++) {
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
for( int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Kort nummer " + deck[i] + ": " + suit + " " + rank);
}
}
else { //if not suffled
for( int i = 0; i < deck.length; i++) deck[i] = i;
for( int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Kort nummer " + deck[i] + ": " + suit + " " + rank);
}
}
}
}
答案 0 :(得分:0)
我不知道我是否理解正确,但您可能希望在程序运行期间从命令行读取数据。
如果是,则可以使用System.console()。
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
String deck = c.readLine("Enter additional deck: ");
您可以在此处找到更多信息:http://docs.oracle.com/javase/tutorial/essential/io/cl.html
答案 1 :(得分:0)
使您的应用程序动态化(1对n套牌)。您必须在编码之前处理应用程序设计。我会建议你这样的节点风格。像:
class Deck{
Deck nextDeck;
Deck periousDeck;
String[] card1=new String[13];
String[] card2=new String[13];
String[] card3=new String[13];
String[] card4=new String[13];
Deck(){
//initialization of cards
}
}
这是你的NODE,也有点复杂。
在进行手动链接列表READ
之前