Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给它分配一个随机数';使用java.util.random访问受尊敬的位置1000次_Java - Fatal编程技术网

给它分配一个随机数';使用java.util.random访问受尊敬的位置1000次

给它分配一个随机数';使用java.util.random访问受尊敬的位置1000次,java,Java,我试图给两个变量分配一个数字1-10。如果数字大于max,则使用该数字更新max;如果数字小于min,则使用更新min。始终使用生成的任何数字更新总数。我以为我说的对,但每当它产生的时候。然后在最后我必须平均它。这个数字不是随机的,而且是固定在同几个数字上的。不知道我做错了什么 package java_programming; import java.util.Random; public class Jp{ public static void main(String[] args

我试图给两个变量分配一个数字1-10。如果数字大于max,则使用该数字更新max;如果数字小于min,则使用更新min。始终使用生成的任何数字更新总数。我以为我说的对,但每当它产生的时候。然后在最后我必须平均它。这个数字不是随机的,而且是固定在同几个数字上的。不知道我做错了什么

package java_programming;
import java.util.Random;

public class Jp{
    public static void main(String[] args){
        //The Power Operation
        double power = Math.pow(5.0,3.0);

        //The Square Root Operation
        double root = Math.sqrt(12.0);

        //The Max Operation
        double Maxi = Math.max(23.4, 23.5);

        //The Min Operation
        double Mini = Math.min(23.4, 23.5);

        //The Random Opeartion
        double rand = Math.random();

        System.out.println("Results");
        System.out.println("Math.pow = " + power);
        System.out.println("Math.sqrt = " + root);
        System.out.println("Math.max = " + Maxi);
        System.out.println("Math.min = " + Mini);

        System.out.println("Math.random = " + rand);
        Jp rs = new Jp();
        System.out.println("Min value");

        rs.randomStudy();
    }

    public void randomStudy(){
        int total = 0;
        int max = -1;
        int min = 11;
        int iterations = 1000;
        Random ran = new Random();
        for(int i = 0; i < iterations; i++){
            int count = ran.nextInt(10);
            min = Math.min(count, min);
            max = Math.max(count, max);
            total += count;
        }
        System.out.println("Result of max: " + max);
        System.out.println("Result of min: " + min);
        System.out.println("Average: " + (1.0 * total / iterations));
    }
}
包java\u编程;
导入java.util.Random;
公开课{
公共静态void main(字符串[]args){
//电力运行
双功率=数学功率(5.0,3.0);
//平方根运算
双根=数学sqrt(12.0);
//最大操作
double Maxi=数学最大值(23.4,23.5);
//Min操作
double Mini=Math.min(23.4,23.5);
//随机操作
double rand=Math.random();
系统输出打印项次(“结果”);
System.out.println(“Math.pow=“+power”);
System.out.println(“Math.sqrt=“+root”);
System.out.println(“Math.max=“+Maxi”);
System.out.println(“Math.min=“+Mini”);
System.out.println(“Math.random=“+rand”);
Jp rs=新的Jp();
系统输出打印项次(“最小值”);
随机研究();
}
公共卫生研究(){
int-total=0;
int max=-1;
int min=11;
int迭代次数=1000次;
Random ran=新的Random();
对于(int i=0;i
仅查看
随机研究()

公共研究()
{
int-total=0;
int max=-1;
int min=11;
int i=0;
Random ran=新的Random();
而(i<1001)
{
对于(int count=1;countmax)
最大值=总数;
System.out.println(“最大值的结果:+max”);
System.out.println(“最小值的结果:+min”);
双倍平均值=总数/1000;
System.out.println(“平均值:+平均值”);
i++;
}
}
  • 初始化变量:total、max、min、i、ran
  • 循环1000次
    • 循环10次
      • 将计数设置为[0,10]
    • 检查并设置最小/最大值
    • 打印最小/最大/平均值

  • 使用while循环可以通过计数器完成,但通常是通过for循环完成的
  • 循环直到你得到一个随机数>=10没有多大意义,因为你只需要一个随机数
  • min/max可能是随机计数的min/max,而不是总数的min/max(使用Math.min/max更清晰)
  • 计算平均值时应使用双除法,而不是整数除法
  • 通常在打印结果时,需要的是整体结果,而不是计算的每个阶段
更新方法:

public void randomStudy()
{
    int total = 0;
    int max = -1;
    int min = 11;
    int iterations = 1000;
    Random ran = new Random();
    for(int i = 0; i < iterations; i++)
    {
        int count = ran.nextInt(11);
        min = Math.min(count, min);
        max = Math.max(count, max);
        total += count;
    }

    System.out.println("Result of max: " + max);
    System.out.println("Result of min: " + min);
    System.out.println("Average: " + (1.0 * total / iterations));
}
公共研究()
{
int-total=0;
int max=-1;
int min=11;
int迭代次数=1000次;
Random ran=新的Random();
对于(int i=0;i

问题如下:

[...]
while(i < 1001)
{
    for(int j = 0; j < 1000; j++)
    [...]
[…]
而(i<1001)
{
对于(int j=0;j<1000;j++)
[...]
这将循环1000*1000(1000000)次,这可能比您试图做的要多一些



要获取随机数[1,10]而不是[0,10],请使用
ran.nextInt(10)+1
代替()

仅查看
randomstudio()

公共研究()
{
int-total=0;
int max=-1;
int min=11;
int i=0;
Random ran=新的Random();
而(i<1001)
{
对于(int count=1;countmax)
最大值=总数;
System.out.println(“最大值的结果:+max”);
System.out.println(“最小值的结果:+min”);
双倍平均值=总数/1000;
System.out.println(“平均值:+平均值”);
i++;
}
}
  • 初始化变量:total、max、min、i、ran
  • 循环1000次
    • 循环10次
      • 将计数设置为[0,10]
    • 检查并设置最小/最大值
    • 打印最小/最大/平均值

  • 使用while循环可以通过计数器完成,但通常是通过for循环完成的
  • 循环直到你得到一个随机数>=10没有多大意义,因为你只需要一个随机数
  • min/max可能是随机计数的min/max,而不是总数的min/max(使用Math.min/max更清晰)
  • 计算平均值时应使用双除法,而不是整数除法
  • 通常在打印结果时,需要的是整体结果,而不是计算的每个阶段
更新方法:

public void randomStudy()
{
    int total = 0;
    int max = -1;
    int min = 11;
    int iterations = 1000;
    Random ran = new Random();
    for(int i = 0; i < iterations; i++)
    {
        int count = ran.nextInt(11);
        min = Math.min(count, min);
        max = Math.max(count, max);
        total += count;
    }

    System.out.println("Result of max: " + max);
    System.out.println("Result of min: " + min);
    System.out.println("Average: " + (1.0 * total / iterations));
}
公共研究()
{
int-total=0;
int max=-1;
int min=11;
int迭代次数=1000次;
Random ran=新的Random();
对于(int i=0;i

问题如下:

[...]
while(i < 1001)
{
    for(int j = 0; j < 1000; j++)
    [...]
[…]
而(i<1001)
{
对于(int j=0