gpt4 book ai didi

java - 这个方法在做什么? (数组和随机数)

转载 作者:行者123 更新时间:2023-12-02 12:00:06 24 4
gpt4 key购买 nike

我的教科书给了我这段代码来帮助计算某个数字在整数数组中出现的次数。我尝试将教科书给我的代码应用到我的作业中,但它似乎不起作用。基本上,我必须在一个数组中生成 30 个随机整数,上限为 15,下限为 -5。

我想找出数组中的一个数字有多少次等于0、1、2...一直到10。第一个代码是我教科书给我的代码。他们还使用了随机数生成器,但他们想要查找每个数字出现的次数,而不是查找有多少个元素等于 0、1 等。 (分数数组只是随机数生成器,其上限为 100)。第二个代码是我的。

int[] counts = new int [100];
for (int i = 0; i < scores.length; i++) {
int index = scores[i];
counts[index]++;
}


//This is my code
public static void main(String[] args) {
int []a = arrayHist ();
printArray (a);
}

public static int randomInt (int low, int high) {
int range = (high - low) +1;
return (int) (Math.random() * range) + low;

}

public static int[] randomIntArray (int x) {
int[] random = new int[x];
for (int i = 0; i< x; i++) {
random [i] = randomInt (-5, 15);

}
return random;
}

public static int[] arrayHist () {
int[] counts = new int [30];
int[] hist = randomIntArray (30);
for (int i = 0; i < 10 && i >= 0; i++) {
int index = hist[i];
counts[index]++;
}
return hist;
}


public static void printArray (int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println (a[i]);
}
}

我应该只得到 11 个元素,但我又得到了 30 个随机数。这是为什么?

最佳答案

我会在你的代码中添加一些注释,看看你是否能发现哪里出了问题:

//take a histogram of the array. We're only going to count values between 0 and 10
//so 25th to 75 centiles, ignoring values that are lower than 0 or higher than 10
public static int[] arrayHist () {

//need to make an array of 11 numbers for the counts
int[] counts = new int [30];

//get an array of 30 random numbers
int[] hist = randomIntArray (30);

//loop over the whole array of 30 numbers
for (int i = 0; i < 10 && i >= 0; i++) {
//retrieve the random number into a variable temporarily
int index = hist[i];

//if the value is too low or too high, skip it

//else, store it in the counts array - the value from the random array
//serves as the index position in the counts array
counts[index]++;
}

//return the counts array
return hist;
}

我对评论所做的相当于使用你所思考的语言(英语)设计算法,然后你可以将其翻译成你正在学习的语言(java)。很少有开发人员用他们编写的编程语言进行思考。作为一名学生,我建议您在在注释下编写代码之前,应该始终编写注释来向自己解释您的算法。你会因为写注释而获得积分(通常),所以如果你先写注释,那么 a) 它可以帮助你编写代码,b) 在代码运行后你就不用再做繁琐的注释工作了

为了您自己的利益/学习,请在查看下面的剧透(答案)之前尝试找出上面的错误所在。将鼠标悬停在框上可显示剧透

 //loop over the whole array of 30 numbers - YOU ONLY LOOP 10
for (int i = 0; i < 10 && i >= 0; i++) {
//if the value is too low or too high, skip it - YOU DIDN'T DO THIS CHECK
...
}
//return the counts array - YOU RETURNED THE WRONG ARRAY
return hist;

针对评论进行的编辑:

检查范围

您必须检查两个限制,因此它需要采用以下形式之一:

if(x < 0 || x > 10) then don't do the count

if(!(x >= 0 && x <= 10)) then don't do the count

if(x >= 0 && x <= 10) then do the count

if(!(x < 0 || x > 10)) then do the count

使用 NOT - 感叹号的测试 ! - 通常有点难以阅读和理解,因此可以尝试避免它们。 “积极的”测试 - 即它们返回积极的结果而不是需要否定的消极结果 - 更容易阅读和理解。

在错误检查方面,循环和方法的一个有用提示是测试满足某些条件的错误值,如果遇到错误值,则跳过处理循环的其余部分(使用 continue ) 关键字,或跳过该方法的其余部分(通过 return 从中获取)

这样做意味着你的 if 主体( { 和 } 之间的位)不会变得很大。比较:

for(...){
if(test for bad values)
continue;

//50 lines long loop body
}

比做更整洁:

for(...){
if(test for goodvalues){
//50 lines long loop body
}
}

如果您使用底部模式,您可能会在几个 IF 之后陷入真正的缩进困惑,{ 和 } 遍布整个地方,并且您的代码会远远超出屏幕的右侧:

    for(...){
//code
if(...){
//code
if(...){
//code
if(...){
//code
if(...){
//code
if(...){
//code
if(...){
//code
}
//code
}
//code
}
//code
}
//code
}
//code
}
//code
}

将缩进级别保持在最低水平有助于提高代码的可读性

因此,我建议在您的情况下,不要测试 0 到 10 范围内的值并对其进行处理,而是采用“如果值超出 0 到 10 范围”的形式,跳过执行循环的其余部分

关于java - 这个方法在做什么? (数组和随机数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47329274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com