【发布时间】:2018-08-06 14:07:13
【问题描述】:
好的,我是 Java 新手,这就是我需要做的。 编写一个 Java 程序,允许用户指定他/她希望在 1 到 100 之间生成多少个随机数。然后生成随机数并在输出中列出它们。您还应该计算最高、最低、总和和平均值。我需要帮助的部分是最低和最高。我不能使用数组,因为我们还没有真正接触过它们。 我需要帮助从我的 randomInt 中抛出 int 最低的东西。
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
int r;
int sum = 0;
int lowest=0;
int highest=0;
System.out.println("Please enter enter how many numbers "
+ "you would like to generate.");
int userInput = sc.nextInt();
for(r=1;r<=userInput;r++)
{ // was unsure how to remove the comma from the last number
int randomInt = (int)(rnd.nextFloat()*100)+1;
System.out.print(randomInt + ",");
sum += randomInt;
if(randomInt < lowest)
{
lowest = randomInt;
}
}
System.out.println(" The total sum of these random numbers are "
+sum);
int average = sum / userInput;
System.out.println("The average of all those numbers is "
+ average);
System.out.println(lowest);
}
【问题讨论】:
-
你需要问一个具体的问题。您刚刚发布了需求、代码,仅此而已。
-
抱歉我编辑了它。我需要帮助做的是从我的 randomInt 中将一些东西放入最低的 int 中。我不知道该怎么做。
-
您已经有一个赋值语句可以做到这一点。
if(randomInt < lowest) { lowest = randomInt; }你的实际问题是什么? -
(其实我可以看出问题出在哪里。但是如果您自己考虑一下,您会了解更多。)给您的问题:在确切什么条件下分配发生?
-
其实……没有。你错过了重点。关键是零总是小于或等于
randomint。看看你是如何声明和初始化lowest的。