0

作为一个项目,我必须为我的 CS 入门课程编写一个程序。程序必须

  1. 向用户询问最小值(可以是负数)
  2. 向用户询问最大值(也可能是负数,但必须大于最小值)
  3. 产生 20 个介于用户给出的最小值/最大值之间的随机数,并且
  4. 产生产生的二十个随机数的平均值。

我以为我已经完成了程序,但是当我执行程序时,数字从来都不是随机的,所有二十个数字都与我输入 emacs 的最大值相同。当然,在这种情况下,平均值也与最大值相同。到目前为止,这是我的程序...

import java.util.Scanner;
import java.util.Random;

public class CS_ProjectOne
{
    public static void main(String[] args)
    {
    int minimum_integer;
    int maximum_integer;
    int rn1;
    int rn2;
    int rn3;
    int rn4;
    int rn5;
    int rn6;
    int rn7;
    int rn8;
    int rn9;
    int rn10;
    int rn11;
    int rn12;
    int rn13;
    int rn14;
    int rn15;
    int rn16;
    int rn17;
    int rn18;
    int rn19;
    int rn20;
    double total;
    double average;
    Scanner keeper;
    Scanner keeper2;
    Random gen;
    gen = new Random();

    keeper = new Scanner(System.in);
    System.out.print("Please enter an integer for the MINIMUM value: ");
    minimum_integer = keeper.nextInt();

    keeper2 = new Scanner(System.in);
    System.out.print("Please enter an integer for the MAXIMUM value: ");
    maximum_integer = keeper2.nextInt();

    rn1 = gen.nextInt();
    rn1 = rn1 % minimum_integer + maximum_integer;
    rn2 = gen.nextInt();
    rn2 = rn2 % minimum_integer + maximum_integer;
    rn3 = gen.nextInt();
    rn3 = rn3 % minimum_integer + maximum_integer;
    rn4 = gen.nextInt();
    rn4 = rn4 % minimum_integer + maximum_integer;
    rn5 = gen.nextInt();
    rn5 = rn5 % minimum_integer + maximum_integer;
    rn6 = gen.nextInt();
    rn6 = rn6 % minimum_integer + maximum_integer;
    rn7 = gen.nextInt();
    rn7 = rn7 % minimum_integer + maximum_integer;
    rn8 = gen.nextInt();
    rn8 = rn8 % minimum_integer + maximum_integer;
    rn9 = gen.nextInt();
    rn9 = rn9 % minimum_integer + maximum_integer;
    rn10 = gen.nextInt();
    rn10 = rn10 % minimum_integer + maximum_integer;
    rn11 = gen.nextInt();
    rn11 = rn11 % minimum_integer + maximum_integer;
    rn12 = gen.nextInt();
    rn12 = rn12 % minimum_integer + maximum_integer;
    rn13 = gen.nextInt();
    rn13 = rn13 % minimum_integer + maximum_integer;
    rn14 = gen.nextInt();
    rn14 = rn14 % minimum_integer + maximum_integer;
    rn15 = gen.nextInt();
    rn15 = rn15 % minimum_integer + maximum_integer;
    rn16 = gen.nextInt();
    rn16 = rn16 % minimum_integer + maximum_integer;
    rn17 = gen.nextInt();
    rn17 = rn17 % minimum_integer + maximum_integer;
    rn18 = gen.nextInt();
    rn18 = rn18 % minimum_integer + maximum_integer;
    rn19 = gen.nextInt();
    rn19 = rn19 % minimum_integer + maximum_integer;
    rn20 = gen.nextInt();
    rn20 = rn20 % minimum_integer + maximum_integer;

    System.out.println("Your random numbers are: " + rn1 + ", " + rn2 + ", " + rn3 + ", " + rn4 + ", " + rn5 + ", " + rn6 + ", " + rn7 + ", " + rn8 + ", " + rn9 + ", " + rn10 + ", " + rn11 + ", " + rn12 + ", " + rn13 + ", " + rn14 + ", " + rn15 + ", " + rn16 + ", " + rn17 + ", " + rn18 + ", " + rn19 + ", " + rn20 + ".");

    total = rn1 + rn2 + rn3 + rn4 + rn5 + rn6 + rn7 + rn8 + rn9 + rn10 + rn11 + rn12 + rn13 + rn14 + rn15 + rn16 + rn17 + rn18 + rn19 + rn20;
    average = total / 20;

    System.out.print("The average is " + average);
    }
}

任何人都可以提供帮助并引导我朝着正确的方向前进吗?我确定有些事情我做的不对(或很多事情)我只是不知道那到底是什么。

这是我试图让它工作的较短版本......

import java.util.Random;
import java.util.Scanner;

public class p1
{
    public static void main(String[] args)
    {
    Random gen;
    gen = new Random();
    Scanner keeper;
    int range, user_min, user_max, num1, num2, num3, total;
    double average;

    keeper = new Scanner(System.in);
    System.out.print("Please enter a maximum integer value: ");
        user_max = keeper.nextInt();

        keeper = new Scanner(System.in);
    System.out.print("Please enter a minimum integer value: ");
    user_min = keeper.nextInt();

    range = user_max - user_min + 1;

    num1 = gen.nextInt() % range + user_min;
    num2 = gen.nextInt() % range + user_min;
    num3 = gen.nextInt() % range + user_min;

    System.out.print("The random numbers computed are " + num1 + ", " + num2 + ", " + num3 + ", ");
    }
}

为什么这不能处理负面因素?而且我的值仍然不在指定范围内?帮助?

4

4 回答 4

2

一些提示/提示:

  1. 对 20 个数字使用数组。然后当你发现一个错误时,你只需要纠正一次而不是 20 次。

  2. 随机数生成器有种子。确保每次提供不同的种子,否则随机数生成器将始终生成相同的随机数集。

  3. MIN 和 MAX 之间可以有多少个值?例如,3 到 7 之间可以有多少个不同的值?45和80?3和1200?找到模式。

  4. 了解为什么您的程序需要 '%' 运算符。通过执行“gen.nextInt() % NUM”可以实现什么?

于 2013-10-19T00:34:41.423 回答
1

随机列表示例:(
为了便于演示,使用 Python 而不是 Java。)

1   #!/usr/bin/env python
2   def spawn(user_min, user_max, rounds):
3     import random 
4     sum = 0
5     user_range = user_max - user_min + 1  
6     for i in range(rounds):
7       result = int((random.random() * user_range)) + user_min
8       sum = sum + result
9       print(str(i) + ': ' + str(result))
10     mean = sum / rounds
11    print('range: ' + str(user_range))
12    print('mean: ' + str(mean))

您感兴趣的线路是:

  • 第 5 行:这决定了随机数的分布。
  • 第 7 行:这会获取 range 的随机值[0.0, 1.0);乘以所需的用户范围;然后转换为整数以“砍掉”尾随小数点;然后将结果添加到范围的最小允许值。

例如:

range = (10) - (-5) + 1 = 16
a_result = (int)(0.14159265359 * 16) + -5 = 3 - 5 = -3
another_result = (int)(0.71828182845 * 16) + -5 = 11 - 5 = 6
于 2013-10-18T06:34:21.990 回答
0

线

rn1 = rn1 % minimum_integer + maximum_integer;

是错的。考虑例如生成 1 到 6 范围内的数字。然后你正在计算

rn1 = rn1 % 1 + 6;

现在,rn1 % 1总是等于 0,所以你总是得到 6。你应该使用不同的公式。

于 2013-10-18T02:31:57.513 回答
0

这就是我想出的..

import java.util.*;
class RandNegToPos {
    public static void main(String[] args) 
    {

            Scanner scan = new Scanner(System.in);
            int array[]=new int[20];
            System.out.println("What is the minimum? ");
            int min = scan.nextInt();

            System.out.println("What is the maximum? ");
            int max = scan.nextInt();

            if (max < min)
                System.out.println("Sorry the maximum is less than the miniumum.");

            else{

            int rnd = (int) (min + Math.random() * ((max) - min));   
            for (int i=0; i < 20 ; i++)
            {
                rnd = (int) (min + Math.random() * ((max) - min));
                array[i] = rnd;
            }
            int sum=0;
            for (int t : array)
                sum += t;
            System.out.println("Your randoms are:");
            System.out.println(Arrays.toString(array));
            System.out.println("The average is: " + (sum/20) );
            }
   }

}
于 2013-10-26T04:43:38.097 回答