如何使用是/否提示进行重复? [英] How to make reiterations using yes/no prompt?

查看:60
本文介绍了如何使用是/否提示进行重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到我的数据溢出存在问题,但是我主要关心的是尝试在最后重新运行程序以重新开始.我已经通过该网站浏览了多个示例,但找不到真正适合我需要的示例.

I realize a there is an issue with my data overflowing, but my main concern is trying to re run the program at the end to start all over. I've looked through multiple examples through this website, but couldn't really find one that fit my need.

我不确定您是否可以看到我的代码的第一部分,但是我本质上是试图使用别人在我的程序中做示例,但我只是想不通.

I am not sure if you can see the first part of my code, but I essentially tried to use someones do while example for my program but i just can't figure it out.

如果有人可以提出任何建议,我将不胜感激!

If anyone could give any suggestions I would greatly appreciate it!

我敢肯定,如果我坚持下去,最终我会弄清楚的,但是我认为这对本网站将是个好问题.

I'm sure if i keep at it i'll figure it out eventually, but i thought this would be a good question for this website.

这是我的源代码:

#include <stdio.h>

int main (void) {
 int days;/* user will input number of days light will travel*/
 int answer;
 char buffer[256];

 printf(" \n" );
 printf("\t**-**-**-**Welcome to the LIGHT RAY!**-**-**-**\n");
 printf(" \n" );
 printf("\tTo get an idea of how unbelieveably fast light is!\n");
 printf("\t come climb aboard the LIGHT RAY!\n", );
 do
 {
   printf(" \n" );
   printf(" \n");
   printf("\tHow many days would you like to travel?\n");
   scanf("%d", &days);

   printf("processing...\n" ) /* fictional terminal computing information*/;
   sleep(2);
   printf("Initializing warp drive...\n" );
   sleep(1);

   printf("3\n" ) /* count down sequence*/;
   sleep(1);
   printf("2\n" );
   sleep(1);
   printf("1\n" );
   sleep(1);
   printf("SHROOOOM!\n" );
   sleep(1);

   int day_time=days * 86400/*86,400 seconds is equal to 1 day*/;
   int distance=day_time*186000/*light travels 186,000 miles per second!*/;



   printf("Congratulations, you have traveled %lld miles! \n",distance);
   printf("Would you like another go?(yes, no)\n" );
   scanf("%s\n", buffer );
 }while (strcmp(buffer, "yes") !=0);

 getchar();

 return 0;

}

推荐答案

感谢大家的投入,您的反馈意见是无价之宝!如果有需要的话,我可能会回过头来

Thank you all for your input your feedback is truly invaluable! I will likely refer back to this one day if i may need it

如果您有兴趣的话,这是我的最终产品,即使我可能会继续摆弄它:

If any of you are interested here's my final product, even though I may continue to fiddle with it:

 #include <stdio.h>

int main (void) {
  int days;/* user will input number of days light will travel*/
  int validate=0;
  char menu_choice;/* choices... choices*/

  printf(" \n" );
  printf("\t**-**-**-**Welcome to the LIGHT RAY!**-**-**-**\n");/*carny introduction */
  printf(" \n" );
  printf("\tCome one come all \n" );
  printf("\tget an idea of how unbelieveably fast light is!\n");
  do{/*loop for return trip*/
      printf("\tTake a trip on the LIGHT RAY?(y/n):\t" );
      if(scanf(" %c", &menu_choice ) == 1){
          if((menu_choice=='y') || (menu_choice=='Y')){

              printf("\tAhh... Good choice!\n\n");/*responce and input*/
              printf(" \n" );
              printf(" \n");
              printf("\tHow many days would you like to travel?\t");
              scanf("%d", &days);

              printf("processing...\n" ) /* fictional terminal computing information*/;
              sleep(2);
              printf("Initializing warp drive...\n" );
              sleep(1);

              printf("3\n" ) /* count down sequence*/;
              sleep(1);
              printf("2\n" );
              sleep(1);
              printf("1\n" );
              sleep(1);
              printf("SHROOOOM!\n\n" );
              sleep(1);

              long long day_time=days * 86400/*86,400 seconds is equal to 1 day*/;
              long long distance=day_time*186000/*light travels 186,000 miles per second!*/;


              printf("Congratulations, you managed not to get trapped in the space time continuum and manage to travel %lld miles! \n\n",distance);



              validate = 1;
          }else if((menu_choice=='n') || (menu_choice=='N')){
              printf("\n" );
              printf("\tStep aside you're holding up the line!!!\n\n\n");
              validate = 2;
          }else{
              printf("\n" );
              printf("\tWHAT did you just call my mother!.\n\n\n");
              validate = 0;
          }
      }
  }while( validate == 0 || validate == 1);





  getchar();

  return 0;
}

最后一个想法,是否可以在第一次迭代后更改操作语句 例如:

One last thought, would it be possible to change the action statement after the first iteration for example:

您想开始穿越LIGHT RAY的旅程吗?

Do you wish to embark on a trip through the LIGHT RAY?

. .

恭喜...

您想再去一趟吗? /重复循环/

Do you wish to take another trip? /repeat loop/

当把"yes"之类的字符推算为days_travel时,我也得到了一些奇怪的互动.

I also got some strange interactions when a character like "yes" is imputed for days_travel.

我正在考虑一个if then语句,但是我不太确定如何构造它?

How would i cover for that, I'm thinking of a if then statement, but im not too sure how to structure it?

这篇关于如何使用是/否提示进行重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆