对于我的任务,我需要写下以下属性:
编写一个类Deck。Deck具有以下私有属性:
静态随机numberGenerator。
使用对用种子123创建的随机对象的引用来“就地”初始化随机numberGenerator (即,在与其声明相同的行上)。
我对如何继续下去感到困惑。我已经尝试初始化属性,但我认为我做的不是正确的事情。
下面是我的代码:
import java.util.Random;
public class Deck {
// Declare the private attributes
private static double getRandomNumber(int seed) {
Random number = new Random(seed);
}发布于 2019-03-28 21:16:46
属性属于类,而不是方法,因此您应该在任何方法外部声明它。
import java.util.Random;
public class Deck {
// Declare the private attributes
private static Random numberGenerator = new Random(123);
// other attributes and methods follow
}发布于 2019-03-28 21:30:37
首先,getRandomNumber方法中没有返回语句。需要为Class声明属性,而不是在方法中声明属性。我想这可能就是你要找的。
import java.util.Random;
public class Deck {
private static Random number = new Random(123); // declare and initialize a Ranom object
public static double getRandomNumber(){
return number.nextDouble(); // return the next value
}
}https://stackoverflow.com/questions/55398534
复制相似问题