【发布时间】:2014-08-08 02:54:55
【问题描述】:
我正在尝试为执行以下操作的类编写代码:
- 询问要使用多少套牌
- 程序会在每次按下 Enter 时从卡组中生成一张未使用的卡片
- 当没有更多牌可以发牌时,程序会通知用户
- 程序允许用户再次播放
- 方法被广泛使用
到目前为止,我的代码如下所示:
import java.util.*;
public class IA3 {
@SuppressWarnings({ })
public static void play () {
}
@SuppressWarnings("resource")
public static void main(String[] args) {
boolean draw = true;
boolean pa = true;
Scanner console = new Scanner(System.in);
// TODO Auto-generated method stub
System.out.println("Hello! Please input the number of decks you would like to use:");
int decks = console.nextInt();
int t = decks * 52;
System.out.println("Your total amount of cards to use are " +t);
int a = 0;
do {
int[] deck = new int[t];
//declaration of suits and ranks
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
//Initializing of the cards
for (int i = 0; i < t; i++) {
deck[i] = i;
}
System.out.println("Press enter to draw a random card : "); //allows player to prompt the system for the next card
String input = console.nextLine();
a++;
if (input.equals("")) {
// Shuffles the cards
for (int i = 0; i < 1; i++) {
int index = (int)(Math.random() * t);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
for (int i = 0; i < 1; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println(rank + " of " + suit);
}
if (a>t) {
System.out.println ("No more cards available");
break;
}
System.out.println("Draw another card? (Yes or No) : ");
String again = console.nextLine();
if (again.equals("Yes")) {
continue;
}
if (again.equals("No")) {
draw = false;
System.out.println("Game over!");
}
System.out.println("Play again? (Yes or No) : ");
String plag = console.nextLine();
if (plag.equals("Yes")) {
continue;
}
if (plag.equals("No")) {
pa = false;
System.out.println("Thank you for playing!");
break;
} while (pa == true);
}
} while (draw == true);
}
}
当我尝试运行超过 1 套牌时会出现问题,有时会立即失败,而有时可能会运行 4 次交易然后失败。我似乎也无法让它再次运行;每当我输入“是”而不是再次播放时,它就会终止。如您所见,我没有方法(我对方法很迷茫)。任何帮助将不胜感激,谢谢!
【问题讨论】:
-
请正确格式化您的代码。很难阅读。
-
对不起,我真的不擅长整个编码。
-
在网上搜索“如何在(您的文本编辑器或 IDE)中格式化代码”