Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java循环的问题_Java_Loops_Switch Statement_Dice - Fatal编程技术网

java循环的问题

java循环的问题,java,loops,switch-statement,dice,Java,Loops,Switch Statement,Dice,我想做一个骰子,一直掷到6点: 当它命中1时,会弹出1和一个独眼骰子,然后再次抛出,。。 用随机数(显然是1-6)直到它达到6。当它达到6 它应该停止 现在我这里有一个开关,当按下时显示正确的数字,但是我很难让这个开关正常工作。或者它命中除6之外的所有数字并不断生成数字,或者它不断抛出相同的数字 谁能帮我一把吗 非常感谢 publicstaticvoidmain(字符串[]args){ //询问使用什么符号打印骰子的眼睛 System.out.print(“选择用于眼睛的符号:”); char

我想做一个骰子,一直掷到6点: 当它命中1时,会弹出1和一个独眼骰子,然后再次抛出,。。 用随机数(显然是1-6)直到它达到6。当它达到6 它应该停止

现在我这里有一个开关,当按下时显示正确的数字,但是我很难让这个开关正常工作。或者它命中除6之外的所有数字并不断生成数字,或者它不断抛出相同的数字

谁能帮我一把吗

非常感谢


publicstaticvoidmain(字符串[]args){
//询问使用什么符号打印骰子的眼睛
System.out.print(“选择用于眼睛的符号:”);
char ch;
扫描仪sc=新的扫描仪(System.in);
ch=sc.findInLine(“.”)字符(0);
int骰子=(int)(6*Math.random())+1;
做{
开关(骰子%6){
案例0:System.out.println(“1”);
系统输出println(ch);
打破
案例1:System.out.println(“2”);
System.out.println(ch+“\n\n”+ch);
打破
案例2:System.out.println(“3”);
System.out.println(ch+“\n”+ch+“\n”+ch);
打破
案例3:System.out.println(“4”);
System.out.println(ch+“”+ch+“”\n“+ch+“”+ch);
打破
案例4:System.out.println(“5”);
System.out.println(ch+“”+ch+“”\n+“”+ch+“”\n“+ch+“”+ch);
打破
}
}
而(骰子<6);
//Else{System.out.println(“6”);
//System.out.println(ch+“”+ch+“\n”+ch+“”+ch+“\n”+ch+
//“+ch”;
}
}
}

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

int dice = (int)(6*Math.random()) + 1;
(否则,您实际上只会抛出一次骰子。)


另外,您生成随机数的方式、打开随机数的方式以及
while
条件彼此并不完全一致。

您在循环之外生成随机数,因此它将永远不变。以下是工作版本:

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);
 }
publicstaticvoidmain(字符串[]args){
//询问使用什么符号打印骰子的眼睛
System.out.print(“选择用于眼睛的符号:”);
char ch;
扫描仪sc=新的扫描仪(System.in);
ch=sc.findInLine(“.”)字符(0);
int骰子=(int)(6*Math.random())+1;
做{
开关(骰子%6){
案例0:System.out.println(“1”);
系统输出println(ch);
打破
案例1:System.out.println(“2”);
System.out.println(ch+“\n\n”+ch);
打破
案例2:System.out.println(“3”);
System.out.println(ch+“\n”+ch+“\n”+ch);
打破
案例3:System.out.println(“4”);
System.out.println(ch+“”+ch+“”\n“+ch+“”+ch);
打破
案例4:System.out.println(“5”);
System.out.println(ch+“”+ch+“”\n+“”+ch+“”\n“+ch+“”+ch);
打破
}
骰子=(int)(6*Math.random())+1;
}
而(骰子<6);
系统输出打印项次(“6”);
//System.out.println(ch+“”+ch+“\n”+ch+“”+ch+“\n”+ch+
//“+ch”;
}

是联机工作版本。

因为您只随机生成一次数字。循环条件错误,应该是骰子%6==0。在循环中生成随机数/还有另一个问题需要解决。你的骰子值将是1比6。然后取它的模数。1%6=1,但您将其设置为案例0。这确实有效,但现在它在为6时不显示6。如何实现“如果骰子=6,则显示6并停止”?你能告诉我为什么我要把骰子放在圈里和上面吗。我可以看到它们都在那里,而不是当我移除一个的时候,它是有效的,。。我不明白为什么。谢谢你的回复though@Rick如果你想看六行代码,只需从我的代码中删除最后两行注释,你不必把骰子放在循环上面,我只想让它看起来更像你的代码,你可以简单地把int骰子=(int)(6*Math.random())+1;在你的循环中,它会起作用,因为每次迭代都会产生另一个随机数。我希望这有帮助。如果这是你想要的,请标出这个答案。谢谢!所以基本上。。。你可以说,当它不具备while中提到的条件时,在while之后会发生什么?所以while(dice@Rick,确切地说,虽然可能是if(dice==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;
        }
        dice = (int)(6*Math.random()) + 1;
    }
    while(dice < 6);
    System.out.println("6");
       //         System.out.println(ch + " " + ch + "\n" + ch + " " + ch + "\n" + ch +
        //        " " + ch);
 }