【问题标题】:Problems with my java loop我的 java 循环有问题
【发布时间】: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


【解决方案1】:

您需要在循环内移动以下内容:

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

(否则你实际上只掷一次骰子。)

另外,你生成随机数的方式,你打开它的方式,和while条件也不是很一致。

【讨论】:

  • 感谢您的回复。如果没有开关,你会如何建议?
【解决方案2】:

你在循环之外生成你的随机数,所以它永远都是一样的,这是工作版本:

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是在线工作版。

【讨论】:

  • 这确实有效,但现在它是 6 时不显示 6。如何实现“if dice = 6 then show 6 and stop”?你能告诉我为什么我必须把骰子放在循环上面吗?我可以看到它适用于它们两者都在那里而不是当我移除一个时,..不明白为什么。感谢您的回复
  • @Rick 如果你想展示六人的眼睛,只需从我的代码中删除最后一行的两个 cmets,你不必将骰子放在循环上方,我只是希望它看起来更像你的代码,你可以简单地把 int dice = (int)(6*Math.random()) + 1;在你的循环中它会起作用,因为每次迭代它都会生成另一个随机数。我希望这会有所帮助。如果你想要什么,请标记这个答案。
  • 谢谢!所以基本上......你可以说当它不具备一段时间后提到的条件时应该发生什么?所以 while(dice
  • @Rick 完全正确,虽然它可能是 if(dice == 6)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多