我已经在这个项目上工作了30多个小时,现在我几乎和第一次开始时一样迷失了。我终于能够打印出7排3张牌,但我的程序仍有一些问题。
我需要将卡片放在“of”
当我调用我的PrintDeck()函数时,它应该打印出牌组中的所有牌,但它不会。
我不能为我的生活找出如何拿起卡片列以便做到这一点,如果用户选择第1列我应该第二次拿起它然后处理它们再次分为7行3列。然后让他们再次选择他们的卡片并做同样的事情。
非常感谢您提供的任何帮助。我知道有些代码不是很漂亮,但为了让我获得荣誉,它必须是分配方式:/非常感谢你的帮助。
说明:如果链接有效,这是一个文档----> instructions doc 计划要求:
您的程序必须使用以下技术生成自己的随机卡片组:
为每张卡生成一个随机整数
显示每张卡片的字符串值(“Ace of Diamonds”)
使用rand()函数生成整数卡值
使用srand(time(0))命令(程序开头只有一次),一旦程序经过测试并正常运行,就可以生成一个真正随机的套牌。
每个值必须转换为上例中的格式。每当你的程序显示卡片时,他们必须按照上面的单词“of”排列 您的程序必须逐行处理这些卡并按列进行拾取(3次以使其正常工作) 你的节目必须要求玩家他/她想要在播放前打印出整个牌组。每张卡必须按照上述格式打印出来 你的节目必须询问玩家他/她是否想再次玩,并继续玩玩家想要的次数 该程序必须向玩家询问他/她的名字,并在整个游戏过程中按名称引用玩家 您的程序代码必须使用函数进行逻辑组织。您还必须使用提供的启动器文件。如果您不使用启动文件,您将获得零(0)!
提供的初学者文件:CardtrickStarterFile.java如果这里贴的很奇怪就是一个pastebin链接:http://pastebin.com/rsZY2vKq
import java.util.Scanner;
import java.lang.String;
import java.util.Random;
public class CardTrickStarterFile {
private static Random rand = new Random();
private static String PlayAgain;
public static void main(String[] args) {
/* declare and initialize variables */
int column = 0, i = 0;
/* Declare a 52 element array of integers to be used as the deck of cards */
int[] deck = new int[52];
/* Declare a 7 by 3 array to receive the cards dealt to play the trick */
int[][] play = new int[7][3];
/* Declare a Scanner object for input */
Scanner input = new Scanner(System.in);
/* Generate a random seed for the random number generator. */
/* Openning message. Ask the player for his/her name */
System.out.println("\nHello, I am a computer program that is so smart");
System.out.println("I can even perform a card trick. Here's how.\n");
System.out.println("To begin the card trick type in your name: ");
String name = input.nextLine();
char firstL = name.charAt(0);
firstL = Character.toUpperCase(firstL);
name = replaceCharAt(name, 0, firstL);
/* Capitalize the first letter of the person's name. */
System.out.println("\nThank you " + name);
do
{
/* Build the deck */
BuildDeck(deck);
/* Ask if the player wants to see the entire deck. If so, print it out. */
System.out.println("Ok " + name + ", first things first. Do you want to see what ");
System.out.println("the deck of cards looks like (y/n)? ");
String SeeDeck = input.nextLine();
switch (SeeDeck)
{
case "y":
PrintDeck(deck);
break;
case "n":
System.out.println("Ok then let us begin.");
Deal(deck,play);
break;
default:
break;
}
System.out.printf("\n%s, pick a card and remember it...\n", name);
/* Begin the card trick loop */
for(i = 0; i < 3; i++)
{
/* Begin the trick by calling the function to deal out the first 21 cards */
/* Include error checking for entering which column */
do
{
/* Ask the player to pick a card and identify the column where the card is */
System.out.print("\nWhich column is your card in (0, 1, or 2)?: ");
column = input.nextInt();
} while(column < 0 || column > 2);
/* Pick up the cards, by column, with the selected column second */
}
/* Display the top ten cards, then reveal the secret card */
/* if the player wants to play again */
System.out.printf("%s, would you like to play again (y/n)? ", name);
String PlayAgain = input.nextLine();
}
while(PlayAgain == "y");
/* Exiting message */
System.out.println("\nThank you for playing the card trick!\n");
return;
}
public static String replaceCharAt(String s, int pos, char c) {
return s.substring(0,pos) + c + s.substring(pos+1);
}
public static void BuildDeck( int deck[])
{
int[] used = new int[52];
int i = 0;
int card = 0;
/* Generate cards until the deck is full of integers */
while(i < deck.length)
{
/* generate a random number between 0 and 51 */
card = rand.nextInt(52);
/* Check the used array at the position of the card.
If 0, add the card and set the used location to 1. If 1, generate another number */
if(used[card] == 0)
{
used[card] = 1;
deck[i] = card;
i++;
}
}
}
public static void PrintDeck( int deck[] )
{
for (int i=0; i < 52; i++){
PrintCard(i);
}
/* Print out each card in the deck */
}
public static void Deal( int deck[], int play[][] )
{
int card = 0;
/* deal cards by passing addresses of cardvalues from
the deck array to the play array */
System.out.println("\n Column 0 Column 1 Column 2");
System.out.println("=======================================================");
for(int row = 0; row < play.length; row++){
for(int col = 0; col < play[row].length; col++){
play[row][col]=deck[card];
card++;
System.out.printf("%s", PrintCard(play[row][col]) + " ");
}
System.out.println();
}
}
public static String PrintCard( int card )
{
int rank = (card % 13);
int suit = (card / 13);
String[] suits = { "Clubs", "Hearts", "Diamonds", "Spades" };
String[] ranks = { "King", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen"};
return (ranks[rank] + " of " + suits[suit]);
}
public static void PickUp( int deck[], int play[][], int column )
{
int card = 0, row = 0;
return;
}
public static void SecretCard( int deck[] )
{
int card = 0;
System.out.println("\nFinding secret card...");
for(card = 0; card < 10; card++)
PrintCard(deck[card]);
System.out.println("\nYour secret card is: ");
PrintCard(deck[card]);
return;
}
}
答案 0 :(得分:2)
好的,有一些问题,所以我会一点一点地帮助你。第一种是你的印刷套牌方法,它实际上并没有印刷任何东西......你也传递了错误的东西。再看看这个方法,看看你是否可以先打印一些东西,然后再看看它为什么打印错误的东西