私はMurachのC#2015の書籍を購入しました。私は現在、アレイの1つのセクションに入っていて、私は立ち往生しています。アレイは1つの入力を受け入れるようにしか見えません。私は、ユーザーからのすべての入力を受け取り、それを保存して、それをメッセージボックスに逆流させる必要があります。私は配列を正しく配置したが、それは正しくforループですか?ここ配列は複数の入力を受け付けていません
は、コードの大部分、私はあなたのループの中であなたは常にので、アレイ全体が現在の入力だけではなく、次のセルを取得し、0と「インデックス」をインスタンス化していることがわかり
public partial class frmInvoiceTotal : Form
{
public frmInvoiceTotal()
{
InitializeComponent();
}
decimal[] totals = new decimal[5];
int index = 0;
// TODO: declare class variables for array and list here
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
if (txtSubtotal.Text == "")
{
MessageBox.Show(
"Subtotal is a required field.", "Entry Error");
}
else
{
for (index = 0; index < totals.Length; index++) //this the correct spot for the for loop?
{
decimal subtotal = Decimal.Parse(txtSubtotal.Text);
totals[index] = subtotal;
if (subtotal > 0 && subtotal < 10000)
{
decimal discountPercent = 0m;
if (subtotal >= 500)
discountPercent = .2m;
else if (subtotal >= 250 & subtotal < 500)
discountPercent = .15m;
else if (subtotal >= 100 & subtotal < 250)
discountPercent = .1m;
decimal discountAmount = subtotal * discountPercent;
decimal invoiceTotal = subtotal - discountAmount;
discountAmount = Math.Round(discountAmount, 2);
invoiceTotal = Math.Round(invoiceTotal, 2);
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString();
txtTotal.Text = invoiceTotal.ToString();
}
else
{
MessageBox.Show(
"Subtotal must be greater than 0 and less than 10,000.",
"Entry Error");
}
}
}
}
catch (FormatException)
{
MessageBox.Show(
"Please enter a valid number for the Subtotal field.",
"Entry Error");
}
txtSubtotal.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
MessageBox.Show("totals\n" + totals[0] + "\n"+ totals[1] + "\n" + totals[2] +
"\n" + totals[3] + "\n" + totals[4], "test");
//string totalsString = "";
//foreach (int index in totals)
//totalsString += subtotal + "\n";
//MessageBox.Show("The totals are:\n" +
//totalsString + "\n", "Order Totals");
// TODO: add code that displays dialog boxes here
this.Close();
}
}
}
また、正しく送信しなかった場合、私はここに投稿したことがありません –
'totals.length'は' for'ループに到達すると常に0になるため、一度ループします。 –
これは、forループがより高く移動しなければならないことを意味しますか? –