Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试做一个循环,直到使用 srand 生成的数字等于我想要的某个数字。解决此问题的最佳方法是什么?我尝试使用 do while 循环来执行此操作,但它不起作用。下面的例子
do{ num = rand()%10+1; }while(x == 5); cout << x;
“x”数总是等于一个随机数,但它不会像语句中那样总是等于 5
do{ x = rand()%10+1; //Use number which changes }while(x != 5); //Use Not Equal to 5 ! cout << x;
或者
x=5; do{ num = rand()%10+1; }while(num != x); //Check if x equals n cout << num;