Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;初学者编程-循环和while语句_C++_Loops_While Loop - Fatal编程技术网

C++ C++;初学者编程-循环和while语句

C++ C++;初学者编程-循环和while语句,c++,loops,while-loop,C++,Loops,While Loop,上面的流程图表示在屏幕上显示数字20、40、60、80、100、120、140、160、180的算法。使用while语句将算法编码到程序中。计数器变量应该是名为count的int变量。保存并运行程序。测试程序以查看是否获得正确的输出。当程序正确运行时,请更正所有错误。将IPO图表和程序代码复制并粘贴到word文档中 我刚开始C++,我不知道如何使用while语句和编写代码。我们将不胜感激。到目前为止,我的代码如下: #include <iostream> using namespac

上面的流程图表示在屏幕上显示数字20、40、60、80、100、120、140、160、180的算法。使用while语句将算法编码到程序中。计数器变量应该是名为count的int变量。保存并运行程序。测试程序以查看是否获得正确的输出。当程序正确运行时,请更正所有错误。将IPO图表和程序代码复制并粘贴到word文档中

<>我刚开始C++,我不知道如何使用while语句和编写代码。我们将不胜感激。到目前为止,我的代码如下:

#include <iostream>
using namespace std;

int main ()
{
  int count;
  count = 10

;  while (count < 200) {
    cout << count << ", ";
    count*=2;
  }

  system("pause");
  return 0;
}
#包括
使用名称空间std;
int main()
{
整数计数;
计数=10
;而(计数<200){

cout要向一个变量添加10,您可以使用:

count = count + 10;
或简称:

count += 10;
要输出一个乘以2的数字,只需使用:

cout << (count * 2);
然后确保在结尾处写下新行:

cout << '\n';
对上面的注释以及示例程序的分析应该足以让您根据自己的规范构建一个类似的程序

当然,如果不是因为强加在您身上的任意限制,通常有更好的方法来做事情。您的整个程序可以写成:

#include <iostream>

int main (void) {
    std::cout << 20;
    for (int num = 40; num < 200; num += 20)
        std::cout << ", " << num;
    std::cout << '\n';
    return 0;
}
#包括
内部主(空){

std::cout根据我对流程图的理解,应该只显示带有2的产品。因此

count*=2;
不正确,因为它更改了计数。您想添加10,因此将其替换为
count+=10;

在上面,您正在输出count,但您希望输出带有两个参数的产品:

cout << (2*count) << ", ";

cout您对流程图的解释是错误的。您将计数器乘以2,并且
count*=2
,您不应该这样做,流程图上说,显示计数器乘以2意味着不同的事情

while (count < 200) {
  cout << count * 2 << ", ";
  count += 10;
}
while(计数<200){
你应该:

  • cout
    ing时进行乘法,这样乘法就不会影响
    count
    的实际值
  • cout
    ing之后添加10,以便下一次迭代将在序列中输出一个新值
  • 在计数<100
  • 时继续迭代 像这样:

    while (count < 100) {
        cout << count*2 << ", ";
        count+=10;
    }
    
    while(计数<100){
    cout使用加号(+)添加一个特定值,并用=;或组合+=

    要缩短代码,它可能会如下所示:

    int main(int argc, char* argv[])
    {
    int count = 10;
    while( (count+=10) < 200 )
        cout << (count*2) << ", ";
    
    system("pause");
    return 0;
    }
    
    intmain(intargc,char*argv[])
    {
    整数计数=10;
    而((计数+=10)<200)
    
    无法将10添加到计数器:count+=10;您能澄清您试图实现的目标吗?将10添加到计数器似乎与您试图显示的数字列表没有任何关系,这些数字的差值为20,并且不是以前数字的两倍。看起来像弃用的家庭作业标记;-)
    系统(“暂停”)
    。请在你还可以的时候忘记
    system()
    。好吧!我知道我做错了什么,但是因为count乘以2,count<200。它显示的所有数字都是380。我想我必须在180处停止它。
    cout << (2*count) << ", ";
    
    while (count < 200) {
      cout << count * 2 << ", ";
      count += 10;
    }
    
    while (count < 100) {
        cout << count*2 << ", ";
        count+=10;
    }
    
    int main(int argc, char* argv[])
    {
    int count = 10;
    while( (count+=10) < 200 )
        cout << (count*2) << ", ";
    
    system("pause");
    return 0;
    }
    
    int main()
    {
    for( int count = 10; count < 200; count+=10)
        cout << (count*2) << ", ";
    
    //do stuff    
    }