我想在Java中生成1到10之间的数字.
这是我尝试过的:
Random rn = new Random();
int answer = rn.nextInt(10) + 1;
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉()在调用nextInt方法时要在括号中放什么以及添加什么?
我应该创建一个类PrimeNumberGenerator,它有一个方法nextPrime可以打印出所有素数,直到用户输入的数字.
例)
Enter a Number:
20
2
3
5
7
11
13
17
19
Run Code Online (Sandbox Code Playgroud)
我们的老师告诉我们,我们应该使用嵌套for循环.我试过了,但是当我试图制作内部(嵌套)循环时,我真的很困惑.
这是我的代码:(我稍后会做一个测试课)
public class PrimeGenerator {
private int num;
boolean isPrime;
public PrimeGenerator(int n)
{
num = n;
}
public int nextPrime (int num)
{
for (int i=2; i < num; i++) // The first prime number is 2 and the prime numbers only have to go up to a number the user inputs.
{
for (int j = 3; …Run Code Online (Sandbox Code Playgroud)