2014-09-30 60 views
0

我想做一个骰子,不断抛出,直到它击中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); 
     } 
    } 

} 
+2

因为你只是随机产生一次数字。 – proulxs 2014-09-30 19:41:12

+0

你的循环条件是错误的,它应该是骰子%6 == 0。在循环中生成随机数/ – StackFlowed 2014-09-30 19:41:14

+0

还有另一个需要涉及的问题。你的骰子值将是1到6.然后你取这个模数。 1%6 = 1,但你有设置为案例0. – Compass 2014-09-30 19:44:37

回答

0

你产生循环外面的随机数,所以这将是永远一样这里的工作版本:

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; 
     } 
     dice = (int)(6*Math.random()) + 1; 
    } 
    while(dice < 6); 
    System.out.println("6"); 
     //   System.out.println(ch + " " + ch + "\n" + ch + " " + ch + "\n" + ch + 
     //  " " + ch); 
} 

here是在线工作版本。

+0

这是工作,但现在它不显示6时,如何执行“如果骰子= 6然后显示六停止”?你能告诉我为什么我必须把骰子放在循环中和上面。我可以看到它与他们在那里工作,而不是当我删除一个,..不明白为什么。谢谢你的回复 – Rick 2014-09-30 20:10:39

+0

@Rick如果你想展示六只眼睛,只需从我的代码中移除两行最后一行注释,你不必把骰子放在循环之上,我只是希望它看起来更像你的代码,你可以简单地把int dice =(int)(6 * Math.random())+ 1;在你的循环内部,它会工作,因为每次迭代它都会产生另一个随机数。我希望这个帮助。请如果它你想要标记这个答案。 – Lrrr 2014-09-30 20:27:23

+0

谢谢!所以基本上......你可以只说出在这段时间之后没有提到的条件时应该发生什么?所以虽然(骰子<6);和下一行说当它大于5时会发生什么? – Rick 2014-09-30 20:41:35

2

您需要将循环内的以下内容:

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

(否则你有效地扔模具只有一次)

而且,您生成随机数的方式,您打开它的方式,以及条形码while离子不完全一致。

+0

感谢您的答复。你会如何建议没有开关呢? – Rick 2014-09-30 20:17:16