추가해야 할 항목이 있으므로 숫자가 계속해서 8이 아니라 1에서 9까지의 숫자 중 하나를 계속 선택하지 않으려면 어떻게해야합니까? 사드란?C에서 1과 9 사이의 임의의 숫자 생성
int main()
{
int iRand = (rand() % 9+1);
if (iRand==1)
{
cout << "The planet of the day is Mercury!" <<endl;
cout << "Mercury is the closest planet to the sun." <<endl;
}
else if (iRand==2)
{
cout << "The planet of the day is Venus!" <<endl;
cout << "Venus is the hottest planet in our solar system." <<endl;
}
// .... 3..4..5..6..7..8
else
{
cout << "The planet of the day is Pluto!" <<endl;
}
return 0;
}
'srand()'함수를 보라. –
컴파일러가 C++ 11을 지원한다면,'rand()'를 버리고 새로운 랜덤 기능을 사용하는 것이 현명 할 수 있습니다. 이 질문은 중복 가능성이 높습니다. – goji
괄호()를 볼 필요가있는 것 같습니다. '(rand() % 8) + 1'을 시도하십시오. –