scanf()函數被跳過不啟動並執行原因__random

來源:互聯網
上載者:User

       當你進行過了scanf函數後, 在第二次scanf函數時, 由於緩衝區裡依然存著第一次結束時送入的斷行符號符,在第二次時沒有讀入就直接把斷行符號符送入了,所以沒有真正起到讀入資料的作用.只要在合適的地方加入fflush(stdin)清空緩衝區,再運行scanf時就可以了.

#include<math.h>
#include <stdlib.h>
#include<stdio.h>


char judge(int try,int answer){

   if(try==answer){

      return 'e';
   }
   else return 'u';

 

 

void main(){

   int order=0,get=1;
   int try=0;
   int level;
   int bits;
   int answer;
   char result='n';
   int length;
   char judgeIf;

   printf("Game begins!/nWelcome to the number guessing game!/nThis is my first biggest program./nHope you like it./nIt is edited by c-resource./n");
   printf("Now it begins./n");

   do{

      printf("Choose the level 3-10 :");
      scanf("%d",&level);

      printf("Set the length of the number 3-5 bits :");
      scanf("%d",&bits);
  
      answer=random(bits);

      printf("Now the number is ready.You can try it now./nInput your try :");

      while(order<level){
         printf("/nTime %d : (%d)",++order,answer);
         scanf("%d",&try);

 

         if(judge(try,answer)=='e'){

            printf("/nYou are right.Congratulations!");
            break;
           
         }
         else {
            if(order==level){

               printf("You are failed.");
            }
            else printf("Try it again.");
         }
      }
  

  
      if(get==1){
         printf("Are you want to play it again?");
         scanf("%c",&result);
         printf("/nYou are so clever now with the help of my software.");
      }

   }
   while(result=='y');
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.