Also kaufte ich Murach C# 2015 Buch, nur um zu versuchen, etwas Code zu lernen. Ich bin derzeit in einem Abschnitt über Array und ich bin fest. Array scheint nur eine Eingabe zu akzeptieren. Ich brauche es, um jede Eingabe vom Benutzer zu nehmen und es zu speichern, um es dann in einer Nachrichtenbox zu erbrechen. Habe ich das Array korrekt platziert und es ist für Schleife richtig?Array akzeptiert nicht mehr als einen Eingang
hier ist der Code bulk
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();
}
}
}
Auch tut mir leid, wenn ich es nicht richtig gesendet habe, habe ich hier nie vorher –
'totals.length' ist immer 0, wenn es auf die' for' Schleife kommt, so wird es immer nur einmal Schleife. –
Also bedeutet das, dass sich die for-Schleife höher bewegen muss? –