【问题标题】:Repeat the Program for again Search Array Element重复程序再次搜索数组元素
【发布时间】:2020-06-20 16:04:40
【问题描述】:

重复程序以再次搜索数组元素。

 #include <stdio.h>
#define MAX_SIZE 100  

  int main()
  {
    int arr[MAX_SIZE];
    int size, i, toSearch, found;
    char repeat;

    printf("Enter the size of an array\n");
    scanf("%d", &size);
    printf("Enter the array elements\n");
    for (i = 0; i < size; i++) 
    {
      scanf("%d", &arr[i]);
    }
do{
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
found = 0; 

 for(i=0; i<size; i++)
  {
      if(arr[i] == toSearch)
      {
          found = 1;
          break;
     }
 }
 
  if(found == 1)
  {
      printf("\n%d is found at position %d", toSearch, i + 1);
  }
 else
  {
      printf("\n%d is not found in the array \n", toSearch);
  }
  printf("\n \n \nPress Y to again Search Any Element in Array\n \nPress Any other Key to Exit the Program\n\n");
scanf(" %c \t",&repeat);
}
while(repeat == 'y' || repeat == 'Y' );
return 0;

}

当用户输入 Y || 时,我想重复我的程序y 否则它将退出程序。 在这段代码中,我想创建一个数组,然后在显示结果之后搜索元素,最后重复搜索元素块中的代码。

【问题讨论】:

    标签: c arrays repeat linear-search


    【解决方案1】:

    我运行了你的代码,除了这一行之外,一切似乎都正常工作:

    scanf(" %c \t",&repeat);
    

    从 scanf 中删除 \t ,它应该可以正常工作。您不想扫描制表符,只需扫描“Y”或“y”字符。

    另外,您对换行符的使用有点不寻常。尝试将换行符放在字符串的末尾而不是开头。

    更新代码:

    #include <stdio.h>
    #define MAX_SIZE 100  
    
    int main() {
      int arr[MAX_SIZE];
      int size, i, toSearch, found;
      char repeat = ' ';
    
      printf("Enter the size of an array\n");
      scanf("%d", &size);
      printf("Enter the array elements\n");
      for (i = 0; i < size; i++) 
        scanf("%d", &arr[i]);
        
      do{
        printf("Enter element to search: \n");
        scanf("%d", &toSearch);
        found = 0; 
    
        for(i=0; i<size; i++) {
          if(arr[i] == toSearch) {
            found = 1;
            break;
          }
        }
        
        if(found == 1)
          printf("%d is found at position %d\n", toSearch, i + 1);
        else printf("%d is not found in the array\n", toSearch);
        
        printf("Press Y to again Search Any Element in Array\nPress Any other Key to Exit the Program\n");
        scanf(" %c",&repeat);
      }
      while(repeat == 'y' || repeat == 'Y' );
      return 0;
    }
    

    【讨论】:

    • 我删除了 \t 但仍然无法正常工作。 :(
    • 你能告诉我更多吗?究竟是什么工作不正常?它是否打印了错误的输出,如果是,它做错了什么?
    • 它不是重复程序来“搜索元素”行。它只是通过获取 Y 或任何其他退出键来停止
    • 查看我的更新。我在删除制表符之前遇到了这个问题,但我相信我在上述编辑之后修复了。
    【解决方案2】:

    将你想在while循环中重复的代码块括起来,比如

    bool flag = false;
    while(flag==true) {
       //Code block
       scanf("%c",&input)
       if((input == 'y') || (input == 'Y')) {flag = true;}
       else {flag = false;}
    }
    

    【讨论】:

      【解决方案3】:

      我想到的第一个方法:

      #include <stdio.h>
      #define MAX_SIZE 100
      
      int main()
      {
          int arr[MAX_SIZE];
          int size, i, toSearch, found;
          char repeat;
      
          printf("Enter the size of an array\n");
          scanf("%d", &size);
          printf("Enter the array elements\n");
          for (i = 0; i < size; i++)
          {
              scanf("%d", &arr[i]);
          }
          do
          {
              printf("\nEnter element to search: ");
              scanf("%d", &toSearch);
              found = 0;
      
              for (i = 0; i < size; i++)
              {
                  if (arr[i] == toSearch)
                  {
                      found = 1;
                      break;
                  }
              }
      
              if (found == 1)
              {
                  printf("\n%d is found at position %d", toSearch, i + 1);
              }
              else
              {
                  printf("\n%d is not found in the array \n", toSearch);
              }
              printf("\n \n \nPress Y to again Search Any Element in Array\n \nPress Any other Key to Exit the Program\n\n");
              repeat = getchar();
              repeat = getchar();
              if(repeat == 'y' || repeat == 'Y') {
                  continue;
              }
              else {
                  break;
              }
          } while (1);
          return 0;
      }
      
      

      【讨论】:

        猜你喜欢
        • 2014-03-30
        • 2015-04-18
        • 2021-01-20
        • 1970-01-01
        • 1970-01-01
        • 2016-12-22
        • 2012-09-01
        • 1970-01-01
        • 2018-01-11
        相关资源
        最近更新 更多