首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >卡片技巧Java

卡片技巧Java
EN

Stack Overflow用户
提问于 2012-03-29 00:12:21
回答 1查看 2.8K关注 0票数 2

我已经在这个项目上工作了30多个小时,现在我几乎和我刚开始的时候一样迷失了。我终于能够打印出7行3张卡片,但我的程序仍然有一些问题。

  1. 我需要把卡片排在“……”这个词上
  2. 当我调用我的PrintDeck()函数时,它应该打印出甲板上的所有卡片,但它没有。
  3. 在我的一生中,我无法弄清楚如何拿起卡片的列来达到这个目的,如果用户选择第1列,我应该第二次拿起它,然后在7行3列中再把它们分发出去。然后让他们再挑选2张牌,然后做同样的事情。

如果你能提供任何帮助,我们将不胜感激。我知道有些代码不是很漂亮,但作业必须是这样的,这样我才能获得学分:/非常感谢您的帮助。

说明:这是一个文档,如果链接有效-> 指示文档程序要求:

您的程序必须使用以下技术生成自己的随机卡片:

  • 为每一张卡生成一个随机整数
  • 为每一张卡片显示一个字符串值(“钻石之一”)
  • 使用rand()函数生成整数卡值
  • 使用srand(time(0))命令(在程序开始时只使用一次),在程序测试并正常运行后生成一个真正的随机甲板。

每个值必须转换为上述示例中所做的格式。每当你的程序显示卡片时,他们必须像上面那样在单词"of“上排成一行。

您的程序必须按行处理卡片,并按列收集它们(3次才能使其正常工作)。

你的程序必须问玩家他/她想在比赛前打印出整个牌面。每张卡片必须按上面的格式打印出来。

您的程序必须询问玩家是否想再玩一次,并继续播放该玩家想玩的次数。

程序必须向玩家询问他/她的名字,并在游戏的整个过程中通过名字引用玩家的名字。

您的程序代码必须使用函数进行逻辑组织。您还必须使用提供的初学者文件。如果您不使用初学者文件,您将得到0 (0)!

启动文件提供: CardtrickStarterFile.java如果这个粘贴奇怪,这里是一个pastebin链接:http://pastebin.com/rsZY2vKq

代码语言:javascript
复制
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;
}
}
EN

回答 1

Stack Overflow用户

发布于 2012-03-29 00:57:57

好吧,有几件事不对,所以我试着一点一点地帮你。第一种是你的打印甲板方法,它实际上不会打印任何东西.你也把错误的东西传给了它。再看看这个方法,看看你能不能先把它打印出来,然后再看看它为什么打印错误的东西

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9917478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档