改变骰子的眼睛

学校给我的任务是制作一个骰子游戏,用户可以在其中改变骰子的眼睛,他们给我的唯一提示是使用 ASCII 表。


到目前为止,这是我的代码,我正在碰壁,因为我如何使数字成为用户的输入(我不是很有创意):


 System.out.println("Which character should be used as the eye of the dice:");

    char eyeDice = input.next().charAt(0);

    System.out.println(eyeDice);



    int Dice;

    Dice = (int)(Math.random()* 6 + 1);


    while (Dice < 6) {

        Dice = (int)(Math.random()* 6 + 1);

        System.out.println(Dice);


    }

代码的输出如下所示:


Which character should be used as the eye of the dice:

$

$

1

4

1

1

1

1

4

1

2

2

6


Process finished with exit code 0

这就是它最终的样子:


Which character should be used as the eye of the dice:


#

  #

    #


#   #

  #

#   #


#   #


#   #

#   #

#   #



Process finished with exit code 0

任何正确方向的提示或提示将不胜感激!


眼眸繁星
浏览 204回答 3
3回答

倚天杖

计算机不附带将数字“4”转换为 ascii 绘图的代码。你必须自己写这个。我建议把这些画在一张纸上。在您的 Java 代码中,您可以有一堆 if/elseif 语句,6 个面中的每一个都有一个。每个块将打印 3 行。首先锁定角色以用于眼睛,然后努力制作用户可以稍后配置的内容。以下是帮助您入门的部分内容:if (dieRoll == 5) {&nbsp; &nbsp; System.out.println("* *");&nbsp; &nbsp; System.out.println(" * ");&nbsp; &nbsp; System.out.println("* *");} else if (dieRoll == 6) {&nbsp; &nbsp; // you figure it out from here...

12345678_0001

在给定的例子中eyeDice是'#'。抛出的dice(请d在 java 中使用小)将是 3、5、4 和 6。因此,您需要一些方法,例如:void printDice(int dice, char eyDice) {&nbsp; &nbsp; ... System.out.println ...}和你的代码int dice = (int)(Math.random()* 6 + 1);while (dice < 6) {&nbsp; &nbsp; printDice(dice, eyeDice);&nbsp; &nbsp; dice = (int)(Math.random()* 6 + 1)}将打印 5(忽略眼睛):System.out.println("?&nbsp; &nbsp;?");System.out.println("&nbsp; ?&nbsp; ");System.out.println("?&nbsp; &nbsp;?");

慕少森

这不是一个完整的例子,但它是为了让你知道该怎么做public static void main(String[] args){&nbsp; &nbsp; System.out.println("Which character should be used as the eye of the dice:");&nbsp; &nbsp; char eyeDice = input.next().charAt(0);&nbsp; &nbsp; System.out.println(eyeDice);&nbsp; &nbsp; int Dice;&nbsp; &nbsp; Dice = (int)(Math.random()* 6 + 1);&nbsp; &nbsp; while (Dice < 6) {&nbsp; &nbsp; &nbsp; &nbsp; Dice = (int)(Math.random()* 6 + 1);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Dice);&nbsp; &nbsp; &nbsp; &nbsp; //If statement calling print&nbsp; &nbsp; }}private void printOne(char character){&nbsp; &nbsp; String dice = "\n&nbsp; #&nbsp; \n";&nbsp; &nbsp; System.out.println(dice.replace('#', character));}private void printTwo(char character){&nbsp; &nbsp; String dice = "#&nbsp; &nbsp;#\n\n#&nbsp; &nbsp;#";&nbsp; &nbsp; System.out.println(dice.replace('#', character));}private void printThree(char character){&nbsp; &nbsp; String dice = "#\n&nbsp; #\n&nbsp; &nbsp;#";&nbsp; &nbsp; System.out.println(dice.replace('#', character));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java