C ++ BINGO循环布尔逻辑

时间:2019-10-25 04:44:39

标签: c++

我需要创建一个循环以生成一个随机数1-75并根据该数字输出BINGO,并且仅在输出所有字母时才完成。

无法提出一个循环

/*
Section 1 : Generate a random number from 1 to 75.
Section 2 : If the number is between 1 to 15, attach B and output the number.
Section 3 : If the number is between 16 to 30, attach I and output the number.
Section 4 : If the number is between 31 to 45, attach N and output the number.
Section 5 : If the number is between 46 to 60, attach G and output the number.
Section 6 : If the number is between 61 to 75, attach O and output the number.
Section 7 : Repeat Sec 1 through 6 until the program generates all B I N G O.
*/
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    int B1;
    bool B = false, I = false, N = false, G = false, O = false, bingo = false;
    srand(time(NULL)); // This line set the random number seed.
    int num = (rand() % 75) + 1;
    if (num <= 15)
    {
        cout << "B-" << num << endl;
        B = true;
    }
    else if (num <= 30)
    {
        cout << "I-" << num << endl;
        I = true;
    }
    else if (num <= 45)
    {
        cout << "N-" << num << endl;
        N = true;
    }
    else if (num <= 60)
    {
        cout << "G-" << num << endl;
        G = true;
    }
    else if (num <= 75)
    {
        cout << "O-" << num << endl;
        O = true;
    }
    return 0;
} // main

我不确定在创建循环时该去哪里或如何进行。据我所知。

2 个答案:

答案 0 :(得分:1)

根据我的理解,您想继续循环直到您填满BINGO的所有字母为止,因此我在这种情况下添加了一个while循环。找到所有字母后,它将停止。

Section 1 : Generate a random number from 1 to 75. 
Section 2 : If the number is between 1 to 15, attach B and output the number.
Section 3 : If the number is between 16 to 30, attach I and output the number. 
Section 4 : If the number is between 31 to 45, attach N and output the number. 
Section 5 : If the number is between 46 to 60, attach G and output the number. 
Section 6 : If the number is between 61 to 75, attach O and output the number. 
Section 7 : Repeat Sec 1 through 6 until the program generates all B I N G O. */ 
#include<iostream>
#include<ctime>
using namespace std;
int main() {
  int B1;
  bool B = false, I = false, N = false, G = false, O = false, bingo = false;
  srand(time(NULL)); // This line set the random number seed.
  while (!(B && I && N && G && O)) {
    int num = (rand() % 75) + 1;
    if(num <= 15) {
      cout << "B-" << num << endl; 
      B = true; 
    } else if (num <= 30) {
      cout << "I-" << num << endl; 
      I = true; 
    } else if (num <= 45) { 
      cout << "N-" << num << endl; 
      N = true; 
    } else if (num <= 60) { 
      cout << "G-" << num << endl; 
      G = true; 
    } else if (num <= 75) { 
      cout << "O-" << num << endl; 
      O = true; 
    }
  }
  return 0; 
} // main 

答案 1 :(得分:0)

添加带有标记<div id="myModal" class="modal"> <span class="close">&times;</span> <img class="modal-content"> </div> let $modal = $('.modal') let $img = $("#imageContainer img"); $img.click(function(){ $('.modal-content').attr('src', $(this).attr('src')); $modal.css('display', 'block') let $span = $(".close"); $span.click(function() { $('.modal').css('display',"none"); }) }) 的{​​{1}}循环。在循环结束时,请检查是否所有 字母。

while