作为一个项目,我必须为我的 CS 入门课程编写一个程序。程序必须
- 向用户询问最小值(可以是负数)
- 向用户询问最大值(也可能是负数,但必须大于最小值)
- 产生 20 个介于用户给出的最小值/最大值之间的随机数,并且
- 产生产生的二十个随机数的平均值。
我以为我已经完成了程序,但是当我执行程序时,数字从来都不是随机的,所有二十个数字都与我输入 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 + ", ");
}
}
为什么这不能处理负面因素?而且我的值仍然不在指定范围内?帮助?