2017-07-21 2 views
-1

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(); 
    } 

} 

}

+0

Auch tut mir leid, wenn ich es nicht richtig gesendet habe, habe ich hier nie vorher –

+1

'totals.length' ist immer 0, wenn es auf die' for' Schleife kommt, so wird es immer nur einmal Schleife. –

+0

Also bedeutet das, dass sich die for-Schleife höher bewegen muss? –

Antwort

0

Ich sehe, dass Sie immer in der Schleife mit 0 instanziiert ‚index‘ daher das gesamte Feld der aktuellen Eingabe statt nur die nächste Zelle bekommt. Wenn ich richtig verstehe, sollte Ihre for-Schleife so ähnlich aussehen:

für (; Index < totals.Length; Index ++) {}

Beachten Sie, dass Sie zu 5 Eingängen begrenzt werden (Anfang Größe des Arrays).

+0

Ich habe versucht, den Code so zu bearbeiten, dass er dem für die Anweisung entspricht, aber es tritt immer noch das gleiche Problem auf. Am Ende, wenn es die Array-Einträge anzeigt, listet es nur eine der Eingaben fünfmal auf. –

+0

Mit einem weiteren Blick auf Ihren Code sehe ich, dass es keine Notwendigkeit für die for-Schleife gibt. Fügen Sie einfach eine if-Anweisung hinzu, die prüft, ob der 'index <5' am Anfang steht, und erhöhen Sie den 'index' am Ende der Methode 'btnCalculate_Click'. Normalerweise wird dies mit einer Liste gemacht und ein Element wird mit jedem Klick hinzugefügt. –

+0

Das ist es, danke! : D Macht jetzt Sinn, wenn ich es ansehe, die for-Schleife wurde nie gebraucht. –

Verwandte Themen