如何使用递归循环代码(当用户输入无效输入时,它将再次提示他们)?

丹尼尔AA9

因此,我以C ++格式编写了这段代码,并试图循环代码,提示用户输入无效输入时(提示输入1到10之间的数字),它不仅会终止输入是否无效,还会要求用户输入输入有效输入,直到输入正确的有效输入为止。C ++的新手,非常感谢您的帮助

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>

using namespace std;

//constants go here
const int MIN = 1;
const int MAX = 10;

const char YES = 'Y';
const char NO = 'N';

//Booleans go here
char wantToPlay();
bool getIntValue(int &userValue);
bool play(int userValue);

//Main routine
int main(){

printf("Welcome, this program will guess a number between 1 and 10");
printf(", if the program guesses the number correctly you'll get VICTORY ROYALE");
printf(", if you don't get it right you'll get BETTER LUCK NEXT TIME, Good luck :)\n");

char playOrNot = wantToPlay();

  //If user value is TRUE, program outputs "Victory Royale" and terminates
  //If user value is FALSE, program outputs "Better luck next time" and terminates
  //If user value is NOT VALID, program outputs "Not a good number" and prompts user again
  //If user enters "N" when prompted to answer Y or N
  int input = -1;
  switch (playOrNot){
  case YES:
    if(getIntValue(input)){
      srand (time(NULL));
      if(play(input)){
    cout << "Victory Royale!"<<endl;
    }else{
    cout<<"Better Luck Next Time!"<<endl;
    }
    }else{
      cout<<"not a good number\n";
    }
    break;
  case NO:
    cout << "sorry you hate my game\n";
    break;

  default:
    cout << "that was not valid\n";
    //If user enters value that is completely not valid
  }


  return 0;
}
char wantToPlay(){
  //Prompt user to enter Y for yes and N for no
  char answer = NO;
  cout << "Do you Want to Play? 'y'for" << " yes, 'n' for no" << endl;
  cin >> answer;
  answer = toupper(answer);
  return answer;
}


bool getIntValue(int &userValue){
  //Prompt user to enter a number between the Min(1) and Max(10)
  bool valid = false;
  cout <<"Enter a number between " << MIN << " and " << MAX <<endl;
  cin>>userValue;
  if(userValue >= MIN && userValue <= MAX){
    valid = true;
  }


  return valid;
}
bool play(int userValue){
  //Random tool to give a random between 1 and 10
  bool match = false;
  int random = (rand()%10)+1;
  if(userValue==random){
    match=true;
  }
  return match;
}
帮助豆
#include<iostream>
using namespace std;
int main()
{
    char s;
    cout<<"Enter valid number between 1 to 10"<<endl;
    cin>>s;
    while(!((s>='1') && (s<='10')))
    {
        cout<<"The number you have entered is not in range 1 - 10"<<endl;
        cout<<"Enter an valid number I.e. between 1 to 10"<<endl;
        cin>>s;
    }
    cout<<"You have successfully entered an valid number"<<endl;
    return;
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

重置开关的大小写,以便他们可以再次进入菜单输入Java

我试图让我的开关盒再次循环,如果他们让用户获得默认值

用户使用循环输入无效

用户输入错误的输入时如何循环代码

当用户输入输入时,如何在for循环外打印输出

autocomplete = new-password,让他们通过再次输入密码来确认

我如何设计一个循环,以接受用户输入的高尔夫球手姓名,然后他们的分数显示列表?

如何正确提示用户输入,直到他们给出有效的响应

阻止用户输入数字,但保留他们已经输入的字母字符

如何告诉用户他们输入了旧密码?

如何提示用户,直到他们输入有效的整数?

初学者java,尝试使用户输入AB或C,这取决于他们选择的内容,它将执行不同的操作

在下面的Java代码中,我如何要求用户在每个咖啡杯中输入他们想要的镜头数量(鉴于他们订购的杯数大于1)?

当用户在C ++上键入时,如何检查用户的输入?

如何提示用户输入一个数字,直到他们选择一个奇数?C ++

如何使用try-catch验证用户的输入,并在输入无效时再次读入他们的输入?

如果无效,如何再次提示用户输入正确的ID,并在输入正确的ID时停止循环

当用户输入无效输入时如何终止程序?

写一个python函数。要求用户输入人名和他们的年龄,它将放入一个空字典并返回

当用户输入字符或字符串时,如何让代码显示“无效输入”(验证)

如果我的 scanf 变量是一个浮点数并且用户输入了一个字符,我如何提示他们输入一个数字?假设 scanf 在 do while 循环中

如果用户输入无效,如何再次提示?

如何在 Java 中创建自动更正,让我在用户输入时替换他们?

如何限制用户在处理中输入他们的名字时只能使用字母?

当用户单击输入时如何聚焦输入?

当用户使用 AngularJS 输入输入时,如何使标签不可见?

如何提示用户再次尝试输入

如何使用 Kotlin 在他们的设备上保存用户输入?

当用户专注于输入时如何推荐文本