For loop 使用for循环将基本复利作为列表输出到MessageBox中
我需要输出基本复利,它将显示在使用for循环的消息框中 消息框需要显示标题为“年”的左栏和显示“金额”的右栏 我的代码只显示messagebox 5次,而我需要它全部显示在列表中 以下是我到目前为止的情况:For loop 使用for循环将基本复利作为列表输出到MessageBox中,for-loop,messagebox,For Loop,Messagebox,我需要输出基本复利,它将显示在使用for循环的消息框中 消息框需要显示标题为“年”的左栏和显示“金额”的右栏 我的代码只显示messagebox 5次,而我需要它全部显示在列表中 以下是我到目前为止的情况: private void button1_Click(object sender, EventArgs e) { decimal amount; decimal principal = 1000; double rate = 0
private void button1_Click(object sender, EventArgs e)
{
decimal amount;
decimal principal = 1000;
double rate = 0.05;
string msg;
msg = "Years \t Amount";
msg += Environment.NewLine;
for (int year = 1; year <= 5; year++)
{
amount = principal * ((decimal)Math.Pow(1.0 + rate, year));
msg += amount.ToString("C");
msg += Environment.NewLine;
}
MessageBox.Show(msg, "Compund Interest");
private void按钮1\u单击(对象发送者,事件参数e)
{
小数金额;
十进制本金=1000;
倍率=0.05;
串味精;
msg=“年份\t金额”;
msg+=Environment.NewLine;
对于(int year=1;yearprivate void按钮1_单击(对象发送者,事件参数e)
{
字符串金额=”;
十进制本金=1000;
倍率=0.05;
对于(int year=1;yearprivate void按钮1_单击(对象发送者,事件参数e)
{
小数金额;
十进制本金=1000;
倍率=0.05;
串味精;
//在此处开始构建字符串,即msg=“header\t header”\t放置一个选项卡
//添加回车符msg+=environment.Newline
对于(int year=1;year您希望消息框显示5次,列表每次添加新行,还是用完成的5行列表显示一次?用完成的5行列表显示一次?如果您不希望消息框显示五次,则消息框可能会移动。您希望生成一个字符串以在该消息框中显示我想我需要把它移出循环,但是它会失去“数量”值,你不会失去“数量”但它只有上一次计算的值,因为每次迭代都会覆盖它。相反,可以使用字符串生成器构建一个字符串,并在循环的每次迭代中附加到它。特别是当它显然是一个家庭作业时,最好尝试教些东西,而不是只发布一个没有解释的答案。您的答案让我更接近了,但我还需要在左边有一个列,显示每只耳朵向下移动的情况,以及显示年份和时间的列标题AMount@RyanMcCloy试着自己用一些提示来完成它,而不是仅仅复制别人的代码。是的,我不会复制它,仍然试图找出这个问题,否则我甚至不会remember@RyanMcCloy字体我已经演示了如何将messagebox移出循环,如何将变量更改为字符串(因为无法将行添加到小数)以及如何添加新行。与新行一样,还有什么不能添加到小数点?或者换句话说,您会在上面的代码中更改/添加/删除哪些内容,以显示年份和金额,并在messagebox中进行良好的格式设置?
private void button1_Click(object sender, EventArgs e)
{
string amount = "";
decimal principal = 1000;
double rate = 0.05;
for (int year = 1; year <= 5; year++) {
amount += string.Format("{0:C}", principal * Convert.ToDecimal(Math.Pow(1.0 + rate, year))) + Environment.NewLine;
}
MessageBox.Show(amount, "Compound Inerest");
}
private void button1_Click(object sender, EventArgs e)
{
decimal amount;
decimal principal = 1000;
double rate = 0.05;
string msg;
// start building string here i.e. msg = "header \t header" \t places a tab
// add a carriage return msg += Enviornment.Newline
for (int year = 1; year <= 5; year++)
{
amount = principal * ((decimal)Math.Pow(1.0 + rate, year));
// don't overwrite msg but append to it msg += "year number use your counter \t" += what you are already doing below.
msg = amount.ToString("C");
// then add your carriage return again.
}
MessageBox.Show(msg);
}