liam爱米莉 发表于 2022-3-31 17:31:35

C中循环有问题

#include<stdio.h>//Include header file stdio.h
int main()//Main function main
{
        char word;//define string word
        char target;
        char number;
        int i=0;//define integer i
        int j=0;

       printf("\n------------------------\n");
       printf("WELCOME TO THE SCRAMBLER\n");
       printf("------------------------\n");
       printf("Please enter your string:");
       gets(word);//get strings
       printf("\nWord is : %s\n",word);//out put word
       printf("\nTarget :");
       scanf("%c",&target)//get the target string from the keyboard
       getchar();//make the program wait
       
while(j<100)//loop
{
        if(target=='q')//check if there are target characters
{
          
        printf("SCRAMBLED STRING IS :%s",word);
           return 0;
}
        else{       
           printf("Replace with:");
           scanf("%c",&number);       
           getchar();
           printf("\nReplacing %c with %c",target,number);
           getchar();
           i=0;
   while(word!='\0')//loop all the string
      {
            if(word==target)//find the same string
    {
           word=number;//replace target string to number
    }
        i++;//i=i+1

   }
   printf("Word is : %s\n",word);
   printf("\nTarget :");
   scanf("%c",&target);
        }
        j++;
}
return 0;
}


大家好,我在做一个可以扫描替换的程序,我发现它第一遍可以,但是当它开始第二轮时无法成功。
想了很久都不行,还请大佬们指点一下
file:///C:/Users/%E6%9D%8E%E4%BA%9A%E8%BD%A9/Desktop/QQ%E6%88%AA%E5%9B%BE20220331172722.png

ba21 发表于 2022-3-31 17:45:09

你这个B装的我给你100分。#include<stdio.h>//Include header file stdio.h
int main()//Main function main
{
      char word;//define string word
      char target;
      char number;
      int i=0;//define integer i
      int j=0;

         printf("\n------------------------\n");
         printf("WELCOME TO THE SCRAMBLER\n");
         printf("------------------------\n");
         printf("Please enter your string:");
         gets(word);//get strings
         printf("\nWord is : %s\n",word);//out put word
         printf("\nTarget :");
         scanf("%c",&target);
         while(getchar()!='\n');//make the program wait
         
while(j<100)//loop
{
      if(target=='q')//check if there are target characters
{
         
      printf("SCRAMBLED STRING IS :%s",word);
         return 0;
}
          else{      
         printf("Replace with:");
         scanf("%c",&number);      
         while(getchar()!='\n');//make the program wait
         printf("\nReplacing %c with %c\n",target,number);

         i=0;
   while(word!='\0')//loop all the string
      {
            if(word==target)//find the same string
    {
             word=number;//replace target string to number
    }
      i++;//i=i+1

   }
   printf("Word is : %s\n",word);
   printf("\nTarget :");
   scanf("%c",&target);
    while(getchar()!='\n');//make the program wait
      }
      j++;
}
return 0;
}

黎羽轩 发表于 2022-4-10 09:57:30


#include<stdio.h>//Include header file stdio.h
int main()//Main function main
{
      char word;//define string word
      char target;
      char number;
      int i=0;//define integer i
      int j=0;

         printf("\n------------------------\n");
         printf("WELCOME TO THE SCRAMBLER\n");
         printf("------------------------\n");
         printf("Please enter your string:");
         gets(word);//get strings   //这个没用到{:10_269:}
         printf("\nWord is : %s\n",word);//out put word
         printf("\nTarget :");
         scanf("%c",&target)//get the target string from the keyboard
         getchar();//make the program wait
         
while(j<100)//loop
{
      if(target=='q')//check if there are target characters
{
         
      printf("SCRAMBLED STRING IS :%s",word);
         return 0;
}
          else{      
         printf("Replace with:");
         scanf("%c",&number);      
         getchar();
         printf("\nReplacing %c with %c",target,number);
         getchar();
         i=0;
   while(word!='\0')//loop all the string
      {
            if(word==target)//find the same string
    {
             word=number;//replace target string to number
    }
      i++;//i=i+1

   }
   printf("Word is : %s\n",word);
   printf("\nTarget :");
   scanf("%c",&target);
      }
      j++;
}
return 0;
}

黎羽轩 发表于 2022-4-10 09:58:07

黎羽轩 发表于 2022-4-10 09:57


看一下第15行
页: [1]
查看完整版本: C中循环有问题