我讨厌如此无助,我决不会让任何人为我做作业,但由于一些奇怪的原因,我很难完成这项任务,而且总的来说循环总是给了我很多麻烦。但我在C#和OOP上仍然有点新鲜。
我只是想通过将嵌套的while循环修改为嵌套的 for-loops 来修改一些代码(教师和我已经写过一些代码)。并将嵌套的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循环。谢谢。
答案 0 :(得分:2)
你必须用for循环替换while循环吗?
For-Loop:
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);