[英]How to properly code a For-Loop and a Do-While Loop in a multiplication table in C#?
我讨厌如此无助,我绝不要求任何人为我做家庭作业,但是出于某些奇怪的原因,我在完成这项任务时遇到了困难,而Loop总是给我带来很多麻烦。 但是我仍然对C#和OOP有点陌生。
我只是应该通过将嵌套的while循环修改为嵌套的for循环来修改某些代码(教练和我已经写了一些代码)。 并将嵌套的while循环修改为do-while循环 。 循环显然是编程中非常重要的部分,但我想我似乎学得不同,并且想知道缺少了什么...
码:
namespace CS10b
{
public partial class frmCS10b : Form
{
public frmCS10b()
{
InitializeComponent();
}
private void btnWhileLoop_Click(object sender, EventArgs e)
{
int r = 0; //row
int c = 0; //column
int intResult;
string strSpace;
txtTable.Clear(); //clear the text box
txtTable.Refresh(); //refresh the form before exiting the method
Thread.Sleep(1000); //wait one second to see the clear text box
//Outer loop goes down the rows
r = 1; //initialize r
while (r < 10)
{
//Inner loop goes across the columns
c = 1; //initialize c
while (c < 10)
{
intResult = r * c;
if (intResult < 10)
strSpace = " "; //two spaces
else
strSpace = " "; //one space
txtTable.AppendText(strSpace); // insert space
txtTable.AppendText(intResult.ToString()); //insert result
c++; //increment c
}
txtTable.AppendText("\r\n"); //Move down one line
r++; //increment r
}
}
//Modify the nested while loops used above to nested do-while loops
private void btnDoWhileLoop_Click(object sender, EventArgs e)
{
int r = 0; //row
int c = 0; //column
int intResult;
string strSpace;
txtTable.Clear(); //clear the text box
txtTable.Refresh(); //refresh the form before exiting the method
Thread.Sleep(1000); //wait one second to see the clear text box
txtTable.AppendText("Nested do-while loops to be developed"); //Delete this after implementation
//Outer loop goes down the rows
//initialize r
//do
{
//Inner loop goes across the columns
//initialize c
//do
{
intResult = r * c;
if (intResult < 10)
strSpace = " "; //two spaces
else
strSpace = " "; //one space
txtTable.AppendText(strSpace); // insert space
txtTable.AppendText(intResult.ToString()); //insert result
//increment c
} //while (c < 10);
txtTable.AppendText("\r\n"); //Move down one line
//increment r
} //while (r < 10);
}
//Modify the nested while loops used above to nested for loops
private void btnForLoop_Click(object sender, EventArgs e)
{
int r = 0; //row
int c = 0; //column
int intResult;
string strSpace;
txtTable.Clear(); //clear the text box
txtTable.Refresh(); //refresh the form before exiting the method
Thread.Sleep(1000); //wait one second to see the clear text box
txtTable.AppendText("Nested do-while loops to be developed"); //Delete this after implementation
//Outer loop goes down the rows
//for (initialize r; Boolean Condition, increment r)
{
//Inner loop goes across the columns
//for (initialize c; Boolean Condition, increment c)
{
intResult = r * c;
if (intResult < 10)
strSpace = " "; //two spaces
else
strSpace = " "; //one space
txtTable.AppendText(strSpace); // insert space
txtTable.AppendText(intResult.ToString()); //insert result
}
txtTable.AppendText("\r\n"); //Move down one line
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}//end of form
}//end of namespace
这应该很容易,但是我从未做过的do-while和for-loop循环。 谢谢。
您必须用for循环替换while循环吗?
前循环:
for(int r = 1; r < 10; r++)
{
for(int c = 1; c < 10; c++)
{
intResult = r * c;
if (intResult < 10)
strSpace = " "; //two spaces
else
strSpace = " "; //one space
txtTable.AppendText(strSpace); // insert space
txtTable.AppendText(intResult.ToString()); //insert result
}
txtTable.AppendText("\r\n"); //Move down one line
}
Do-While-Loop :(对此不确定)
int r = 1;
do
{
int c = 1;
do
{
//Your code
} while c < 10
} while (r < 10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.