如何在Java中将整数方法转换为字符串方法?

时间:2017-03-02 02:56:30

标签: java methods

我正在尝试创建一个骰子程序,我需要输出为'One','Two','Three'等字符串。它当前打印的输出为0,但这是因为我的OutputDice方法不正确。当我把它拿出来时,它将参数作为整数传递,但我需要它们作为字符串。我该怎么做?

我的代码如下:

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());
    }
}

2 个答案:

答案 0 :(得分:3)

您也永远不会为骰子分配值。您需要在显示值之前调用roll()方法。此外,使用switch case语句,您需要在案例之后包含中断

public void OutputDice()
{
    switch (Value)
    {
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
        break;
    case 3:
        System.out.println("Three");
        break;
    case 4:
        System.out.println("Four");
        break;
    case 5:
        System.out.println("Five");
        break;
    case 6:
        System.out.println("Six");
        break;
    }   
}

答案 1 :(得分:0)

感谢您的帮助。这是一个简单的疏忽。以下是我得到的答案:

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");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            case 4:
                System.out.println("Four");
                break;
            case 5:
                System.out.println("Five");
                break;
            case 6:
                System.out.println("Six");
                break;
            }   
        }
    }

public class BrandonAssignment4 {

    public static void main(String[]args) {

        Dice firstDie = new Dice();
        Dice secondDie = new Dice();

        firstDie.roll();
        secondDie.roll();

        firstDie.OutputDice();
        secondDie.OutputDice();
    }
}