2015-08-15 59 views
-1

如何通过打印奇数(1 - > 10) - while?如何打印奇数(1 - > 10) - 而

我的代码:http://codepad.org/yS6DNq8Y

#include <stdio.h> 
#include <conio.h> 
int i; 
void Dayso() 
{ 

    do 
    { 
     i = 1 
     i++; 
     if (i % 2 == 0) 
     { 
      continue; 
     } 
     printf ("\n%d",i); 

    }while (i <= 10); 

} 

int main() 
{ 
    Dayso(); 
    getch(); 
    return 0; 
} 

和输出:

Line 18: error: conio.h: No such file or directory 
In function 'Dayso': 
Line 10: error: expected ';' before 'i' 

我该如何解决这个问题?

+2

错误消息是清楚的方式:开始添加';'在行9.关于错误'18线的端部:错误:CONIO.H:没有这样的文件或目录,[这可能会帮助你] http://stackoverflow.com/questions/8792317/why-cant-i-find-conio-h-on-linux)。 –

+1

在循环体中完成的第一件事是什么(并因此完成每个循环)?为什么这是个问题)? – user2864740

+0

将'i = 1'移至外部作用域并为其添加分号。 –

回答

2

编译错误:

  1. Linux中有没有机器conio.h头文件。您可以在该程序中删除getch()功能。
  2. 您在一行缺少分号9.

的逻辑错误:

  1. 您分配上每隔1〜i变量(9号线),所以你刚才创建的无限循环,同时反复做。将循环外的赋值移至1。
  2. 您在ods中缺少1个,而在当前实现中缺少11个。

更正溶液: http://ideone.com/IB3200

#include <stdio.h> 

void Dayso() 
{ 
    int i = 1; 
    do 
    { 
     if (i % 2 != 0) { 
      printf ("\n%d",i); 
     } 

     i++; 
    } while (i <= 10); 

} 

int main() 
{ 
    Dayso(); 
    return 0; 
}