在java中调用方法并在for循环中递增数组

在java中调用方法并在for循环中递增数组,java,random,Java,Random,嗨,我是编程新手,我的程序是一个使用math.random类的随机数生成器,我的代码有问题。我需要在randTest方法中调用randInt方法n次,每次递增与返回值对应的元素计数的计数。基本上,每次生成一个随机数时,counts元素都会增加 这是我目前的代码: public class RandNumGenerator { public static int RandInt(){ int n = (int) Math.random()*10; retu

嗨,我是编程新手,我的程序是一个使用math.random类的随机数生成器,我的代码有问题。我需要在randTest方法中调用randInt方法n次,每次递增与返回值对应的元素计数的计数。基本上,每次生成一个随机数时,counts元素都会增加

这是我目前的代码:

public class RandNumGenerator {

    public static int RandInt(){
        int n = (int) Math.random()*10;
        return n;
        }

    public static void randTest(int n){
        int [] counts = new int [10];

        for(int i=0;i<10;i++){
            RandInt();

            }
            }
    }
公共类随机数发生器{
公共静态int RandInt(){
int n=(int)Math.random()*10;
返回n;
}
公共静态测试(int n){
int[]计数=新的int[10];

对于(int i=0;i可能需要替换此行:

RandInt();
与:


另外,在randTest方法中,将所有的“10”替换为
n
,我有3个建议,可以让您更轻松

首先,for循环需要从1变为N

for(int i=0; i< n; i++)
第三,根据您的实际需求(我不确定这是否是您想要的),您可以使用映射来存储每个返回值的出现次数 像

Map myMap=newmap();
所以总的来说,看起来

Map myMap<Int,Int> = New Map<Int,Int>();
int returnNum;
for(int i=0; i<n; i++)
{
    returnNum = RandInt();
    if(myMap.get(returnNum) == null) //randomnumber hasnt been used et
    {
        myMap.put(returnNum,1);
    }
    else  //increase occurence of random number
    {
        myMap.put(returnNum,myMap.get(returnNum) +1);
    }
}
Map myMap=newmap();
int returnNum;

对于(int i=0;i我认为您想要计算每个数字中随机生成的数量

我还修复了rand函数,因为
(int)Math.random()*10
不是获取[0..9]范围内rand的好方法

public class RandNumGenerator {

    private Random rand = new Random();

    public int RandInt(){
        int n = rand.nextInt(10); //gives random in range [0..9]
        return n;
    }

    public void randTest(int n){
        int [] counts = new int [10];

        for(int i=0; i<10; i++){
            int r = RandInt();
            counts[r]++;
        }
    }
}
公共类随机数发生器{
private Random rand=new Random();
公共int RandInt(){
int n=rand.nextInt(10);//给出[0..9]范围内的随机值
返回n;
}
公共测试(int n){
int[]计数=新的int[10];

对于(inti=0;iJFPicard是正确的。我只是为您的RandInt函数提供了一些建议,这是我第一次使用它时得到的

如果您有一个类似于上面的RandInt函数的函数,该函数将计算值赋给一个变量,然后返回该变量,您可以立即返回该值,而无需先赋给变量。这有助于保持代码简洁,并提高可读性

此外,还可以使用Random类生成随机整数

import java.util.Random;

public class Main {

    // a random number generator seeded with the current time
    private static Random rand = new Random(System.currentTimeMillis()); 

    /**
     * RandInt returns a random number between 1 and 100
     * @return
     */
    public static int RandInt()
    {
        // notice how we can return the result immediately without having
        // to assign the value to a variable first
        return rand.nextInt(100) + 1;
    }

    public static void main(String[] args) {
        int[] counts = new int[10];
        for(int i = 0; i < counts.length; i++)
        {
            counts[i] = RandInt();
            System.out.println(counts[i]);
        }

        for(int i = 0; i < counts.length; i++)
        {
            System.out.printf("The random value for counts[%d] is %d\n", i, counts[i]);
        }
    }
}
import java.util.Random;
公共班机{
//以当前时间为种子的随机数生成器
private static Random rand=new Random(System.currentTimeMillis());
/**
*RandInt返回一个介于1和100之间的随机数
*@返回
*/
公共静态int RandInt()
{
//请注意,我们如何可以立即返回结果,而无需
//首先将值赋给变量的步骤
返回兰特nextInt(100)+1;
}
公共静态void main(字符串[]args){
int[]计数=新的int[10];
for(int i=0;i
为什么不干脆
int count=0;
然后你在for循环中做
count++
呢?我必须在这个项目中使用math.random类你确定他们说的是math.random类吗?因为那没有意义,你使用了
math.random
函数
Map myMap<Int,Int> = New Map<Int,Int>();
int returnNum;
for(int i=0; i<n; i++)
{
    returnNum = RandInt();
    if(myMap.get(returnNum) == null) //randomnumber hasnt been used et
    {
        myMap.put(returnNum,1);
    }
    else  //increase occurence of random number
    {
        myMap.put(returnNum,myMap.get(returnNum) +1);
    }
}
public class RandNumGenerator {

    private Random rand = new Random();

    public int RandInt(){
        int n = rand.nextInt(10); //gives random in range [0..9]
        return n;
    }

    public void randTest(int n){
        int [] counts = new int [10];

        for(int i=0; i<10; i++){
            int r = RandInt();
            counts[r]++;
        }
    }
}
import java.util.Random;

public class Main {

    // a random number generator seeded with the current time
    private static Random rand = new Random(System.currentTimeMillis()); 

    /**
     * RandInt returns a random number between 1 and 100
     * @return
     */
    public static int RandInt()
    {
        // notice how we can return the result immediately without having
        // to assign the value to a variable first
        return rand.nextInt(100) + 1;
    }

    public static void main(String[] args) {
        int[] counts = new int[10];
        for(int i = 0; i < counts.length; i++)
        {
            counts[i] = RandInt();
            System.out.println(counts[i]);
        }

        for(int i = 0; i < counts.length; i++)
        {
            System.out.printf("The random value for counts[%d] is %d\n", i, counts[i]);
        }
    }
}