我想做一个骰子,不断抛出,直到它击中6: 当它击中1时,它弹出1和一个眼睛的骰子,并再次抛出,.. 只是随机数字(明显1-6)直到命中6.当命中6 它应该停止。问题与我的Java循环
现在我有了这个开关,它显示了正确的数字时,但我有麻烦让这个开关正常工作。或者它击中所有的数字,但六,并继续生成数字,或者它一直抛出相同的数字。
任何人都可以借我一把吗?
非常赞赏
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);
}
}
}
因为你只是随机产生一次数字。 – proulxs 2014-09-30 19:41:12
你的循环条件是错误的,它应该是骰子%6 == 0。在循环中生成随机数/ – StackFlowed 2014-09-30 19:41:14
还有另一个需要涉及的问题。你的骰子值将是1到6.然后你取这个模数。 1%6 = 1,但你有设置为案例0. – Compass 2014-09-30 19:44:37