萌新求救啊,大佬们帮帮我

90508-四夕 2019-04-29 10:36:49
题目是这样的: 编写程序,模拟从一副52张的牌中选择一张牌。程序应显示牌的大小(Ace、2、3、4、5、6、7、8、9、10、Jack、Queen、King)以及牌的花色(Clubs、Diamond、Heart、Spades)。
提示: 设1-13为黑桃Ace至King,14-26为红桃Ace至King,27-39为梅花Ace至King,40-52为方片Ace至King。随机生成1-52内的一个随机整数,根据以上规则判断并提示牌的花色和数字。 我的代码是这样的: package project; import java.util.Scanner; public class project_3 { public static void main(String[] args) { // TODO Auto-generated method stub int number = (int)(Math.random()*52 + 1); int [][] poker; poker = new int[5][14]; int i = 0; int j = 0; poker[0][0] = 1; poker[1][0] = 14; poker[2][0] = 27; poker[3][0] = 40; while (i < 4) { while (j < 13) { poker[i][j+1] = poker[i][j] + 1; j++; } j=0; i++; } int m = 0; int n = 0; do { while (j < 13) { if (poker[i][j] == number) { m = i; n = j; } j++; } j=0; i++; } while (i < 4); System.out.println("The randomNumber is " + number); figure(n); color(m); } public static void figure(int n) { if (n>0 && n<10) System.out.println("The number is " + n+1); if (n==0) System.out.println("The number is " + "Ace"); else if (n==10) System.out.println("The number is " + "Jack"); else if (n==11) System.out.println("The number is " + "Queen"); else if (n==12) System.out.println("The number is " + "King"); } public static void color(int m) { if (m==0) System.out.println("The color is Clubs"); else if (m==1) System.out.println("The color is Diamond"); else if (m==2) System.out.println("The color is Heart"); else if (m==3) System.out.println("The color is Spades"); } } 问题:我运行的时候m,n总是为0,不能返回对应的数组下标
...全文
166 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2019-04-29
  • 打赏
  • 举报
回复
如果已经有满意答案,请结贴。一楼右上角,结贴。
十八道胡同 2019-04-29
  • 打赏
  • 举报
回复
package com.demo;

public class project_3 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int number = (int)(Math.random()*52 + 1);
        System.out.println("number="+number);
        int[][] poker;
        poker = new int[5][14];
        int i = 0;
        int j = 0;
        poker[0][0] = 1;
        poker[1][0] = 14;
        poker[2][0] = 27;
        poker[3][0] = 40;
        while (i < 4) {
            while (j < 13) {
                poker[i][j + 1] = poker[i][j] + 1;
                j++;
            }
            j = 0;
            i++;
        }
        int m = 0;
        int n = 0;
        i=0;//设置i=0,否则此时i=4
        j=0;//设置i=0  修改了这里-------------
        do {
            while (j < 13) {
                if (poker[i][j] == number) {
                    m = i;
                    n = j;
                }
                j++;
            }
            j = 0;
            i++;
        }
        while (i < 4);
        System.out.println("The randomNumber is " + number);
        figure(n);
        color(m);
    }

    public static void figure(int n) {
        if (n > 0 && n < 10) System.out.println("The number is " + n + 1);
        if (n == 0) System.out.println("The number is " + "Ace");
        else if (n == 10) System.out.println("The number is " + "Jack");
        else if (n == 11) System.out.println("The number is " + "Queen");
        else if (n == 12) System.out.println("The number is " + "King");
    }

    public static void color(int m) {
        if (m == 0) System.out.println("The color is Clubs");
        else if (m == 1) System.out.println("The color is Diamond");
        else if (m == 2) System.out.println("The color is Heart");
        else if (m == 3) System.out.println("The color is Spades");
    }
}

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧