我正在尝试制作一个简单的程序来根据客户购买的商品数量显示折扣。
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;
double discountItem = 0;
int[] items = new int[SIZE] { 0, 10, 26, 61 };
double[] discount = new double[SIZE] { 0.0, 0.05, 0.10, 0.15 };
InputItems(ref itemsbought);
getDiscount(items, discount, ref itemsbought, ref discountItem);
Console.WriteLine("Your discount is {0}", discountItem);
}
private static void getDiscount(int[] items, double[] discount, ref int itemsbought, ref double discountItem)
{
int idx = 0;
for (idx = 1; idx >= items.Length; idx++)
{
while (itemsbought >= items[idx])
{
discountItem = discount[idx];
idx++;
}
}
}
private static void InputItems(ref int itemsbought)
{
Console.WriteLine("Enter the amount of items you bought");
while (!int.TryParse(Console.ReadLine(), out itemsbought))
Console.WriteLine("Error, whole numbers only");
}
}
}
不知何故,我知道这个逻辑真的很糟糕,但我不确定。主题之一当然是显示与输入对齐的折扣。无论输入什么值,它都只是显示“折扣为 0”。