Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.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

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
C++ While循环和gen rand()_C++_Loops_Random_While Loop - Fatal编程技术网

C++ While循环和gen rand()

C++ While循环和gen rand(),c++,loops,random,while-loop,C++,Loops,Random,While Loop,所以我基本上是在创建一个21点游戏,我必须问用户是否需要另一张牌(打),我决定做一个while循环。我想弄明白的一个问题是,我如何才能使第一次命中时选择的随机数不能再被选择,我被卡住了,因为我正在将该变量分配给“card3” while(hit='yes'| | hit='yes'| | hit='Y'| | hit='Y') { //创建新卡,添加到总数,再次询问 card3=rand()%51+1; 而(card3==card1 | | deal1==card2 | | card3==dea

所以我基本上是在创建一个21点游戏,我必须问用户是否需要另一张牌(打),我决定做一个while循环。我想弄明白的一个问题是,我如何才能使第一次命中时选择的随机数不能再被选择,我被卡住了,因为我正在将该变量分配给“card3”

while(hit='yes'| | hit='yes'| | hit='Y'| | hit='Y')
{
//创建新卡,添加到总数,再次询问
card3=rand()%51+1;
而(card3==card1 | | deal1==card2 | | card3==deal1){
card3=rand()%52+1;
}
如果(card3==1 | | card3==2 | | card3==3 | | card3==4){

你可以把所有的牌放在一个容器里,然后用它洗牌。然后从容器后面一次弹出一张牌。当牌堆完成后,再开始。看起来像是一个练习,你应该使用数组。我不能为你编写程序,但要读数组——它们是解决方案的重要部分。Y你可以制作一个由52个布尔变量组成的数组,用于指示已拾取的卡片。你应该设计一个算法来检查卡片是否已拾取,当我们随机拾取卡片时,继续拾取一张新卡片。

不幸的是,我只能做课堂上教给我们的事情,所以如果语句和loops@MichaelRamos那么你应该在问题中清楚地说明你的要求,因为这些要求都是人为的。这是有限制的作业,还是你自己在学习?@hyde这是作业可能与@Johnsyweb重复不这是关于受到打击,而不是生成用户第二张卡片处理第三张、第四张、第五张第一张随机牌来自洗牌组…都是一样的。我们没有涵盖阵列,所以我不能使用它们
while (hit == 'yes' || hit == 'Yes' || hit =='Y' || hit == 'y')
{
//create new card, add onto total, ask again
card3 = rand() % 51 + 1;
while (card3 == card1 || deal1 == card2 || card3 == deal1){
  card3 = rand() % 52 + 1;
}
if (card3 == 1 || card3 == 2 || card3 == 3 || card3 == 4){
cout <<"|A|";
total = total + 11;}
else if (card3 == 5 || card3 == 6 || card3 == 7 || card3 == 8){
cout <<"|2|";
total = total + 2;}
else if (card3 == 9 || card3 == 10 || card3 == 11 || card3 == 12){
cout <<"|3|";
total = total + 3;}
else if (card3 == 13 || card3 == 14 || card3 == 15 || card3 == 16){
cout <<"|4|";
total = total + 4;}
else if (card3 == 17 || card3 == 18 || card3 == 19 || card3 == 20){
cout <<"|5|";
total = total + 5;}
else if (card3 == 21 || card3 == 22 || card3 == 23 || card3 == 24){
cout <<"|6|";
total = total + 6;}
else if (card3 == 25 || card3 == 26 || card3 == 27 || card3 == 28){
cout <<"|7|";
total = total + 7;}
else if (card3 == 29 || card3 == 30 || card3 == 31 || card3 == 32){
cout <<"|8|";
total = total + 8;}
else if (card3 == 33 || card3 == 34 || card3 == 35 || card3 == 36){
cout <<"|9|";
total = total + 9;}
else if (card3 == 37 || card3 == 38 || card3 == 39 || card3 == 40){
cout <<"|10|";
total = total + 10;}
else if (card3 == 41 || card3 == 42 || card3 == 43 || card3 == 44){
cout <<"|J|";
total = total + 10;}
else if (card3 == 45 || card3 == 46 || card3 == 47 || card3 == 48){
cout <<"|Q|";
total = total + 10;}
else if (card3 == 49 || card3== 50 || card3 == 51 || card3 == 52){
cout <<"|K|";
total = total + 10;}
cout << endl;
cout <<"Your total is: " << total << endl;
cout <<"Would you like another card? yes or no: " << endl;
cin >> hit; 
cout << endl;
}