java while循环未中断

java while循环未中断,java,for-loop,while-loop,infinite-loop,Java,For Loop,While Loop,Infinite Loop,所以我在写一个程序,有人从一副牌中抽牌。所以我写了一个while循环,循环并检查是否有超过4张随机创建的卡被抽出来,如果有,就换卡 这是我的密码: String card = (int)Math.ceil(Math.random() * 13) + " "; String[] used2 = used.split(" "); //used is a String like "12 3 7 8 4 ... # etc" such that it is all the previously draw

所以我在写一个程序,有人从一副牌中抽牌。所以我写了一个while循环,循环并检查是否有超过4张随机创建的卡被抽出来,如果有,就换卡

这是我的密码:

String card = (int)Math.ceil(Math.random() * 13) + " ";
String[] used2 = used.split(" ");
//used is a String like "12 3 7 8 4 ... # etc" such that it is all the previously drawn cards.
boolean checking = true;
boolean isIn = false;
int in = 0;
int check = 0;
while(checking){
    for(int q = 0; q < used2.length; q++){
        check += 1;
        if(card.equals(used2[q] + " ")){
            in += 1;
            if(in == 4){
                System.out.println(check); //debugging line
                check += 1;
                card = (int)Math.ceil(Math.random() * 13) + " ";
                card_val = (int)Math.ceil(Math.random() * 13);
                isIn = true;
                in = 0;
                break;
            }
        }
    }
    if(isIn){
        //will execute if there is 4 of the cards already drawn so the while loop continues with a different card
        checking = true;
    }
    else{
        //breaks out of while loop because the card can be drawn
        checking = false;
    }
}
used += card;
stringcard=(int)Math.ceil(Math.random()*13)+;
字符串[]used2=used.split(“”);
//使用的是类似“123784…#等”的字符串,因此它是所有先前绘制的卡。
布尔检查=真;
布尔isIn=false;
int in=0;
整数检查=0;
同时(检查){
对于(int q=0;q
现在它运行了,但当我把它放在for循环中,并将它设置为运行40次,大约2/3次,它创建了一个无限循环

我发现只有当
if(in==4)
语句为真时,才会创建无限循环


为什么会这样?从昨晚开始我就一直在调试,我无法解决这个问题。

一旦你将
isIn
设置为
true
,你就再也不会将其设置为
false
。因此,底部的
if
语句将继续将
checking
设置为
true
,从而产生无限循环

循环开始时,将
isIn
设置为
false

while(checking){
    isIn = false;  // Add this line.
    for(int q = 0; q < used2.length; q++){
while(检查){
isIn=false;//添加此行。
对于(int q=0;q
一旦将
isIn
设置为
true
,就再也不会将其设置回
false
。因此,底部的
if
语句将继续将
checking
设置为
true
,从而产生无限循环

循环开始时,将
isIn
设置为
false

while(checking){
    isIn = false;  // Add this line.
    for(int q = 0; q < used2.length; q++){
while(检查){
isIn=false;//添加此行。
对于(int q=0;q
对于一项简单的任务来说,似乎是一个复杂的算法

为什么不将实现更改为使用简单减法?首先分配一个包含所有52个值的
列表
,然后当您从牌组中“抽出”牌时,将其从列表中删除


然后您可以使用
Math.random()*list.size()
来获得适当范围的索引?

对于一个简单的任务来说,似乎是一个复杂的算法

为什么不将实现更改为使用简单减法?首先分配一个包含所有52个值的
列表
,然后当您从牌组中“抽出”牌时,将其从列表中删除


然后可以使用
Math.random()*list.size()
获取适当范围的索引?

编写
if else
语句的更简洁方法:
checking=isIn;
因为您在该块中将
isIn设置为
true
,这意味着
checking
永远不会设置为
false
。还有对
(int)Math.ceil(Math.random()*13)的注释
。我怀疑您之所以使用
是为了将int转换为字符串,但是您只能执行
+”
,这样以后代码会更简单。@KubaSpatny:我不确定您的意思that@RyanSaxe那么,
+''的原因是什么
?编写
if else
语句的更简洁方法:
checking=isIn;
因为您在该块中将
isIn
设置为
true
,这意味着
checking
永远不会设置为
false
。还有对
(int)Math.ceil(Math.random()*13)+
的注释。我怀疑您的原因是
是将int转换为字符串,但是您只能执行
+”
,这样以后代码会更简单。@KubaSpatny:我不确定您的意思that@RyanSaxe那么,
+”的原因是什么“
?我还没有学会列表,这是一个硬件问题,我们甚至还没有学会数组,我刚刚知道拆分方法,所以我用它来简化字符串的操作。我还没有学会列表,这是一个硬件问题,我们甚至还没有学会数组,我刚刚知道拆分方法,所以我用它来简化字符串的操作