我想知道如何让程序随机输出两个字符串之一。 很难解释,但这是我的例子:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string age;
cout << "How old are you?" << endl;
cin >> age;
if (age < 0)
{
cout << "Invalid age" << endl;
}
if (0 >= age && age <<= 3)
{
cout << "Oh you're just a baby" << endl;
// OR (random)
cout << "Time to take a nap!" << endl;
}
return 0;
}
我想编程输出“哦,你只是个婴儿”或“该睡午觉了!”每当用户输入 0 到 3 之间的数字时随机出现。有人可以解释一下吗?
最佳答案
尝试:
#include <cstdlib>
...
...
...
/* initialize random seed: */
srand(time(NULL));
/* generate secret number: */
int ss = rand() % 2;
if (ss) {
cout << "Oh you're just a baby" << endl;
} else {
cout << "Time to take a nap!" << endl;
}
关于c++ - 选择两个字符串之一输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513875/