如何用java中的二维数组完成这道题?
大家好,我正在阅读《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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么a就像一段历史吗?就像你现在一样,它总是充满零,就像你初始化一样,你永远不会分配给它!在“洗牌”for循环之后,您需要设置
意味着在第i次洗牌之后,卡CARD_VALUE处于位置POSITION。我不想向您提供所有细节,但它将需要另一个 for 循环,并且用于打印的嵌套 for 循环需要独立于任何其他循环,在其他所有操作完成时发生。
看起来您有一些关于 for 循环的事情需要仔细检查。手动或使用调试器跟踪程序流程,您会注意到其中一些大括号和代码块需要移动。
--试试这个--
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
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--