Java 在几个不同的参数内生成随机数
我每次运行这个程序都会遇到一个逻辑错误,我觉得下面的代码应该完全符合问题的要求,但是输出肯定是错误的。我看不出我做错了什么 编写一个Java程序(命名为RandomNumbers),生成如下随机数。确保正确标记以下每个零件的输出,并在单独的行上打印输出。与上一个程序一样,使用制表符转义字符将输出排列在标签之后。a) 介于30和50(含)之间的随机整数。b) 介于20和-20(含20)之间的随机整数。c) 介于-20和-60(包括-20和-60)之间的随机整数。d) 介于0.0和15.9999(含)之间的随机浮点数 这就是我到目前为止所做的:Java 在几个不同的参数内生成随机数,java,Java,我每次运行这个程序都会遇到一个逻辑错误,我觉得下面的代码应该完全符合问题的要求,但是输出肯定是错误的。我看不出我做错了什么 编写一个Java程序(命名为RandomNumbers),生成如下随机数。确保正确标记以下每个零件的输出,并在单独的行上打印输出。与上一个程序一样,使用制表符转义字符将输出排列在标签之后。a) 介于30和50(含)之间的随机整数。b) 介于20和-20(含20)之间的随机整数。c) 介于-20和-60(包括-20和-60)之间的随机整数。d) 介于0.0和15.9999(含
public class RandomNumbers{
public static void main(String[] args) {
// Part A) Generate a random integer number between 30 and 50 (inclusive)
System.out.println("a) Random integer between 30 and 50 (inclusive): " +
30 + (int)(Math.random() * ((50 - 30) + 1)));
// Part B) Generate a random integer number between 20 and -20 (inclusive)
System.out.println("a) Random integer between 30 and 50 (inclusive): " +
(-20) + (int)(Math.random() * ((20 - (-20)) + 1)));
// Part C) Generate a random integer number between -20 and -60 (inclusive)
System.out.println("a) Random integer between 30 and 50 (inclusive): " +
(-60) + (int)(Math.random() * (((-20) - (-60)) + 1)));
// Part d) Generate a random floating-point number between 0.0 and 15.9999 (inclusive)
System.out.println("a) Random integer between 30 and 50 (inclusive): " +
0.0 + (double)(Math.random() * ((15.9999 - 0.0) + 1)));
}}
我的输出是:a)30到50之间的随机整数(含):3018
a) 30到50之间的随机整数(含):-207
a) 30到50之间的随机整数(含):-6014
a) 30到50之间的随机整数(含):0.03.7171159220482286
正如你所看到的,这是不对的。你的问题是你没有把你的计算放在括号里。 java将在表达式中考虑“<代码> +//>代码>符号作为字符串级联。 例如:
System.out.println("Test"+1+2);
打印Test12
,但
System.out.println("Test"+(1+2));
打印Test3
因此,在第一个示例中,您将获得3018
作为输出,因为它不是将30+18相加,而是将两个数字都添加到字符串中
因此,将为您提供所需输出的正确代码
System.out.println("a) Random integer between 30 and 50 (inclusive): " +
(30 + (int)(Math.random() * ((50 - 30) + 1))));
你的问题是你没有把你的计算放在括号里。 java将在表达式中考虑“<代码> +//>代码>符号作为字符串级联。 例如:
System.out.println("Test"+1+2);
打印Test12
,但
System.out.println("Test"+(1+2));
打印Test3
因此,在第一个示例中,您将获得3018
作为输出,因为它不是将30+18相加,而是将两个数字都添加到字符串中
因此,将为您提供所需输出的正确代码
System.out.println("a) Random integer between 30 and 50 (inclusive): " +
(30 + (int)(Math.random() * ((50 - 30) + 1))));
您需要在添加表达式中添加括号,否则它将是concatenad而不是added。(示例:text+30+1=>text301) 在最后一种情况下,不需要添加1
public static void main(String[] args) {
// Part A) Generate a random integer number between 30 and 50 (inclusive)
System.out.println("a) Random integer between 30 and 50 (inclusive): " +
(30 + (int)(Math.random() * ((50 - 30) + 1))) );
// Part B) Generate a random integer number between 20 and -20 (inclusive)
System.out.println("a) Random integer between -20 and 20 (inclusive): " +
((-20) + (int)(Math.random() * ((20 - (-20)) + 1))) );
// Part C) Generate a random integer number between -20 and -60 (inclusive)
System.out.println("a) Random integer between -20 and -60 (inclusive): " +
((-60) + (int)(Math.random() * (((-20) - (-60)) + 1))) );
// Part d) Generate a random floating-point number between 0.0 and 15.9999 (inclusive)
System.out.println("a) Random integer between 0.0 and 15.9999 (inclusive): " +
(0.0 + (double)(Math.random() * (16)) );
}
您需要在添加表达式中添加括号,否则它将是concatenad而不是added。(示例:text+30+1=>text301) 在最后一种情况下,不需要添加1
public static void main(String[] args) {
// Part A) Generate a random integer number between 30 and 50 (inclusive)
System.out.println("a) Random integer between 30 and 50 (inclusive): " +
(30 + (int)(Math.random() * ((50 - 30) + 1))) );
// Part B) Generate a random integer number between 20 and -20 (inclusive)
System.out.println("a) Random integer between -20 and 20 (inclusive): " +
((-20) + (int)(Math.random() * ((20 - (-20)) + 1))) );
// Part C) Generate a random integer number between -20 and -60 (inclusive)
System.out.println("a) Random integer between -20 and -60 (inclusive): " +
((-60) + (int)(Math.random() * (((-20) - (-60)) + 1))) );
// Part d) Generate a random floating-point number between 0.0 and 15.9999 (inclusive)
System.out.println("a) Random integer between 0.0 and 15.9999 (inclusive): " +
(0.0 + (double)(Math.random() * (16)) );
}
我有错误
-最好让我们看看你尝试了什么以及你遇到了什么错误。你可以复制粘贴你的代码并替换参数。还有另一个大学作业…@定期计划的编程我不确定这是否真的是重复的。标题表明是这样,但OP实际上要求的是完全不同的东西(如何对不同的参数集执行)。告诉你的教师选项卡不是“转义字符”制表符和转义符都是控制字符。我有错误
-最好让我们看看你尝试了什么以及你遇到了什么错误。你可以复制和粘贴你的代码并替换参数。还有另一个大学作业…@RegularyScheduledProgramming我不确定这是否真的是重复的。标题表明是这样,但OP实际上要求的是完全不同的东西(如何对不同的参数集执行)。告诉你的教师tab不是“转义字符”。tab和escape都是控制字符。你是对的,在计算开始时忘记了括号。非常感谢。您是对的,在计算开始时忘记了括号。非常感谢。