英文:
How can I make it so the random number is changed every time it prints?
问题
/* Lesson 8 Coding Activity Question 1 */
import java.util.Scanner;
import java.util.Random;
public class U2_L8_Activity_One {
public static void main(String[] args) {
/* Write your code here */
Scanner scan = new Scanner(System.in);
System.out.println("Enter a positive integer.");
int n = scan.nextInt();
Random rand = new Random(); // Create a Random object
System.out.println("Printing 10 random integers from 1 to n...");
for (int i = 0; i < 10; i++) {
int randomInt = rand.nextInt(n) + 1; // Generate a random integer between 1 and n
System.out.println(randomInt);
}
}
}
英文:
> Write the code to ask the user for a positive integer n, then print 10 random integers from 1 to n inclusive using Math.random()
.
How do I make it so that the output is different every time? As of now every output following the first one is exactly the same.
/* Lesson 8 Coding Activity Question 1 */
import java.util.Scanner;
import edhesive.testing.Math;
public class U2_L8_Activity_One{
public static void main(String[] args){
/* Write your code here */
Scanner scan = new Scanner(System.in);
System.out.println("Enter a positive integer.");
int n = scan.nextInt();
double ran = Math.random();
int range = n;
int min = 1;
int answer = (int) (ran * range) + min;
System.out.println("Printing 10 random integers from 1 to n...");
System.out.println(answer);
System.out.println(answer);
System.out.println(answer);
System.out.println(answer);
System.out.println(answer);
System.out.println(answer);
System.out.println(answer);
System.out.println(answer);
System.out.println(answer);
System.out.println(answer);
}
}
答案1
得分: 1
你已经正确地生成了一个随机数,但随后只是将其打印了十次。如果你想要十个不同的数,你需要每次重新生成一个新的随机数。例如:
for (int i = 0; i < 10; ++i) {
double ran = Math.random();
int answer = (int) (ran * range) + min;
System.out.println(answer);
}
英文:
You properly generate a random number, but then just print it out ten times. If you want ten different numbers, you need to regenerate the number each time. E.g.:
for (int i = 0; i < 10; ++i) {
double ran = Math.random();
int answer = (int) (ran * range) + min;
System.out.println(answer);
}
答案2
得分: 1
你需要每次想要随机数时都调用Math.random()方法。你目前只调用了一次,并将结果赋值给了一个变量。调用System.out.println
来打印这个变量不会改变结果,因为你打印的变量没有被重新赋值。
使用以下代码:
System.out.println("随机数为:" + Math.random());
英文:
You need to call the Math.random() method every time you want a random number. You are calling it once and assigning the result to a variable. Calling a System.out.println
to print the variable will not change the result, because there is no reassignment of the variable you're printing.
Go with
System.out.println("The random number is " + Math.random());
答案3
得分: 0
这应该可以工作:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("输入一个正整数。");
int max = scan.nextInt();
int min = 1;
for (int i = 0; i < 10; ++i) {
int ran = (int) (Math.random() * (max - min)) + min;
System.out.println(ran);
}
}
英文:
This should work:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a positive integer.");
int max = scan.nextInt();
int min = 1;
for (int i = 0; i < 10; ++i) {
int ran = (int) (Math.random() * (max - min)) + min;
System.out.println(ran);
}
}
答案4
得分: -1
import java.util.Scanner;
import edhesive.testing.Math;
public class U2_L8_Activity_One {
public static void main(String[] args) {
System.out.println("请输入一个整数");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
double ran = Math.random();
int min = 1;
int answer = (int) (Math.random() * n) + min;
int answer1 = (int) (Math.random() * n) + min;
int answer2 = (int) (Math.random() * n) + min;
int answer3 = (int) (Math.random() * n) + min;
int answer4 = (int) (Math.random() * n) + min;
int answer5 = (int) (Math.random() * n) + min;
int answer6 = (int) (Math.random() * n) + min;
int answer7 = (int) (Math.random() * n) + min;
int answer8 = (int) (Math.random() * n) + min;
int answer9 = (int) (Math.random() * n) + min;
System.out.println(answer);
System.out.println(answer1);
System.out.println(answer2);
System.out.println(answer3);
System.out.println(answer4);
System.out.println(answer5);
System.out.println(answer6);
System.out.println(answer7);
System.out.println(answer8);
System.out.println(answer9);
}
}
英文:
This Will work when exciuted, and its done in a simple manner such as what you provided
import java.util.Scanner;
import edhesive.testing.Math;
public class U2_L8_Activity_One{
public static void main(String[] args){
System.out.println("Please enter a integer");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
double ran = Math.random();
int min = 1;
int answer = (int) (Math.random() * n) + min;
int answer1 = (int) (Math.random() * n) + min;
int answer2 = (int) (Math.random() * n) + min;
int answer3 = (int) (Math.random() * n) + min;
int answer4 = (int) (Math.random() * n) + min;
int answer5 = (int) (Math.random() * n) + min;
int answer6 = (int) (Math.random() * n) + min;
int answer7 = (int) (Math.random() * n) + min;
int answer8 = (int) (Math.random() * n) + min;
int answer9 = (int) (Math.random() * n) + min;
System.out.println(answer);
System.out.println(answer1);
System.out.println(answer2);
System.out.println(answer3);
System.out.println(answer4);
System.out.println(answer5);
System.out.println(answer6);
System.out.println(answer7);
System.out.println(answer8);
System.out.println(answer9);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论