我想制作一个程序,其中用户输入一个数字,在这种情况下是一些项目。 然后将项目数与数组中的值进行比较,并显示相应的折扣。
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而不是61给出10?
的输出每次我输入的内容都会显示错误信息,只有在数字小于0或双倍时才会显示。
答案 0 :(得分:1)
要显示您所犯的小错误,请参阅此更正版本:
for (idx = 0; idx < items.Length && itemsbought > items[idx]; 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 :(得分: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;
}
}
您收到错误的原因是因为数字&gt;你的items数组中不包含61,所以for循环继续,你可以突破它来返回折扣数组中的最后一个元素(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);
答案 2 :(得分: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();
}