如何在Java中使用while循环生成随机数?

huangapple go评论73阅读模式
英文:

how to generate random numbers using a while loop in java?

问题

我需要生成从1到20的随机数,并计算它们的总和,直到总和超过100。

Random rand = new Random();
int num = rand.nextInt(20) + 1;
System.out.println("随机数 " + num);
int sum = num;
System.out.println("当前总和 " + sum);
num = rand.nextInt(20) + 1;
sum = num + sum;
System.out.println("随机数 " + num);
System.out.println("当前总和 " + sum);
while (sum > 100) 
{
    System.out.println("总和 " + sum + " 超过100");
}

这是我目前的进展。

英文:

i need to generate random numbers from 1 to 20 and count their sum until it reaches over 100

Random rand = new Random();
 int num = rand.nextInt(20)+1;
 System.out.println("Random number " + num);
 int sum = num;
 System.out.println("Current sum " + sum);
 num = rand.nextInt(20)+1;
 sum = num + num;
 System.out.println("Random number " + num); 
 System.out.println("Current sum " + sum);
 while (sum > 100)
 { 
     System.out.println("The sum " + sum + " exceeds 100"); 
 }

this is what i have so far

答案1

得分: 1

这里有一种相当有趣的方法来实现它:

  • 生成一个在1到20之间(包括1和20)的ints流。
  • 转换为迭代器。
  • 继续求和,直到和大于100。
Random rand = new Random();
int sum = 0;
for (Iterator<Integer> it = rand.ints(1, 21).iterator();
        sum < 100 && it.hasNext();) {
    sum += it.next();
}

甚至更好,因为它避免了拆箱。

int sum = 0;
while ((sum += rand.nextInt(20)+1) < 100);
System.out.println(sum);
英文:

Here is a rather interesting way to do it.

  • generate a stream of ints between 1 and 20 inclusive.
  • turn into an iterator.
  • continue summing until the sum > 100.
Random rand = new Random();
int sum = 0;
for (Iterator&lt;Integer&gt; it = rand.ints(1, 21).iterator();
		sum &lt; 100 &amp;&amp; it.hasNext();) {
	sum += it.next();
}

Or even better as it avoids unboxing.

int sum = 0;
while ((sum += rand.nextInt(20)+1) &lt; 100);
System.out.println(sum);

</details>



# 答案2
**得分**: 0

你需要将生成和求和的部分放在循环内。不能使用同一个变量(`num`)保存两个值。你之前是让总和等于两个 num,而不是实际上跟踪 num 的总和。

终止语句需要放在循环外部,并且在循环外部初始化你的总和。

```java
Random rand = new Random();
int sum = 0; // 初始化你的总和

while (sum < 100) // 这个等式写反了
{ 
    int num = rand.nextInt(20) + 1;
    System.out.println("随机数 " + num);

    sum += num; // 将随机数加到总和上
                // 不要只是让总和等于两个随机数。
    System.out.println("当前总和 " + sum);
}
System.out.println("总和 " + sum + " 超过了 100");
英文:

You need to put the generation and sum inside the loop. You can't use the same variable (num) to hold two values. You were making the sum equal to two nums, not actually tracking the sum of the nums.

You need to put the termination statement outside the loop. and initialise your sum outside the loop.

Random rand = new Random();
int sum = 0; //initialise your sum

while (sum &lt; 100) // this equality was the wrong way around
 { 
    int num = rand.nextInt(20)+1;
    System.out.println(&quot;Random number &quot; + num);

    sum += num; //add your random num to the sum
                //  don&#39;t just make it equal to two nums.
    System.out.println(&quot;Current sum &quot; + sum);
 }
 System.out.println(&quot;The sum &quot; + sum + &quot; exceeds 100&quot;); 

huangapple
  • 本文由 发表于 2020年9月30日 09:24:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64129677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定