本文介绍了使用for循环来跟踪播放的轮次并将用户选择与计算机进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 背景:计算机角色在类中定义。所有输入/输出必须在主功能内部进行。另外,我为标记c ++ 98和14道歉。我不知道这是哪个版本。 < pre lang = c ++> #include< string> #include< iostream> using namespace std; int ROUNDS; int USER_CHOICE; int SCORE; class ComputersChoice {//包含AI播放器随机数函数的类 private: int randNum = rand()%5 + 1; public: void SetNum(int value){//由于某些原因,程序在没有此参数的情况下运行缓慢,并将其设置为等于randNum randNum = value; } int GetNum(){ return randNum; } void printResult(){ if(randNum == 1){ cout<<计算机选择摇滚。; } else if(randNum == 2){ cout<<计算机选择了纸张。; } 否则如果(randNum == 3){ cout<<计算机选择了剪刀。; } else if(randNum == 4){ cout<<计算机选择了蜥蜴。; } else { cout<<计算机选择了spock。; } } }; int main(){ //用户想玩多少轮? cout<<你想玩3轮,5轮还是7轮? << ENDL; cin>> ROUNDS; 开关(ROUNDS){案例3: cout<<你选择最好的3轮。<< endl ; 休息; 案例5: cout<<你选择了最好的5轮。<< endl; 休息; 案例7: cout<<你选择了最好的7轮。<< endl; 休息; 默认值: cout<<输入无效。输入3,5或7。<< endl; 休息; } //用户输入 cout<<输入1表示摇滚,2表示纸张,3表示剪刀,4表示蜥蜴或者5表示spock。<< endl; cin>> USER_CHOICE; 开关(USER_CHOICE){案例1: cout<<你选择了摇滚乐。 << ENDL ;; 休息; 案例2: cout<<你选择了纸张。 << ENDL; 休息; 案例3: cout<<你选择了剪刀。 << ENDL; 休息; 案例4: cout<<你选择了蜥蜴。 << ENDL; 休息; 案例5: cout<<你选择了spock。 << ENDL; 休息; 默认值: cout<<无效输入。输入1表示摇滚,2表示纸张,3表示剪刀,4表示蜥蜴或5表示spock。 << ENDL; 休息; } srand(time(NULL)); //对象创建 ComputersChoice randomNumber; //获取随机数并相应地打印结果 randomNumber.GetNum(); randomNumber.printResult(); } 我尝试了什么: 通过循环,我尝试将整个switch语句包装在循环,在switch语句的外部,在case的上方或下方和内部放置一个循环。我完全迷失了如何将计算机的随机数与用户选择进行比较。随机数是在类中生成的,我不确定如何访问随机数。解决方案 Background: The computers role is defined in the class. All input/output must take place inside the main function. Also, I apologize for tagging c++98 and 14. I don't know which version this is.<pre lang="c++">#include <string>#include <iostream>using namespace std;int ROUNDS;int USER_CHOICE;int SCORE;class ComputersChoice { //Class that contains the random number function for the AI player private: int randNum = rand() % 5 + 1; public: void SetNum(int value) { //Program runs slow for some reason without this parameter and setting it equal to randNum randNum = value; } int GetNum() { return randNum; } void printResult() { if (randNum == 1) { cout <<"The computer chose rock."; } else if (randNum == 2) { cout<<"The computer chose paper."; } else if (randNum == 3) { cout<<"The computer chose scissors."; } else if (randNum == 4) { cout<<"The computer chose lizard."; } else { cout<<"The computer chose spock."; } } }; int main() { //How many rounds does the user want to play? cout<<"Do you want to play 3, 5 or 7 rounds?" << endl; cin>>ROUNDS; switch(ROUNDS) { case 3: cout<<"You picked best of 3 rounds."<<endl; break; case 5: cout<<"You picked best of 5 rounds."<<endl; break; case 7: cout<<"You picked best of 7 rounds."<<endl; break; default: cout<<"Invalid input. Enter 3, 5 or 7."<<endl; break; } //User Input cout<<"Enter 1 for rock, 2 for paper, 3 for scissors, 4 for lizard or 5 for spock."<<endl; cin>>USER_CHOICE; switch(USER_CHOICE) { case 1: cout<<"You chose rock." << endl;; break; case 2: cout<<"You chose paper." << endl; break; case 3: cout<<"You chose scissors." << endl; break; case 4: cout<<"You chose lizard." << endl; break; case 5: cout<<"You chose spock." << endl; break; default: cout<<"Invalid input. Enter 1 for rock, 2 for paper, 3 for scissors, 4 for lizard or 5 for spock." << endl; break; } srand(time(NULL)); //Object creation ComputersChoice randomNumber; //Fetches the random number and prints results accordingly randomNumber.GetNum(); randomNumber.printResult(); }What I have tried:With the loop I have tried wrapping the entire switch statement in a loop, putting a loop on the outside of the switch statement above or below and inside the cases. I am completely lost on how to compare the random number of the computer to the user choice. The fact the random number is generated in a class, I am unsure how to access the random number. 解决方案 这篇关于使用for循环来跟踪播放的轮次并将用户选择与计算机进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 06:04