鱼C论坛

 找回密码
 立即注册
查看: 1673|回复: 2

提示int temp = num 那一行 syntax error : missing ';' before 'type'

[复制链接]
发表于 2023-10-25 23:02:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>

  2. int main()
  3. {
  4.     int num, count, temp = 0;

  5.     printf("请输入一个不多于5位的正整数:");
  6.     scanf("%d", &num);

  7.         int temp = num;

  8.     while (temp > 0)
  9.         {
  10.         temp /= 10;
  11.         count++;
  12.     }
  13.     printf("它是%d位数\n", count);
  14.     printf("每一位数字分别为:");

  15.     while (num > 0)
  16.         {
  17.         int digit = num % 10;
  18.         printf("%d", digit);
  19.         num /= 10;
  20.     }
  21.     printf("\n");
  22.     printf("逆序输出各位数字为:");

  23.     while (count > 0)
  24.         {
  25.         int digit = num % 10;
  26.         printf("%d", digit);
  27.         num /= 10;
  28.         count--;
  29.     }
  30.     printf("\n");

  31.     return 0;
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-25 23:02:22 | 显示全部楼层
在这段代码中,出现了一个语法错误。错误发生在 int temp = num; 这一行,错误信息为"missing ';' before 'type'",即在'type'之前缺少分号。

这个错误是因为在声明变量 temp 时,已经在 int num, count, temp = 0; 这一行中声明了 temp 变量,所以在后续的代码中再次声明 int temp = num; 是错误的。

要解决这个问题,只需将 int temp = num; 这一行修改为 temp = num; 即可。这样就不会再次声明 temp 变量,而是将 num 的值赋给已经声明的 temp 变量。

修改后的代码如下:

  1. #include <stdio.h>

  2. int main()
  3. {
  4.     int num, count, temp = 0;

  5.     printf("请输入一个不多于5位的正整数:");
  6.     scanf("%d", &num);

  7.     temp = num;

  8.     while (temp > 0)
  9.     {
  10.         temp /= 10;
  11.         count++;
  12.     }
  13.     printf("它是%d位数\n", count);
  14.     printf("每一位数字分别为:");

  15.     while (num > 0)
  16.     {
  17.         int digit = num % 10;
  18.         printf("%d", digit);
  19.         num /= 10;
  20.     }
  21.     printf("\n");
  22.     printf("逆序输出各位数字为:");

  23.     while (count > 0)
  24.     {
  25.         int digit = num % 10;
  26.         printf("%d", digit);
  27.         num /= 10;
  28.         count--;
  29.     }
  30.     printf("\n");

  31.     return 0;
  32. }
复制代码


这样修改后的代码就不会再出现语法错误了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-25 23:02:37 | 显示全部楼层
在这段代码中,出现了两个问题:

1. 在 int num, count, temp = 0; 这行中,缺少一个分号。应该改为 int num, count, temp = 0;

2. 在 int temp = num; 这行中,重复定义了变量 temp。前面已经定义了 temp,所以这里不需要再次定义。可以直接赋值,像这样 temp = num;

修正后的代码如下:

  1. #include <stdio.h>

  2. int main() {
  3.     int num, count, temp = 0;
  4.    
  5.     printf("请输入一个不多于5位的正整数:");
  6.     scanf("%d", &num);
  7.    
  8.     temp = num;

  9.     while (temp > 0) {
  10.         temp /= 10;
  11.         count++;
  12.     }
  13.    
  14.     printf("它是%d位数\n", count);
  15.     printf("每一位数字分别为:");
  16.    
  17.     while (num > 0) {
  18.         int digit = num % 10;
  19.         printf("%d", digit);
  20.         num /= 10;
  21.     }
  22.    
  23.     printf("\n");
  24.     printf("逆序输出各位数字为:");
  25.    
  26.     while (count > 0) {
  27.         int digit = num % 10;
  28.         printf("%d", digit);
  29.         num /= 10;
  30.         count--;
  31.     }
  32.    
  33.     printf("\n");
  34.    
  35.     return 0;
  36. }
复制代码


请注意,修正后的代码也需要注意其他可能存在的逻辑错误或潜在问题。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 00:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表