【发布时间】:2014-11-25 13:11:24
【问题描述】:
我想制作一个一直被抛出的骰子,直到它达到 6: 当它击中 1 时,它会弹出 1 和一个单眼骰子并再次投掷,.. 只是随机数(显然是 1-6),直到它达到 6。当它达到 6 它应该停止。
现在我在这里有了这个开关,它在被击中时会显示正确的数字,但是我无法让这个开关正常工作。或者它击中所有数字,但 6 并不断生成数字,或者它一直抛出相同的数字。
谁能帮帮我?
非常感谢
public static void main(String[] args) {
// asking what symbol to use to print the eye(s) of the dice
System.out.print("choose symbol to use for eyes: ");
char ch;
Scanner sc = new Scanner(System.in);
ch = sc.findInLine(".").charAt(0);
int dice = (int)(6*Math.random()) + 1;
do{
switch(dice % 6){
case 0: System.out.println("1");
System.out.println(ch);
break;
case 1: System.out.println("2");
System.out.println(ch + "\n\n " + ch);
break;
case 2: System.out.println("3");
System.out.println(ch + "\n " + ch + "\n " + ch);
break;
case 3: System.out.println("4");
System.out.println(ch + " " + ch + "\n" + ch + " " + ch);
break;
case 4: System.out.println("5");
System.out.println(ch + " " + ch + "\n" + " " + ch + " \n"+ ch + " " + ch);
break;
}
}
while(dice < 6);
// Else{ System.out.println("6");
// System.out.println(ch + " " + ch + "\n" + ch + " " + ch + "\n" + ch +
// " " + ch);
}
}
}
【问题讨论】:
-
因为你只是随机生成一个数字一次。
-
你的循环条件不对,应该是 dice%6==0。在循环中生成随机数/
-
还有一个问题需要解决。你的骰子值将是 1 到 6。然后你取它的模数。 1 % 6 = 1,但您将其设置为 case 0。
标签: java loops switch-statement dice