改变骰子的眼睛
Change the eye of a dice
学校给我的作业是制作一个用户可以改变骰子眼睛的骰子游戏,他们给我的唯一提示是使用 ASCII table..
到目前为止,这是我的代码,我遇到了困难,因为我无法让数字成为用户的输入(我不是很有创意):
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
任何正确方向的提示或暗示将不胜感激!
计算机未附带将数字“4”转换为 ascii 图形的代码。
你必须自己写这个。我建议把这些画在一张纸上。在您的 java 代码中,您可以有一堆 if/elseif 语句,每个语句对应 6 个面孔。每个块将打印 3 行。首先锁定用于眼睛的角色,然后努力使用户可以稍后配置。
以下是帮助您入门的部分内容:
if (dieRoll == 5) {
System.out.println("* *");
System.out.println(" * ");
System.out.println("* *");
} else if (dieRoll == 6) {
// you figure it out from here...
在给定的示例中,eyeDice
是 '#'
。抛出的 dice
(请在 java 中加上一个小的 d
)将是 3、5、4 和 6。
因此你需要一些方法,比如:
void printDice(int dice, char eyDice) {
... System.out.println ...
}
和您的代码
int dice = (int)(Math.random()* 6 + 1);
while (dice < 6) {
printDice(dice, eyeDice);
dice = (int)(Math.random()* 6 + 1)
}
将打印 5(忽略眼睛):
System.out.println("? ?");
System.out.println(" ? ");
System.out.println("? ?");
这不是一个完整的示例,但它可以让您了解该怎么做
public static void main(String[] args){
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);
//If statement calling print
}
}
private void printOne(char character){
String dice = "\n # \n";
System.out.println(dice.replace('#', character));
}
private void printTwo(char character){
String dice = "# #\n\n# #";
System.out.println(dice.replace('#', character));
}
private void printThree(char character){
String dice = "#\n #\n #";
System.out.println(dice.replace('#', character));
}
我知道您已经有了答案,但另一种可能的解决方案是使用 ENUM。例如:
public class Dice
{
private enum DieFace
{
ONE('1', " \n * \n "),
TWO('2', "* \n \n *"),
THREE('3', "* \n * \n *"),
FOUR('4', "* *\n \n* *"),
FIVE('5', "* *\n * \n* *"),
SIX('6', "* *\n* *\n* *");
private char characterCode;
private String representation;
DieFace(char characterCode, String representation)
{
this.characterCode = characterCode;
this.representation = representation;
}
public static DieFace getDieFaceFromCharacterCode(char characterCode)
{
DieFace dieFaceFound = null;
for (DieFace dieFace : values())
{
if (dieFace.characterCode == characterCode)
{
dieFaceFound = dieFace;
break;
}
}
return dieFaceFound;
}
@Override
public String toString()
{
return this.representation;
}
}
public static String getDieFaceFromCharacter(char characterInput)
{
DieFace dieFace = DieFace.getDieFaceFromCharacterCode(characterInput);
return dieFace == null ? null : dieFace.toString();
}
}
这里是 class 的测试:
public class DieTest
{
@Test
public void testGetOne()
{
String expectedResult = " \n * \n ";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('1'));
}
@Test
public void testGetTwo()
{
String expectedResult = "* \n \n *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('2'));
}
@Test
public void testGetThree()
{
String expectedResult = "* \n * \n *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('3'));
}
@Test
public void testGetFour()
{
String expectedResult = "* *\n \n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('4'));
}
@Test
public void testGetFive()
{
String expectedResult = "* *\n * \n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('5'));
}
@Test
public void testGetSix()
{
String expectedResult = "* *\n* *\n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('6'));
}
@Test
public void testGetInvalid()
{
// < 1 is invalid
assertNull(Dice.getDieFaceFromCharacter('0'));
// invalid character (non-number)
assertNull(Dice.getDieFaceFromCharacter('a'));
// > 6 is invalid
assertNull(Dice.getDieFaceFromCharacter('7'));
}
}
简单的骰子程序
import java.util.Random;
public class Program
{
public static void main(String[] args) {
Random input =new Random();
int a = 3;
int b = 6;
int c =input.nextInt(b-a)+a;
switch(a){
case 2:
System.out.println("*");
break;
case 5:
System.out.println("* *");
break;
case 6:
System.out.println("* \n * \n *");
break;
case 4:
System.out.println("* * \n* *");
break;
case 1:
System.out.println("* *\n *\n* *");
break;
case 3:
System.out.println(" * * *\n\n * * *");
break;
}
}
}
如果您希望尽量减少重用,可以使用 switch 语句。例如:
public class Dice
{
private static final String LINE_BREAK = "\n";
public static String getDieFaceFromCharacter(char characterInput)
{
String top = " ";
String middle = " ";
String bottom = " ";
switch(characterInput)
{
case '5':
top = "* *";
bottom = "* *";
case '1':
middle = " * ";
break;
case '3':
top = "* ";
middle = " * ";
bottom = " *";
break;
case '2':
top = "* ";
bottom = " *";
break;
case '6':
middle = "* *";
case '4':
top = "* *";
bottom = "* *";
}
return top + LINE_BREAK + middle + LINE_BREAK + bottom;
}
}
学校给我的作业是制作一个用户可以改变骰子眼睛的骰子游戏,他们给我的唯一提示是使用 ASCII table..
到目前为止,这是我的代码,我遇到了困难,因为我无法让数字成为用户的输入(我不是很有创意):
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
任何正确方向的提示或暗示将不胜感激!
计算机未附带将数字“4”转换为 ascii 图形的代码。
你必须自己写这个。我建议把这些画在一张纸上。在您的 java 代码中,您可以有一堆 if/elseif 语句,每个语句对应 6 个面孔。每个块将打印 3 行。首先锁定用于眼睛的角色,然后努力使用户可以稍后配置。
以下是帮助您入门的部分内容:
if (dieRoll == 5) {
System.out.println("* *");
System.out.println(" * ");
System.out.println("* *");
} else if (dieRoll == 6) {
// you figure it out from here...
在给定的示例中,eyeDice
是 '#'
。抛出的 dice
(请在 java 中加上一个小的 d
)将是 3、5、4 和 6。
因此你需要一些方法,比如:
void printDice(int dice, char eyDice) {
... System.out.println ...
}
和您的代码
int dice = (int)(Math.random()* 6 + 1);
while (dice < 6) {
printDice(dice, eyeDice);
dice = (int)(Math.random()* 6 + 1)
}
将打印 5(忽略眼睛):
System.out.println("? ?");
System.out.println(" ? ");
System.out.println("? ?");
这不是一个完整的示例,但它可以让您了解该怎么做
public static void main(String[] args){
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);
//If statement calling print
}
}
private void printOne(char character){
String dice = "\n # \n";
System.out.println(dice.replace('#', character));
}
private void printTwo(char character){
String dice = "# #\n\n# #";
System.out.println(dice.replace('#', character));
}
private void printThree(char character){
String dice = "#\n #\n #";
System.out.println(dice.replace('#', character));
}
我知道您已经有了答案,但另一种可能的解决方案是使用 ENUM。例如:
public class Dice
{
private enum DieFace
{
ONE('1', " \n * \n "),
TWO('2', "* \n \n *"),
THREE('3', "* \n * \n *"),
FOUR('4', "* *\n \n* *"),
FIVE('5', "* *\n * \n* *"),
SIX('6', "* *\n* *\n* *");
private char characterCode;
private String representation;
DieFace(char characterCode, String representation)
{
this.characterCode = characterCode;
this.representation = representation;
}
public static DieFace getDieFaceFromCharacterCode(char characterCode)
{
DieFace dieFaceFound = null;
for (DieFace dieFace : values())
{
if (dieFace.characterCode == characterCode)
{
dieFaceFound = dieFace;
break;
}
}
return dieFaceFound;
}
@Override
public String toString()
{
return this.representation;
}
}
public static String getDieFaceFromCharacter(char characterInput)
{
DieFace dieFace = DieFace.getDieFaceFromCharacterCode(characterInput);
return dieFace == null ? null : dieFace.toString();
}
}
这里是 class 的测试:
public class DieTest
{
@Test
public void testGetOne()
{
String expectedResult = " \n * \n ";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('1'));
}
@Test
public void testGetTwo()
{
String expectedResult = "* \n \n *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('2'));
}
@Test
public void testGetThree()
{
String expectedResult = "* \n * \n *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('3'));
}
@Test
public void testGetFour()
{
String expectedResult = "* *\n \n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('4'));
}
@Test
public void testGetFive()
{
String expectedResult = "* *\n * \n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('5'));
}
@Test
public void testGetSix()
{
String expectedResult = "* *\n* *\n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('6'));
}
@Test
public void testGetInvalid()
{
// < 1 is invalid
assertNull(Dice.getDieFaceFromCharacter('0'));
// invalid character (non-number)
assertNull(Dice.getDieFaceFromCharacter('a'));
// > 6 is invalid
assertNull(Dice.getDieFaceFromCharacter('7'));
}
}
简单的骰子程序
import java.util.Random;
public class Program
{
public static void main(String[] args) {
Random input =new Random();
int a = 3;
int b = 6;
int c =input.nextInt(b-a)+a;
switch(a){
case 2:
System.out.println("*");
break;
case 5:
System.out.println("* *");
break;
case 6:
System.out.println("* \n * \n *");
break;
case 4:
System.out.println("* * \n* *");
break;
case 1:
System.out.println("* *\n *\n* *");
break;
case 3:
System.out.println(" * * *\n\n * * *");
break;
}
}
}
如果您希望尽量减少重用,可以使用 switch 语句。例如:
public class Dice
{
private static final String LINE_BREAK = "\n";
public static String getDieFaceFromCharacter(char characterInput)
{
String top = " ";
String middle = " ";
String bottom = " ";
switch(characterInput)
{
case '5':
top = "* *";
bottom = "* *";
case '1':
middle = " * ";
break;
case '3':
top = "* ";
middle = " * ";
bottom = " *";
break;
case '2':
top = "* ";
bottom = " *";
break;
case '6':
middle = "* *";
case '4':
top = "* *";
bottom = "* *";
}
return top + LINE_BREAK + middle + LINE_BREAK + bottom;
}
}