2013-04-05 69 views
1

我想製作一個程序,其中用戶輸入一個數字,在這種情況下是多個項目。 然後將項目數量與數組中的值進行比較,並顯示相應的折扣。輸入的數字超出數組設置的範圍,索引超出範圍錯誤

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication11 
{ 
    class Program 
    { 
     const int SIZE = 4; 
     static void Main(string[] args) 
     { 
      int itemsbought = 0; 
      int discountItem = 0; 
      int[] items = new int[SIZE] { 0, 10, 26, 61 }; 
      int[] discount = new int[SIZE] { 0, 5, 10,15 }; 

      InputItems(ref itemsbought); 
      getDiscount(items, discount, ref itemsbought, ref discountItem); 

      Console.WriteLine("Your discount is {0}", discountItem); 

     } 

     private static void getDiscount(int[] items, int[] discount, ref int itemsbought, ref int discountItem) 
     { 
      int idx = 0; 
      for (idx = 0; itemsbought > items[idx] || idx > items.Length; idx++) 
      { 

        discountItem = discount[idx]; 
      } 
      } 

     private static void InputItems(ref int itemsbought) 
     { 
      Console.WriteLine("Enter the amount of items you bought"); 
      while (!int.TryParse(Console.ReadLine(), out itemsbought)) 
       if (itemsbought < 0) 
      { 
        Console.WriteLine("Error, whole numbers over 0 only"); 
      } 
       Console.WriteLine("Error, whole numbers over 0 only"); 
     } 
    } 
} 

當輸入上述61個電話號碼我得到「索引超出範圍」錯誤。我怎麼能這樣做,如果輸入一個高於61的數字,它顯示15?另外我怎樣才能做到這一點,使得這個邊界包含61個而不是61個,輸出10個?

同樣每次我輸入的東西,它只給出顯示的錯誤信息,只有當數字小於0或雙精度時才顯示。

回答

1

爲了顯示你做的小錯誤,看到這個修改後的版本:

for (idx = 0; idx < items.Length && itemsbought > items[idx]; idx++) 

有三個重要的變化:

  1. IDX> items.Length永遠是假的。而idx = items.Length超出範圍。
  2. 使用& &代替||記住,如果這個條件是真的,循環繼續執行,一旦它是假的,執行就停止。
  3. 交換了命令。您必須檢查idx < items.Length 之前訪問項目[idx]。短路& &從左到右評估,如果結果已確定則停止。

所以,你的更正後的代碼看起來就像這樣:

private static void getDiscount(int[] items, int[] discount, int itemsbought, ref int discountItem) 
{ 
    int idx = 0; 
    for (idx = 0; idx < items.Length && itemsbought > items[idx]; idx++) 
     discountItem = discount[idx]; 
} 

但我寧願把條件內循環,以使其更易於閱讀:

private static void getDiscount(int[] items, int[] discount, int itemsbought, ref int discountItem) 
{ 
    for (int i = 0; i < items.Length; i++) 
    { 
     if(itemsbought > items[i]) 
      discountItem = discount[i]; 
     else 
      break; 
    } 
} 

解決您的其他問題

同樣每次我輸入的東西都會顯示錯誤消息,只有當數字小於0或雙精度時纔會顯示。

正確地重新格式化您的代碼,一個消息輸出在正確的位置,另一個總是執行。

1

我會重寫你getDiscount如下:

private static void getDiscount(int[] items, int[] discount, ref int itemsbought, ref int discountItem) 
{ 
    for (int i = 0; itemsbought > items[i];) 
    { 
     discountItem = discount[i]; 
     i++; 
     if (i >= items.Length)//will return the last element in the discount array(15) 
      break; 
    } 
} 

你的錯誤是因爲號碼>比61沒有你的項目數組中包含的原因,所以對循環繼續,你可以打出來它以折扣數組中返回的最後一個元素(15)

然後重寫你的InputItems方法,像這樣:

private static void InputItems(int itemsbought) 
{ 
    Console.WriteLine("Enter the amount of items you bought"); 
    if (!int.TryParse(Console.ReadLine(), out itemsbought) || itemsbought < 0) 
    { 
     Console.WriteLine("Error, whole numbers over 0 only"); 
    } 
} 

無論tryparse/if語句是否成功,您都會返回相同的錯誤消息。您還需要將呼叫更新爲InputItems(itemsbought);

0

查看問題的不同方式,但您可以選擇使用它。

public class Item 
    { 
     public Item(string name, int qty) 
     { 
      ItemName = name; 
      Qty = qty; 
     } 

     public enum DiscountRate 
     { 
      ZeroPercent = 0, 
      FivePercent = 10, 
      TenPercent = 26, 
      FifteenPercent = 61 

     } 

     public override string ToString() 
     { 
      return string.Format("Items Name: {0} | Units: {1} | Discount Rate: {2}", this.ItemName, this.Qty, this.Rate.ToString()); 
     } 

     public string CalculateDiscount() 
     { 
      if (this.Qty >= (int)DiscountRate.FifteenPercent) 
      { 
       this.Rate = DiscountRate.FifteenPercent; 
       return this.ToString(); 
      } 
      else if (this.Qty >= (int)DiscountRate.TenPercent && this.Qty < (int)DiscountRate.FifteenPercent) 
      { 
       this.Rate = DiscountRate.TenPercent; 
       return this.ToString(); 
      } 
      else if (this.Qty < (int)DiscountRate.TenPercent && this.Qty > 9) 
      { 
       this.Rate = DiscountRate.FivePercent; 
       return this.ToString(); 
      } 
      else 
      { 
       this.Rate = DiscountRate.ZeroPercent; 
       return this.ToString(); 
      } 
     } 

     public string ItemName { get; set; } 
     public int Qty { get; set; } 
     public DiscountRate Rate {get; set;} 
    } 

主要代碼: -

class Program 
    { 
     static void Main(string[] args) 
     { 
      Item a = new Item("Tennis Ball", 100); 
      Item b = new Item("Spoon", 10); 
      Item c = new Item("Candles", 27); 
      Item d = new Item("Battery's", 2); 
      Item e = new Item("Nails", 10); 
      Item f = new Item("Marbles", 0); 

      Console.WriteLine(a.CalculateDiscount()); 
      Console.WriteLine(); 
      Console.WriteLine(b.CalculateDiscount()); 
      Console.WriteLine(); 
      Console.WriteLine(c.CalculateDiscount()); 
      Console.WriteLine(); 
      Console.WriteLine(d.CalculateDiscount()); 
      Console.WriteLine(); 
      Console.WriteLine(e.CalculateDiscount()); 
      Console.WriteLine(); 
      Console.WriteLine(f.CalculateDiscount()); 
      Console.WriteLine(); 
      Console.ReadLine(); 
     }