我想创建一个骰子程序,我需要的输出是'一个','两','三'等字符串。它当前正在打印输出0,但这是因为我的OutputDice方法是不正确的。当我拿出它时,它将整个参数作为整数传递,但我需要它们作为字符串。我怎么做?如何将整数方法转换为Java中的字符串方法?
我的代码如下:
import java.util.Random;
public class Dice {
private int Value;
public void setValue(int diceValue)
{
Value = diceValue;
}
public int getValue()
{
return Value;
}
public void roll()
{
Random rand = new Random();
Value = rand.nextInt(6) + 1;
}
public void OutputDice()
{
switch (Value)
{
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
case 4:
System.out.println("Four");
case 5:
System.out.println("Five");
case 6:
System.out.println("Six");
}
}
}
和
public class DiceRoll {
public static void main(String[]args) {
Dice firstDie = new Dice();
Dice secondDie = new Dice();
firstDie.OutputDice();
secondDie.OutputDice();
System.out.println("Dice 1: "+ firstDie.getValue());
System.out.println("Dice 2: "+ secondDie.getValue());
}
}
你忘记打电话'roll' –
如果你不滚你的骰子,我不认为它会得到你的任何号码。 :) – Blank