gpt4 book ai didi

c++ - while循环转义范围

转载 作者:太空宇宙 更新时间:2023-11-04 15:26:14 25 4
gpt4 key购买 nike

我有一个 while 循环,我希望在我点击数字 1 到 5 时跳出。

对此最好的陈述是什么?

我目前有这个。

while (  oneChoice!= 1 ||  oneChoice!= 2 || oneChoice!= 3 || oneChoice!= 4 || oneChoice!= 5  )
{
cout << "Please make a selection" << endl;
cout << "Choose once more: ";
cin >> oneChoice;
break;
}

最佳答案

我会这样做:

int n;
for (;;)
{
cout << "Please make a selection (1-5): "
cin >> n;
if (n >= 1 && n <= 5) break;
cout << "You must choose a number from 1 through 5.\n";
}

中断位于中间,以便仅当用户输入的值超出可接受范围时才会打印错误消息。 for (;;) 是适用于您不想在顶部或底部测试退出条件的循环的正确 C 系列习惯用法。

关于c++ - while循环转义范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7706182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com