即使条件为 false,Java do-while 循环也会继续

Java do-while loop continues even though the condition is false

提问人:user21505321 提问时间:3/28/2023 更新时间:3/28/2023 访问量:76

问:

import java.util.Scanner;
public class DealCardsGNN{
public static void main(String[] args){

int numCards = 52;
int CardDealt = 0;

String CardName = "";
String SuitName = "";
char response = ' ';

do{

  while(CardDealt <= numCards){

  int card = (int)(Math.random() * 13) + 1;
  int suit = (int)(Math.random() * 4) + 1;

  switch(card){
    case 1: CardName = "Ace"; break;
    case 2: CardName = "2"; break;
    case 3: CardName = "3"; break;
    case 4: CardName = "4"; break;
    case 5: CardName = "5"; break;
    case 6: CardName = "6"; break;
    case 7: CardName = "7"; break;
    case 8: CardName = "8"; break;
    case 9: CardName = "9"; break;
    case 10: CardName = "10"; break;
    case 11: CardName = "Jack"; break;
    case 12: CardName = "Queen"; break;
    case 13: CardName = "King"; break;
    
  }

  switch(suit){
    case 1: SuitName = "Diamonds"; break;
    case 2: SuitName = "Clubs"; break;
    case 3: SuitName = "Hearts"; break;
    case 4: SuitName = "Spades"; break;
  
  }
  
  System.out.println("The card is" + " " + CardName + " " + "of" + " " + SuitName);
  CardDealt++;
  
  Scanner scan = new Scanner(System.in);
  
  System.out.println("Do you want another card? (Enter Y or N)");
  response = scan.next().charAt(0);
  
  
  }

} while(response == 'Y'); 

}
}

该程序应该在不使用数组的情况下模拟发牌。我在 do-while 循环中编写了一个 while 循环和开关,该循环的条件是响应等于字符 Y。但是,即使不满足条件,循环也会重复。我将如何解决这个问题。

java while-loop switch-statement do-while

评论

2赞 user16320675 3/28/2023
也许有一个正确(一致的)缩进会有助于更好地看到问题

答:

3赞 dan1st 3/28/2023 #1

您遇到的问题

请注意,您有嵌套循环。

do{
    while(CardDealt <= numCards){
        //...
        response = scan.next().charAt(0);
    }
} while(response == 'Y'); 

因此,您只会循环直到所有牌都发完为止,然后才检查响应。

相反,您可以将检查集成到 -loop 中:CardDealt <= numCardsdo while

do{
    //...
    response = scan.next().charAt(0);
} while(response == 'Y' && CardDealt <= numCards); 

这将检查每次迭代中的响应,如果不是,则停止循环。Y

其他问题/改进

重新创造Scanner

另外,我不建议您在每次迭代中都创建一个新的。相反,您可以在整个程序中重复使用相同的扫描仪:Scanner

Scanner scan = new Scanner(System.in);//before the loop
do{
    //...
    //also do not create the scanner here
    response = scan.next().charAt(0);
} while(response == 'Y' && CardDealt <= numCards); 

一张牌抽得太多了

正如评论中所述,当已经发了 52 张牌时,您也抽了一张牌,这也太多了(这在一差错误中称为)。而不是 ,您需要使用(只有在取用少于 52 张卡时才取另一张卡)。CardDealt <= numCardsCardDealt < numCards

Scanner scan = new Scanner(System.in);//before the loop
do{
    //...
    //also do not create the scanner here
    response = scan.next().charAt(0);
} while(response == 'Y' && CardDealt < numCards); //< instead of <= here

重复取卡

饰演 Ole V.V. 在评论中提到,您的代码可能会多次选择同一张卡。

如果您能够使用收集 API(如果您不能使用数组,则不太可能),您可以通过收集列表中的所有卡片、对其进行随机播放,然后每次删除一张卡片来解决这个问题。

public record Card(String name, String suit){}//or similar

然后,您可以使用循环创建卡片列表:

List<String> cardNames=List.of("Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",  "Jack", "Queen", "King");
List<String> suitNames=List.of("Diamonds","Clubs","Hearts","Spades");
List<Card> cards=new ArrayList<>();

//go through all combinations of card and suit names
for(String cardName:cardNames){
    for(String suitName:suitNames){
        //add that as a card
        cards.add(new Card(cardName, suitName))
    }
}

//shuffle the list of cards
Collections.shuffle(cards);

//draw a card
//just do this every time you draw a card
int index=cards.size()-1;
Card card=cards.remove(index);

//check whether there are cards left
//!cards.isEmpty()

在代码中将它们放在一起:

List<String> cardNames=List.of("Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",  "Jack", "Queen", "King");
List<String> suitNames=List.of("Diamonds","Clubs","Hearts","Spades");
List<Card> cards=new ArrayList<>();

//go through all combinations of card and suit names
for(String cardName:cardNames){
    for(String suitName:suitNames){
        //add that as a card
        cards.add(new Card(cardName, suitName))
    }
}

//shuffle the list of cards
Collections.shuffle(cards);


char response = ' ';
Scanner scan = new Scanner(System.in);
do{
    int index=cards.size()-1;
    Card card=cards.remove(index);
    System.out.println("The card is" + " " + card.name() + " " + "of" + " " + card.suit());
    System.out.println("Do you want another card? (Enter Y or N)");
    response = scan.next().charAt(0);
} while(response == 'Y' && !cards.isEmpty()); 

在另一个文件中:

public record Card(String name, String suit){}

评论

0赞 Anonymous 3/28/2023
此外,检查发牌的数量在这里可能没有多大意义,因为每次都发随机和独立的牌。您可以在 52 张牌内轻松获得 7 张方块的 3 次,因此发了 52 张牌并不意味着发了整副牌。
0赞 dan1st 3/28/2023
哦,我没有读那么远。
1赞 dan1st 3/28/2023
我现在在我的回答中解决了这些问题。
0赞 dan1st 3/28/2023
但我认为引入额外的 / 之类的事情有点矫枉过正。QueueDeque