英文:
My java method is returning 0.0 when it is supposed to output a random number, why isn't it working?
问题
以下是翻译好的部分:
这是一个面向编程课程的介绍。我应该编写一个程序,在海龟图形显示器上随机位置绘制各种对象,但为了确保随机位置在画布上,我应该使用方法 public int generateCoordinate(int limit)
。
限制值应该是 35,它应该是一个修饰随机生成的数字的值,以确保没有东西会显示在屏幕外。
然而,当我尝试应用这个方法时,我的代码只返回 0.0。
以下是我将限制设置为 5 的完整代码:
public class debug
{
private int x;
private int coord;
/**
* 类 debug 的对象构造函数
*/
public debug()
{
generateCoordinate(5);
System.out.println(coord);
}
public int generateCoordinate(int limit){
double coord = (Math.random()*301-159);
if (coord<(-150+limit))
return (int)coord + limit;
if (coord>(150-limit))
return (int)coord - limit;
return (int)coord;
}
}
当我让程序打印结果时,它输出为 0.0,我不知道如何修复它。
英文:
This is for an intro to programming course. I'm supposed to make a program that will draw a variety of objects on a turtle graphics displayer at a number of random places, however in order to ensure that the random places are on the board, I am supposed to use the method public int generateCoordinate(int limit)
The limit value is supposed to be 35, and it is supposed to be a modifier to the randomly generated number to ensure that nothing is off-screen.
When I attempt to apply this, however, my code returns nothing but 0.0.
Here's my full code with the limit set to 5:
public class debug
{
private int x;
private int coord;
/**
* Constructor for objects of class debug
*/
public debug()
{
generateCoordinate(5);
System.out.println(coord);
}
public int generateCoordinate(int limit){
double coord = (Math.random()*301-159);
if (coord<(-150+limit))
return (int)coord + limit;
if (coord>(150-limit))
return (int)coord - limit;
return (int)coord;
}
}
When I have the program print the result, it comes out as 0.0, and I can't figure out how to fix it.
答案1
得分: 1
问题似乎是coord
的变量范围问题。
在类级别有一个coord
,在方法内部有一个遮盖的coord
变量。
generateCoordinate
的返回值与方法范围的变量相关。
而debug
方法将从类范围读取值,该值从未被写入。
public class debug
{
private int x;
private int coord;
public debug()
{
generateCoordinate(5);
System.out.println(coord); // 类范围
// 试试这样
coord = generateCoordinate(5); // 使用方法返回值写入类范围
System.out.println(coord); // 类范围
}
public int generateCoordinate(int limit){
double coord = (Math.random()*301-159); // 方法范围
if (coord < (-150+limit))
return (int)coord + limit;
if (coord > (150-limit))
return (int)coord - limit;
return (int)coord; // 仍然是方法范围
}
}
英文:
The issue seems to be variable scope of coord
You have 1 coord
at Class level, and 1 overshadowing coord
variable inside your method.
The return value of generateCoordinate
is linked to the method scoped variable.
While the debug
method will read the value from the Class scope, which is never written.
public class debug
{
private int x;
private int coord;
public debug()
{
generateCoordinate(5);
System.out.println(coord); // class scope
// Try it like this instead
coord = generateCoordinate(5); // write the class scope with the method return value
System.out.println(coord); // class scope
}
public int generateCoordinate(int limit){
double coord = (Math.random()*301-159); // Method scope
if (coord<(-150+limit))
return (int)coord + limit;
if (coord>(150-limit))
return (int)coord - limit;
return (int)coord; // still method scope
}
}
答案2
得分: 0
由于您正在打印_coord_,您需要为它分配一个值。
当未分配时,默认的_int_值为_0_。
```java
public debug()
{
coord = generateCoordinate(5);
System.out.println(coord);
}
此外,Math#random 方法将返回0到1之间的_double_值。
您可以使用以下算法获取一个范围。
其中,35_是您的_限制,1_是您的_下限。
(Math.random() * 35 - 1) + 1
我建议使用_ Random_ 类。
它是与_ Math#random_ 在_ 这里_ 使用的相同对象。
_ Random#nextDouble_ 方法允许您设置_原点_ 和_上限_。
请注意,上限 是排他的,所以如果_限制_ 是_35_,您需要使用_36_。
public int generateCoordinate(int limit){
Random random = new Random();
double coord = random.nextDouble(1, limit + 1);
if (coord<(-150+limit))
return (int)coord + limit;
if (coord>(150-limit))
return (int)coord - limit;
return (int)coord;
}
输出
21
9
15
3
15
<details>
<summary>英文:</summary>
Since you are printing _coord_, you'll need to assign it a value.
The default _int_ value, when unassigned, is _0_.
```java
public debug()
{
coord = generateCoordinate(5);
System.out.println(coord);
}
Furthermore, the Math#random method is going to return a double value between 0 and 1.
You can use the following algorithm to get a range.
Where, 35 is your limit, and 1 is your lower bound.
(Math.random() * 35 - 1) + 1
I recommend using the Random class.
It's the same object that Math#random is utilizing here.
The Random#nextDouble method allows you to set an origin, and upper bound.
Note, the upper bound is exclusive, so you'll need to use 36, if your limit is 35.
public int generateCoordinate(int limit){
Random random = new Random();
double coord = random.nextDouble(1, limit + 1);
if (coord<(-150+limit))
return (int)coord + limit;
if (coord>(150-limit))
return (int)coord - limit;
return (int)coord;
}
Output
21
9
15
3
15
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论