如何用java中的二维数组完成这道题?

发布于 2024-10-02 08:12:32 字数 1149 浏览 10 评论 0原文

大家好,我正在阅读《Java 编程简介》一书,其中一个练习是这样的:

经验洗牌检查。跑步 计算实验来检查 我们的洗牌代码的工作原理是 做了广告。写一个程序 采用命令行的 ShuffleTest 参数 M 和 N,是否进行 N 次洗牌 已初始化的大小为 M 的数组 每次洗牌之前使用 a[i] = i,并且 打印一个 M-by-M 表,使得第 i 行 给出了我结束的次数 位于所有 j 的位置 j 。所有参赛作品 数组中的值应接近 N/M。

现在,这段代码只输出一个零块......

public class ShuffleTest2 {
  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    int N = Integer.parseInt(args[1]); 
    int [] deck = new int [M];

    for (int i = 0; i < M; ++i)
      deck [i] = i;

    int [][] a = new int [M][M];

    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M; j++) {
        a[i][j] = 0 ;

        for(int n = 0; n < N; n++) {
          int r = i + (int)(Math.random() * (M-i));
          int t = deck[r];
          deck[r] = deck[i];
          deck[i] = t;

          for (int b = 0; b < N; b++)
          {
            for (int c = 0; c < M; c++)
              System.out.print(" " + a[b][c]);
            System.out.println();
          }
        }
      }
    }
  }
}

我做错了什么? :(

谢谢

Hey guys, im working through the Introduction to Programming in Java book and one of the exercises is this:

Empirical shuffle check. Run
computational experiments to check
that our shuffling code works as
advertised. Write a program
ShuffleTest that takes command-line
arguments M and N, does N shuffles of
an array of size M that is initialized
with a[i] = i before each shuffle, and
prints an M-by-M table such that row i
gives the number of times i wound up
in position j for all j. All entries
in the array should be close to N/M.

Now, this code just outputs a block of zeros...

public class ShuffleTest2 {
  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    int N = Integer.parseInt(args[1]); 
    int [] deck = new int [M];

    for (int i = 0; i < M; ++i)
      deck [i] = i;

    int [][] a = new int [M][M];

    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M; j++) {
        a[i][j] = 0 ;

        for(int n = 0; n < N; n++) {
          int r = i + (int)(Math.random() * (M-i));
          int t = deck[r];
          deck[r] = deck[i];
          deck[i] = t;

          for (int b = 0; b < N; b++)
          {
            for (int c = 0; c < M; c++)
              System.out.print(" " + a[b][c]);
            System.out.println();
          }
        }
      }
    }
  }
}

What am i doing wrong? :(

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-10-09 08:12:32

那么a就像一段历史吗?就像你现在一样,它总是充满零,就像你初始化一样,你永远不会分配给它!在“洗牌”for循环之后,您需要设置

A[i][POSITION] = CARD_VALUE

意味着在第i次洗牌之后,卡CARD_VALUE处于位置POSITION。我不想向您提供所有细节,但它将需要另一个 for 循环,并且用于打印的嵌套 for 循环需要独立于任何其他循环,在其他所有操作完成时发生。

看起来您有一些关于 for 循环的事情需要仔细检查。手动或使用调试器跟踪程序流程,您会注意到其中一些大括号和代码块需要移动。

--试试这个--

public class ShuffleTest2 {

  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    int N = Integer.parseInt(args[1]); 
    int [] deck = new int [M];

    int [][] a = new int [M][M]; 

    for (int i = 0; i < M; i++) {  //initialize a to all zeroes
      for (int j = 0; j < M; j++) {
        a[i][j] = 0 ; 
      }
    }

    for(int i = 0; i < N; i++)   //puts the deck in order, shuffles it, and records. N times
    {
        for (int j = 0; j < M; j++)  //order the deck
          deck[j] = j;

        for(int j = 0; j < M; j++) {       //shuffle the deck (same as yours except counter name)
          int r = j + (int)(Math.random() * (M-j));
          int t = deck[r];
          deck[r] = deck[j];
          deck[j] = t;
        }

       for(int j = 0; j < M; j++)   //record status of this deck as described
       {
           int card_at_j = deck[j];  //value of card in position j
           a[card_at_j][j]++;        //tally that card_at_j occured in position j
       }
    }  //big loop ended

    for (int b = 0; b < M; b++)  //print loop.  a is MxM, so limit of N was wrong.
    {
        for (int c = 0; c < M; c++)
        {
           System.out.print(" " + a[b][c]);
           System.out.println();
        }
    }  //print loop ended
  }  //main() ended
 } //class ended

So a is like a history? As you are now it is always filled with zeroes just like you initialized, you never assign to it! After the "shuffling" for loop you need to set

A[i][POSITION] = CARD_VALUE

Meaning that after i-th shuffle, card CARD_VALUE is in position POSITION. I don't want to give you all the specifics, but it will take another for loop, and the nested for-loop for printing needs to be independent of any other loop, occuring when everything else is done.

Looks like you have a few things concerning the for-loops that you need to look over carefully. Trace the program flow manually or with a debugger and you'll notice that some of those braces and code blocks need to be moved.

--TRY THIS--

public class ShuffleTest2 {

  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    int N = Integer.parseInt(args[1]); 
    int [] deck = new int [M];

    int [][] a = new int [M][M]; 

    for (int i = 0; i < M; i++) {  //initialize a to all zeroes
      for (int j = 0; j < M; j++) {
        a[i][j] = 0 ; 
      }
    }

    for(int i = 0; i < N; i++)   //puts the deck in order, shuffles it, and records. N times
    {
        for (int j = 0; j < M; j++)  //order the deck
          deck[j] = j;

        for(int j = 0; j < M; j++) {       //shuffle the deck (same as yours except counter name)
          int r = j + (int)(Math.random() * (M-j));
          int t = deck[r];
          deck[r] = deck[j];
          deck[j] = t;
        }

       for(int j = 0; j < M; j++)   //record status of this deck as described
       {
           int card_at_j = deck[j];  //value of card in position j
           a[card_at_j][j]++;        //tally that card_at_j occured in position j
       }
    }  //big loop ended

    for (int b = 0; b < M; b++)  //print loop.  a is MxM, so limit of N was wrong.
    {
        for (int c = 0; c < M; c++)
        {
           System.out.print(" " + a[b][c]);
           System.out.println();
        }
    }  //print loop ended
  }  //main() ended
 } //class ended
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文