C++ 条件while循环(此社区新增)

C++ 条件while循环(此社区新增),c++,C++,我是这个网站的新手。 我是一名新手,我正在创建一个简单的游戏,名为“用代码选择数字”: #include <iostream> using namespace std; int main() { double number; char letter; char choice; while (number=true) { cout<<"enter a number from 0-10. no letters"<

我是这个网站的新手。 我是一名新手,我正在创建一个简单的游戏,名为“用代码选择数字”:

#include <iostream>
using namespace std;
int main()
{
    double number;
    char letter;
    char choice;
    while (number=true)

    {
        cout<<"enter a number from 0-10. no letters"<<endl;
        cin>>number;
        if (number==6){
            cout<<"you win"<<endl;
            system ("pause");}
        else if (number<0||number>10){
            cout<<"pick a number from the range given"<<endl;
            system ("pause");}
        else if (number!=6){
            cout<<"you lose"<<endl;
            system ("paeuse");}
        else if (letter!=6){
            cout<<"no letter. just a number"<<endl;
            system ("pause");}

        while (choice=true)
        {
            cout<<"would you like to play again? Y/y for yes, and N/n for no."<<endl;
            cin>>choice;
            if (choice=='Y','y'){
                cout<<"lets play again"<<endl;
                system ("pause");}
            else if (choice=='N','n'){
                cout<<"come again later"<<endl;
                system ("pause");
                return choice;}
            else if (choice!='Y','y','N','n'){
                cout<<"unknown. do you want to play?"<<endl;
                system ("pause");}
        }
    }
}
#包括
使用名称空间std;
int main()
{
双数;
字符字母;
字符选择;
while(number=true)
{
coutcomparation与赋值
=
运算符用于赋值。因此,
while(choice=true)
表示:

  • 循环的每次迭代,将
    choice
    设置为
    true
  • 对条件使用
    选项的值
=
运算符用于比较,例如
while(choice==true)
。这将实现您所期望的功能

然而,
==true
是多余的。比较
香蕉是黄色的颜色是事实吗?
是香蕉黄色的颜色吗?
。更惯用的说法是
while(choice)

系统
使用
系统
的做法是许多程序员都不喜欢的,但从技术上讲,这应该是可行的。这里的问题是拼写:
paeuse
->
pause

多项选择 不幸的是,此语法与您认为的不同:

if(a == 'b', 'c', 'd')
您需要完全指定每个条件,如下所示:

if((a == 'b') || (a == 'c') || (a == 'd'))
或者,您可以使用开关:

switch(choice){
case 'Y':
case 'y':
    /* yes code */
    break;
case 'N':
case 'n':
    /* no code */
    break;
default:
    /* none of the above */
}
循环应该什么时候结束? 你这么说是什么意思?就像说,
哪个更大?灯泡还是-7?
没有意义

返回
return
在代码中结束主循环,并将传递给它的值作为程序的退出代码。这可能与您的预期相差甚远

未赋值变量
字母
你在哪里使用字母?它只用于将它与某物进行比较,但由于你没有设置它,因此它是未定义的行为

在编译器中打开警告 他们是你的朋友,而不是你的敌人。你可能不想得到更多的编译器错误,但如果你关闭它们,你将不得不以艰难的方式找到同样的问题

我该怎么写呢 请注意,这是未经测试的

#include <iostream>
#include <cctype>

int main(){
    const int MIN = 0;  // avoid magic numbers
    const int MAX = 10;
    do {
        int secret = 4; // Chosen by a fair dice roll
                        // Guaranteed to be random ;)
                        // TODO: pseudo-random number generation
        int guess;

        while(true){        // this could've been done with a do..while,
                            // but it ended up increasing the complexity
            std::cout << "Enter a number from " << MIN
                      << " to " << MAX << std::endl;
            std::cin  >> guess;

            if(guess >= MIN && guess <= MAX){
                break;
            }

            std::cout << "The provided number is not in range, try again!\n";
        }

        if(secret == guess){
            std::cout << "Congratulations!";
        }else{
            std::cout << "Sorry, good luck next time!";
        }

        char choice;
        do {
            std::cout << " Play again? [Y/N] ";
            std::cin  >> choice;
            choice = std::tolower(choice); // convert uppercase to lowercase, don't touch the rest
            if(choice == 'n'){
                return;
            }else if(choice != 'y'){
                std::cout << "Y or N please."
            }
        }while(choice != 'y');
    }
}

Analyse this program. If you don't understand something, ask! I hope I made it clear.
#包括
#包括
int main(){
const int MIN=0;//避免使用幻数
常数int MAX=10;
做{
int secret=4;//由公平掷骰选择
//保证是随机的;)
//TODO:伪随机数生成
智力猜测;
while(true){//这本可以用do来完成的..while,
//但它最终增加了复杂性

std::cout
choice=='Y','Y'
应该是
choice='Y'| choice='Y'
打字:
“paeuse”
无法访问的代码:
如果(字母!=6)
而(数字=true)
简单的
=
是可疑的,
=
将是UB,因为
数字
没有初始化,但类型不匹配(即使转换可以应用)。我会创建子函数。
#include <iostream>
#include <cctype>

int main(){
    const int MIN = 0;  // avoid magic numbers
    const int MAX = 10;
    do {
        int secret = 4; // Chosen by a fair dice roll
                        // Guaranteed to be random ;)
                        // TODO: pseudo-random number generation
        int guess;

        while(true){        // this could've been done with a do..while,
                            // but it ended up increasing the complexity
            std::cout << "Enter a number from " << MIN
                      << " to " << MAX << std::endl;
            std::cin  >> guess;

            if(guess >= MIN && guess <= MAX){
                break;
            }

            std::cout << "The provided number is not in range, try again!\n";
        }

        if(secret == guess){
            std::cout << "Congratulations!";
        }else{
            std::cout << "Sorry, good luck next time!";
        }

        char choice;
        do {
            std::cout << " Play again? [Y/N] ";
            std::cin  >> choice;
            choice = std::tolower(choice); // convert uppercase to lowercase, don't touch the rest
            if(choice == 'n'){
                return;
            }else if(choice != 'y'){
                std::cout << "Y or N please."
            }
        }while(choice != 'y');
    }
}

Analyse this program. If you don't understand something, ask! I hope I made it clear.