如何将随机数生成器中的数字相加
How to add together numbers from a random number generator
我正在为学校制作一个随机数生成器,我必须将总数加在一起,但问题是我使用了一个循环,所以我实际上并没有将随机生成的数字存储在任何地方,所以我不是非常确定如何创建一个总。就像随机数生成器输出 3 和 3 一样,总数会告诉你它是 6。这是我的代码:
import java.util.Scanner;
import java.util.Random;
public class Assignment7 {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rand = new Random();
int randomNumber = 0;
System.out.println("Welcome to randomdiceroll.com or something!"
+ "\nPlease enter how many dice you'd like to roll.");
int amount = key.nextInt();
System.out.println("Please enter the range for the di(c)e from 1-?");
int limit = key.nextInt();
for(int i = 0; i < amount; i++) {
randomNumber = rand.nextInt(limit) +1;
System.out.println(randomNumber);
}
int total; //The part I cant figure out
System.out.println("\nYour total is..." + total + ".");
key.close();
}
}
请原谅糟糕的格式,我试过了:
randomNumber * amount
但这显然行不通。我正在使用 Java 如果我解释这个很奇怪,我可以肯定地澄清!我确定答案非常简单,但我是初学者,遗憾的是不知道。
提前致谢!
在for循环之前定义总和,每次把随机数加到总和上。
我会这样做:
import java.util.Scanner;
import java.util.Random;
public class Assignment7 {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rand = new Random();
System.out.println("Welcome to randomdiceroll.com or something!"
+ "\nPlease enter how many dice you'd like to roll.");
int amount = key.nextInt();
System.out.println("Please enter the range for the di(c)e from 1-?");
int limit = key.nextInt();
int total = 0;
for(int i = 0; i < amount; i++) {
randomNumber = rand.nextInt(limit) +1;
total += randomNumber;
System.out.println(randomNumber);
}
System.out.println("\nYour total is..." + total + ".");
key.close()
}
}
您可以在循环中添加随机数。
int total=0;
for(int i=0; i<amount; i++){
int randomNumber = rand.nextInt(limit) +1;
total += randomNumber;
System.out.println(randomNumber);
}
我正在为学校制作一个随机数生成器,我必须将总数加在一起,但问题是我使用了一个循环,所以我实际上并没有将随机生成的数字存储在任何地方,所以我不是非常确定如何创建一个总。就像随机数生成器输出 3 和 3 一样,总数会告诉你它是 6。这是我的代码:
import java.util.Scanner;
import java.util.Random;
public class Assignment7 {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rand = new Random();
int randomNumber = 0;
System.out.println("Welcome to randomdiceroll.com or something!"
+ "\nPlease enter how many dice you'd like to roll.");
int amount = key.nextInt();
System.out.println("Please enter the range for the di(c)e from 1-?");
int limit = key.nextInt();
for(int i = 0; i < amount; i++) {
randomNumber = rand.nextInt(limit) +1;
System.out.println(randomNumber);
}
int total; //The part I cant figure out
System.out.println("\nYour total is..." + total + ".");
key.close();
}
}
请原谅糟糕的格式,我试过了:
randomNumber * amount
但这显然行不通。我正在使用 Java 如果我解释这个很奇怪,我可以肯定地澄清!我确定答案非常简单,但我是初学者,遗憾的是不知道。
提前致谢!
在for循环之前定义总和,每次把随机数加到总和上。
我会这样做:
import java.util.Scanner;
import java.util.Random;
public class Assignment7 {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rand = new Random();
System.out.println("Welcome to randomdiceroll.com or something!"
+ "\nPlease enter how many dice you'd like to roll.");
int amount = key.nextInt();
System.out.println("Please enter the range for the di(c)e from 1-?");
int limit = key.nextInt();
int total = 0;
for(int i = 0; i < amount; i++) {
randomNumber = rand.nextInt(limit) +1;
total += randomNumber;
System.out.println(randomNumber);
}
System.out.println("\nYour total is..." + total + ".");
key.close()
}
}
您可以在循环中添加随机数。
int total=0;
for(int i=0; i<amount; i++){
int randomNumber = rand.nextInt(limit) +1;
total += randomNumber;
System.out.println(randomNumber);
}