C++计算器While循环不循环

2w2cym1i  于 2023-01-22  发布在  其他
关注(0)|答案(2)|浏览(134)

我已经放在一起猜测结果计算器,用户得到提示输入他们的猜测随机生成(乘法,加法或减法)操作。一切工作,直到我把while循环的情况下,如果用户没有输入正确的猜测或猜测结果正确,但想再试一次。没有while循环一切工作,直到他们选择Y为是'继续或'否'退出后,程序显然会被终止,无论他们选择'Y'对'N'。与while循环的Xcode报告“建立成功”,它的结束没有输出在所有。有人能告诉我哪里是错误的,或者是Xcode在耍我吗?Thx GC

#include <iostream>

#include <ctime>

#include <string>

using namespace std;int main (){
srand((int) time(0));
bool end{ false };
while (!end){}{
    
    int RanNumber1 = rand()%200;
    int RanNumber2 = rand()%200;
    int mult = RanNumber1 * RanNumber2;
    int add = RanNumber1 + RanNumber2;
    int sub = RanNumber1 - RanNumber2 ;
    
    
    
    cout << " What's the result of " << endl;
    
    
    //
    int rand_num = int((std::rand() % 3));
    
    switch (rand_num) {
        case 0:
            std::cout << RanNumber1 << " * " <<  RanNumber2 << " = " << std::endl;
            break;
        case 1:
            std::cout << RanNumber1 << " + "  <<  RanNumber2 <<  " = " <<  std::endl;
            break;
        case 2:
            std::cout <<RanNumber1 << " / " << RanNumber2 <<  " = " <<std::endl;
            break;
            
    }
    
    
    
    int guess;
    
    cin >> guess ;
    
    
    if  ( guess ==  mult  || guess ==  add  || guess ==  sub )
        
    {
        
        cout << " correct " << endl;
      
        
    }
    
    else if ( guess != mult || guess !=  add  || guess !=  sub )
        
    {
        
        cout << " not right ! " << endl;
     
     
    }
    
    cout << "Do you want me to try again ? (Y | N) : "<< endl;
    
    char go_on;
    cin >> go_on;
    end = ((go_on == 'Y') || (go_on == 'y')) ?  false : true;
    
   
}
       return 0;
    
    }
====================================================================================

Here is now working one with While Loop on

====================================================================================

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

int main (){
    
    srand((int) time(0));
    bool end{ false };
    
    
    
    while (!end){}{
        
        int RanNumber1 = rand()%200;
        int RanNumber2 = rand()%200;
        int mult = RanNumber1 * RanNumber2;
        int add = RanNumber1 + RanNumber2;
        int sub = RanNumber1 - RanNumber2 ;
        
        
        
        cout << " What's the result of " << endl;
        
        
        //
        int rand_num = int((std::rand() % 3));
        
        switch (rand_num) {
            case 0:
                std::cout << RanNumber1 << " * " <<  RanNumber2 << " = " << std::endl;
                break;
            case 1:
                std::cout << RanNumber1 << " + "  <<  RanNumber2 <<  " = " <<  std::endl;
                break;
            case 2:
                std::cout <<RanNumber1 << " / " << RanNumber2 <<  " = " <<std::endl;
                break;
                
        }
        
        
        int guess;
        
        cin >> guess ;
        
        
        if  ( guess ==  mult  || guess ==  add  || guess ==  sub )
            
        {
            
            cout << " correct " << endl;
            //            cout << "Do you want me to try again ? (Y | N) : "<<endl;
            
        }
        
        else if ( guess != mult || guess !=  add  || guess !=  sub )
            
        {
            
            cout << " not right ! " << endl;
            //                                cout << "Do you want me to try again ? (Y | N) : "<<endl;
            
            
        }
        
        cout << "Do you want me to try again ? (Y | N) : "<< endl;
        
        
        char go_on;
        cin >> go_on;
        end = ((go_on == 'Y') || (go_on == 'y')) ?  false : true;
        
        
        
    }
            
            
            
            
            return 0;
            
            
        }
vwkv1x7d

vwkv1x7d1#

你把While语句放错了,你应该把代码 Package 在while循环中。

#include <iostream>
#include <ctime>
#include <string>

int main () {
    
srand((int) time(0));

bool end = true;
    
    int RanNumber1 = std::rand()%200;
    int RanNumber2 = std::rand()%200;
    int mult = RanNumber1 * RanNumber2;
    int add = RanNumber1 + RanNumber2;
    int sub = RanNumber1 - RanNumber2 ;
    
    while (end) {
    
    std::cout << " What's the result of " << std::endl;
    
    int rand_num = int((std::rand() % 3));
    
    switch (rand_num) {
        case 0:
            std::cout << RanNumber1 << " * " <<  RanNumber2 << " = " << std::endl;
            break;
        case 1:
            std::cout << RanNumber1 << " + "  <<  RanNumber2 <<  " = " <<  std::endl;
            break;
        case 2:
            std::cout <<RanNumber1 << " / " << RanNumber2 <<  " = " <<std::endl;
            break;
            
    }
    
    int guess;
    std::string message;
    
    std::cin >> guess ;
    
    message = ( guess ==  mult  || guess ==  add  || guess ==  sub ) ? " correct " : " not right ! ";
    
    std::cout << message << std::endl;
      
    std::cout << "Do you want me to try again ? (Y | N) : "<< std::endl;
    
    char go_on;
    std::cin >> go_on;
    end = ((go_on == 'Y') || (go_on == 'y')) ?  true : false;
    }
    
    
    return 0;
}
q3aa0525

q3aa05252#

实际上,它是这样工作的。再试一次,它工作了。Xcode是用!!!

相关问题