- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试掷骰子,然后当我在 Player 类中调用 slaTarningar() 时系统打印 1 个骰子,
class Player{
int armees = 0;
int diceAmount = 0;
Dice Dices[];
Player(String playerType){
armees = 10;
diceAmount = ("A".equals(playerType)) ? 3 : 2;
Dices= new Dice[diceAmount];
for(int i=0;i<Dices.length;i++){
Dices[i]=new Dice();
}
}
void slaTarningar(){
for(int i=0;i<Dices.length;i++){
Dices[i].role();
}
System.out.println ("Dice: "+ Dices[1]);
}
void visaTarningar(){
String allDices="";
for(int i=0;i<Dices.length;i++){
allDices += ", " + Dices[i];
}
}
}
class Dice{
int value;
Dice(){
value=0;
}
void role(){
int role;
role = (int)(Math.random()*6+1);
value=role;
}
}
我得到的只是我的项目名称,还有一些奇怪的东西:
Dice: javaapplication9.Dice@9304b1
这里出了什么问题?
最佳答案
您需要向 Dice
添加一个 toString
方法:
class Dice{
int value;
Dice(){
value=0;
}
void role(){
int role;
role = (int)(Math.random()*6+1);
value=role;
}
public String toString() {
return "" + value + "";
}
}
或者添加一个getValue
方法:
class Dice{
int value;
Dice(){
value=0;
}
void role(){
int role;
role = (int)(Math.random()*6+1);
value=role;
}
public int getValue() {
return value;
}
}
//.. in other class:
System.out.println ("Dice: "+ Dices[1].getValue());
关于Java:掷骰子和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6127237/
我是 Java 新手,我正在尝试创建一个数组列表。 我制作了一个小程序,要求用户提供要掷的骰子数量: System.out.println("How many dices do you wan
我目前需要一些关于“简单”问题的建议。我正在构建一个游戏助手,用户可以在其中掷骰子(6 面骰子、20 面骰子等)。实际上,我只向用户显示号码。但是,为了更好用,我想在屏幕上显示骰子(例如,在带有自定义
我刚刚开始学习 Java 编程,并编写了一个程序来掷 x 面骰子 x 次。边数和卷数由用户输入定义。该程序以 JTable 格式给出每个数字的绝对频率和相对频率。一切都很顺利,直到您为侧面和卷数选择较
这段代码的用途:模拟100场CRAPS,记录第一轮输,第一轮赢,第二轮负加分,第二轮赢加分的#。 那些不熟悉掷骰子规则的人;您基本上掷两个骰子,如果结果不是 2、3 或 12 的总数,您可以再次掷骰(
所以我为龙与地下城创建了一个基本的掷骰子 dicord 机器人。 我到目前为止的代码可以掷任何类型的骰子,(例如“roll xdy”“roll 1d20”,“roll 100d100”) 当有人发送匹
我有一些关于java的问题。代码中有两个问题(我将它们作为注释留下)。另外使用设置和获取方法的目的是什么?您能简单地解释一下吗?我是初学者。谢谢:) public class Die { pri
尝试绘制 2 个骰子总和的 pmf,但出现一些右尾问题。 我尝试过使用 numpy 和其他 python 库,但问题仍然存在: import tensorflow as tf tf.enable_ea
当掷 2 个六面骰子时,最常见的结果应该是 7,而 2 和 12 是最不常见的结果。 当我执行下面的代码时,数字 12 的出现频率很高,这是错误的。 #include #include #incl
我正在尝试学习 Python 库 itertools,我认为一个好的测试是模拟掷骰子。使用 product 并使用 collections 库计算可能的方法数,很容易生成所有可能的滚动。我正在尝试解决
所以我做了这个掷骰子 100 次的方法,有 50% 的机会掷出 6。基本思想是 1 到 6 之间有 50% 的奇数和 50% 的偶数,所以如果掷出偶数,系统打印 6,否则打印 1 到 5 之间的随机数
我是 C++ 的初学者,这是家庭作业,但我卡住了。我还有一个问题,然后我就完成了。我想不出一种算法可以判断用户输入的是小直线 (1234) 还是 (2345) 还是 (3456)。我知道如何使用循环来
我是一名优秀的程序员,十分优秀!