gpt4 book ai didi

java - 编写一个带有随机数的程序

转载 作者:行者123 更新时间:2023-12-01 23:30:17 25 4
gpt4 key购买 nike

我必须为我的 CS 入门类(class)编写一个程序作为一个项目。该程序必须

  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 + ", ");
}
}

为什么这不能处理负数?我的值仍然不在指定范围内?帮忙吗?

最佳答案

一些技巧/提示:

  1. 使用一个数组来存储 20 个数字。这样,当你发现错误时,你只需纠正一次,而不是 20 次。

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

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

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

关于java - 编写一个带有随机数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19448379/

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