我用这个代码做错了什么
本文关键字:错了 什么 代码 | 更新日期: 2023-09-27 17:58:42
private void btnCalculate_Click(object sender, EventArgs e)
{
//variable declarations
int num;
//clear the listbox
lstPwrs.Items.Clear();
//add header to listbox
lstPwrs.Items.Add("N'tN^2'tN^3");
//each subsequent line is displayed as N is incremented
//use a while loop to do this, as you must be able to create
//the proper output for any upper limit the user enters
//initialize loop variable, then loop
num = Convert.ToInt32( txtInput.Text );
while ( num <= 5 )
++num;
{
lstPwrs.Items.Add( Math.Pow( num, 1 ) + "'t" + Math.Pow( num, 2 ) +
"'t" + Math.Pow( num, 3 ) );
}
}
这就是我所做的,只显示一行数字,如果我输入5作为我的数字,那么所有的事情都是以每次幂6为基数完成的,比如6^1、6^2、6^3,当5在文本框中时,这些数字的答案就会出现。希望这是有意义的。
您的while
语法错误。它执行++num
直到num <= 5
,然后执行块。我想你想要:
while ( num <= 5 )
{
++num;
lstPwrs.Items.Add( Math.Pow( num, 1 ) + "'t" + Math.Pow( num, 2 ) +
"'t" + Math.Pow( num, 3 ) );
}
或
num = 1;
while ( num <= 5 )
{
lstPwrs.Items.Add( Math.Pow( num, 1 ) + "'t" + Math.Pow( num, 2 ) +
"'t" + Math.Pow( num, 3 ) );
++num;
}
如果你想从1循环到5。
将循环后的代码放入循环中。
while ( num <= 5 ){
++num;
lstPwrs.Items.Add( Math.Pow( num, 1 ) + "'t" + Math.Pow( num, 2 ) +
"'t" + Math.Pow( num, 3 ) );
}