0

我只是想在这里请求你的帮助。我是 C++ 编程的新手。如何打印出 100 - 200 范围内的偶数。我试着写了一些代码,但没有成功。这是我的代码。我希望,这里有人可以帮助我。会非常感激。谢谢。

include <stdio.h>

void main()
{
    int i;
    for (i= 100; i<= 200; i += 2){
        print i;
    }
}
4

4 回答 4

4

嗯,很简单:

#include <iostream> // This is the C++ I/O header, has basic functions like output an input.

int main(){ // the main function is generally an int, not a void.
   for(int i = 100; i <= 200; i+=2){ // for loop to advance by 2.
       std::cout << i << std::endl; // print out the number and go to next line, std:: is a prefix used for functions in the std namespace.
   } // End for loop
   return 0; // Return int function
} // Close the int function, end of program

您使用的是 C 库,而不是 C++ 库,也没有print在 C++ 中调用的函数,也没有C. 也没有 void main 功能,请int main()改用。最后,您需要std::在名称空间中设置它们coutendl因为它们位于std名称空间中。

于 2012-08-09T08:05:25.310 回答
0

使用以下代码:

#include <iostream>

int main()
{
    int i;
    for (i= 100; i<= 200; i += 2){
        std::cout << i << std::endl;
    }
    return 0;
}
于 2012-08-09T08:17:32.423 回答
0

您的代码看起来不错....仅需要更改打印部分

  #include <stdio.h>
  int main()
  {
    for (int i= 100; i<= 200; i += 2){
      printf("%d",i);
    }
    return 0;
  }
于 2012-08-09T08:09:23.973 回答
0

这可能会有所帮助!

 #include <iostream>
using namespace std;
int main()
{
    for (int count = 100; count <= 200; count += 2)
    {
        cout << count << ", ";
    }
    cout << endl;
    return 0;

}
于 2015-03-06T00:35:21.917 回答