[英]Nested For loop to Do while Loop
Hi I read the guidelines about homework questions and it says to clearly state that it is homework. 嗨,我阅读了有关家庭作业问题的指导原则,并明确表示这是家庭作业。 This is homework, I have spent the last 45 minutes trying over and over again. 这是家庭作业,我花了最后45分钟一遍又一遍地尝试。 I've hit a wall and need help. 我撞墙了,需要帮助。
My assignment was to take this code that came from a double For loop and convert it into a while loop nested into a for loop. 我的任务是获取来自双For循环的代码并将其转换为嵌套到for循环中的while循环。 I have successfully completed that. 我已经成功完成了。 However, the 3rd part is to take that code and make the outer for loop into a do while loop. 但是,第三部分是采用该代码并将外部for循环变为do while循环。 The output needs to increment a "#" each line like so if the input was "4" 如果输入为“4”,输出需要增加“#”每行
#
##
###
####
Below is my code that I wrote that I need to make the outer for loop into a do while loop: 下面是我写的代码,我需要将外部for循环变为do while循环:
int main()
{
int side;
cout << "Enter a number: ";
cin >> side;
for (int i = 0; i < side; i++)
{
int j = i;
while(j >= 0)
{
cout << "#";
j--;
}
cout << "\n";
}
}
This is my attempt so far: 这是我到目前为止的尝试:
int main()
{
int side;
int i;
cout << "Enter a number: ";
cin >> side;
int j=side;
do
{
while(j >= 0)
{
cout << "#";
j--;
}
cout << "\n";
i++;
}
while(j >= side);
}
My teacher said as long as the code is explained and I understand how it works that it's okay. 我的老师说,只要代码被解释,我理解它是如何工作的,这没关系。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks. 谢谢。
I suggest 我建议
int main()
{
int side;
int i = 0;
cout << "Enter a number: ";
cin >> side;
do
{
int j = i;
while(j >= 0)
{
cout << "#";
j--;
}
cout << "\n";
i++;
}while(i < side)
}
A for loop usually consists in an initialization ( i=0
), a stop condition ( i < side
) and an increment ( i++
); for循环通常包括初始化( i=0
),停止条件( i < side
)和增量( i++
); why would you not use i
anymore? 你为什么不再使用i
了?
The first mistake you made is this: 你犯的第一个错误就是:
int i; //not initialized!
/*...*/
i++;
and you didn't even use it in your do-while condition. 而且你甚至没有在你的do-while条件下使用它。
So while(j >= side);
所以while(j >= side);
> while (i >= side);
> while (i >= side);
Actually, that's not true, either. 实际上,这也不是真的。 Since side is the input, you want i
to check if it's smaller not greater then the input. 因为一边是输入,你要i
检查,如果它的体积更小不大于输入。 So it's while (i < side);
所以它是while (i < side);
Another thing is int j=side;
另一件事是int j=side;
, when you decrement j
it will never reset, so you must set this into your do-while loop and also initialize it with i
rather than side.... ,当你递减j
它永远不会重置,所以你必须将它设置为你的do-while循环,并用i
而不是侧面初始化....
Anyway, here's the full code: 无论如何,这是完整的代码:
#include <iostream>
using namespace std;
int main()
{
int side;
int i = 0;
cout << "Enter a number: ";
cin >> side;
do
{
int j = i;
while (j >= 0)
{
cout << "#";
j--;
}
cout << "\n";
i++;
} while (i < side);
return 0;
}
example output: 示例输出:
Enter a number: 10
#
##
###
####
#####
######
#######
########
#########
##########
while(j >= side);
should be while(i <= side);
应该是while(i <= side);
j
should be initialized in each iteration of the outer loop ( j = i;
) j
应该在外循环的每次迭代中初始化( j = i;
) int j=side;
is unnecessary. 没必要。 An friendly suggestion - name your variables and functions descriptively - row
and column
are much better than i
and j
. 友好的建议 - 描述性地命名您的变量和函数 - row
和column
比i
和j
好得多。
They answer above solves your problem but let me show you a shorter way to do things: 他们上面的回答解决了你的问题,但让我告诉你一个更短的做事方式:
int main()
{
int side;
std::cout << "Enter a number: ";
std::cin >> side;
int row = 1;
do
{
int col = 0;
while (col++ != row)
{
std::cout << "#";
}
std::cout << "\n";
} while (row++ != side); //This way, the condition is checked (row != side), and then row is incremented
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.