我已经在这个项目上工作了30多个小时,现在我几乎和我刚开始的时候一样迷失了。我终于能够打印出7行3张卡片,但我的程序仍然有一些问题。
如果你能提供任何帮助,我们将不胜感激。我知道有些代码不是很漂亮,但作业必须是这样的,这样我才能获得学分:/非常感谢您的帮助。
说明:这是一个文档,如果链接有效-> 指示文档程序要求:
您的程序必须使用以下技术生成自己的随机卡片:
每个值必须转换为上述示例中所做的格式。每当你的程序显示卡片时,他们必须像上面那样在单词"of“上排成一行。
您的程序必须按行处理卡片,并按列收集它们(3次才能使其正常工作)。
你的程序必须问玩家他/她想在比赛前打印出整个牌面。每张卡片必须按上面的格式打印出来。
您的程序必须询问玩家是否想再玩一次,并继续播放该玩家想玩的次数。
程序必须向玩家询问他/她的名字,并在游戏的整个过程中通过名字引用玩家的名字。
您的程序代码必须使用函数进行逻辑组织。您还必须使用提供的初学者文件。如果您不使用初学者文件,您将得到0 (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;
}
}发布于 2012-03-29 00:57:57
好吧,有几件事不对,所以我试着一点一点地帮你。第一种是你的打印甲板方法,它实际上不会打印任何东西.你也把错误的东西传给了它。再看看这个方法,看看你能不能先把它打印出来,然后再看看它为什么打印错误的东西
https://stackoverflow.com/questions/9917478
复制相似问题