whileiftruesleep 2023-03-09 23:13 采纳率: 50%
浏览 179
已结题

Java关于Array的问题

我目前是刚学习java,因为在国外上学,讲解能力是在是只能用灾难来形容。所以来这里请求帮助。
目前已经学习了Scanner,radom,if类,while,do,for类。现在开始学习Array。但实在是不理解。也只能用以上的语句。其他的没学过不让用
这是问题,

img

如图所示,创建三个array,第三个array需要将前两个整合起来。对于这个我是实在不理解

这是第二问:

img

英文:Section2: In this section, you need to randomly shuffle the deck of cards created in section1 as
below:
Algorithm to shuffle the deck:
▪ Consider each card index i from 0 to 51
• Calculate a random index r between i and 51
• Exchange deck[i] with deck[r]

机器翻译:Section2:在本节中,您需要将section1中创建的牌组随机洗牌为
以下:

洗牌的算法:
▪ 考虑每张卡片索引 i 从 0 到 51
• 计算一个介于 i 和 51 之间的随机索引 r
• 交换 deck[i] 和 deck[r]

对于这个问题似乎是将卡牌随机排列。我看例子用了两个for 语句。将for语句嵌套在另一个里面。因此如果可以麻烦使用两个for来回答这个问题。

第三问是
英文:
Algorithm to play the game:

  1. Prompt the user to enter number of cards, which will represent the winning cards, and
    store it in variable cardNum
  2. The system randomly selects cardNum unique cards from the deck and store them in a
    new array called winningCards
  3. Create a counter variable called totalDrawnCards to hold the total number of drawn
    cards and initialize it to 0
  4. The user randomly draws cards one a time and find whether the drawn card match any
    of the winning cards or not.
  5. If the drawn card matches any of the winning cards, mark that card as selected.
  6. Increase totalDrawnCards by one.
  7. Repeat steps 4 through 6 until all the winning cards are drawn.

机器翻译:
玩游戏的算法:

  1. 提示用户输入代表中奖牌的牌数,以及
    将其存储在变量 cardNum 中
  2. 系统从牌组中随机选择cardNum唯一的牌,存入一个
    名为 winningCards 的新数组
  3. 创建一个名为totalDrawnCards的计数器变量来保存抽取的总数
    卡并将其初始化为0
  4. 用户每次随机抽一张牌,看抽到的牌是否匹配
    中奖卡与否。
  5. 如果抽到的牌与任何获胜牌相匹配,则将该牌标记为已选中。
  6. 将 totalDrawnCards 增加一。
  7. 重复步骤 4 到 6,直到抽出所有中奖牌。

这是输出的举例:

img

  • 写回答

9条回答 默认 最新

  • pzzhao 2023-03-10 00:13
    关注

    你不是已经学了random么。怎么还不能用呢?
    第一问你看看这样满足你不

    import java.util.Random;
    
    public class Test01 {
        public static void main(String[] args) {
            String[] suits = {"\u2660", "\u2663", "\u2665", "\u2666",};
            String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
            String[] deck = new String[suits.length * ranks.length];
    
            for (int i = 0; i < suits.length; i++) {
                for (int j = 0; j < ranks.length; j++) {
                    deck[i * ranks.length + j] = ranks[j] + suits[i];
                }
            }
            System.out.println("The original deck is arranged as below:");
            for (int i = 0; i < deck.length; i++) {
                System.out.print(deck[i]+" ");
            }
            System.out.println();
    
            Random random = new Random();
    
            for (int i = 0; i < deck.length; i++) {
                int r = random.nextInt(51);
                String temp = deck[i];
                deck[i] = deck[r];
                deck[r] = temp;
            }
            System.out.println("The shuffled deck is arranged as below:");
            for (int i = 0; i < deck.length; i++) {
                System.out.print(deck[i]+" ");
            }
            System.out.println();
    
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(8条)

报告相同问题?

问题事件

  • 系统已结题 3月24日
  • 已采纳回答 3月16日
  • 创建了问题 3月9日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效