[英]Using objects in a subclass java
我想使用我在子类的一个类中声明的对象,但它给了我无法从静态上下文中引用的非静态变量我是一个初学者,所以我可以改变什么来使这项工作
class PairofDice {
int d61;
int d62;
PairofDice d1 = new PairofDice();
PairofDice d2 = new PairofDice();
class BoxCars {
public static void main(String[] args) {
roll();
Random rand = new Random();
int BC = 0;
for (int i = 0; i < 1000; i++) {
d1.d61 = rand.nextInt(6 + 1 - 1) + 1;
d2.d62 = rand.nextInt(6 + 1 - 1) + 1;
if (d1.d61 + d2.d62 == 12) {
BC++;
}
}
}
}
}
(忽略滚动方法它是其他东西的一部分)
如下图移动你的骰子实例。 在同一个nextInt()
调用中减 1 nextInt()
1 没有意义。 由于您的代码中还有其他有问题的结构,更好的问题可能是寻求帮助以实现最终目标。
public class PairofDice {
int d61;
int d62;
class BoxCars {
public static void main(String[] args) {
// roll();
Random rand = new Random();
PairofDice d1 = new PairofDice();
PairofDice d2 = new PairofDice();
int BC = 0;
for (int i = 0; i < 1000; i++) {
d1.d61 = rand.nextInt(6 + 1 - 1) + 1;
d2.d62 = rand.nextInt(6 + 1 - 1) + 1;
if (d1.d61 + d2.d62 == 12) {
BC++;
}
}
}
}
}
您是否尝试过将内部类设为静态?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.