2013-04-05 68 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(); 
     }