我試圖做一個簡單的程序來顯示折扣取決於客戶購買的項目數。一組值範圍中的數組值不顯示
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」。