【发布时间】:2015-11-26 04:15:14
【问题描述】:
我被指示创建这个也有搜索功能的乘法表,但我在创建循环方面非常薄弱,这几乎是我第二次编码数组......最让我困惑的部分是如何准确编码搜索功能。另外,作业要求使用
intTable.GetLength(0) 和 intTable.GetLength(1)
分别向下和越过。至于循环的类型,刚才说的是“循环”,那么用 for-loops 代替 while-loops 会更好吗?
这是我几天来一直在搞乱的(可怕的)代码:
namespace CS12c
{
public partial class frmCS12c : Form
{
int [ , ] intTable = new int[9,9]; // 9 x 9 table
public frmCS12c()
{
InitializeComponent();
}
private void btnLoadArray_Click(object sender, EventArgs e)
{
int r; //row
int c; //column
int intResult; //result
string strSpace; //space
//Index references begin at zero
r = 0;
while (r < 9)
{
(r = 0; r < intTable.GetLength(0); r++);
c = 0;
while (c < 9)
{
(c = 0; c < intTable.GetLength);
if (intTable[r, c] < 10)
strSpace = " "; //two spaces
else
strSpace = " "; //one space
}
}
//Add 1 to the indexes before multiplying to build multiplication table
//Use intTable.GetLength(0) and intTable.GetLength(1) to control processing
intResult = (r + 1) * (c + 1);
intTable[r, c] = intResult;
}
txtTable.AppendText("\r\n"); //Delete this after implementation
}
}
//Modify the nested for loops used above to nested while loops
private void btnDisplayArray_Click(object sender, EventArgs e)
{
int r; //row
int c; //column
string strSpace;
txtTable.Clear(); //clear the text box
r = 0;
while (r < 9)
{
c = 0;
while (c < 9)
{
if (intTable[r, c] < 10)
strSpace = " "; //two spaces
else
strSpace = " "; //one space
txtTable.AppendText(strSpace); // insert space
txtTable.AppendText(intTable[r, c].ToString()); //insert result
c++;
}
r++;
txtTable.AppendText("\r\n"); //Move down one line
}
}
private void btnSearchArray_Click(object sender, EventArgs e)
{
int r; //row
int c; //column
int intSearchNumber;
txtTable.Clear(); //clear the text box
//Not enclosed in a try-catch; make you enter a number in textbox
intSearchNumber = int.Parse(txtSearchNumber.Text);
while (r = 0; r < intTable.GetLength(0); r++)
{
while (c = 0; c < intTable.GetLength(1); c++)
{
if (intSearchNumber == intTable[r, c])
{
txtTable.AppendText("\r\n");
//In search display all occurences that match the search numbers
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}//end of form
}//end of namespace
非常感谢您的帮助,谢谢。
【问题讨论】: