到目前为止我有以下程序:
using System; namespace ParkingTicket { class Program { static void Main() { int speed; int yrInSchool; double fine; char choice = ' '; do { Console.Clear(); speed = GetSpeed(); if (speed <= 15) Console.WriteLine("No speeding fine to pay."); else { yrInSchool = GetYrInSchool(); fine = CalculateFine(speed, yrInSchool); DisplayFine(fine); } choice = GetUserChoice(); } while (choice != 'Q' && choice != 'q'); } static int GetSpeed() { int speed; string userInput; try { Console.Write("Please enter the speed you were traveling: "); userInput = Console.ReadLine(); speed = Convert.ToInt32(userInput); } catch { Console.WriteLine("\a\n INVALID - PLEASE TRY AGAIN"); Console.Write("Please press enter to continue...."); userInput = Console.ReadLine(); speed = GetSpeed(); // this is the recursion - calling myself } return speed; // code this method } static int GetYrInSchool() { string userEntry; int year; /************************************************************* * modify this method to validate the year using a Try/Catch *************************************************************/ Console.WriteLine("\nClassifications"); Console.WriteLine("\tFreshman (enter 1)"); Console.WriteLine("\tSophomore (enter 2)"); Console.WriteLine("\tJunior (enter 3)"); Console.WriteLine("\tSenior (enter 4)"); try { Console.Write("Enter choice: "); userEntry = Console.ReadLine(); year = Convert.ToInt32(userEntry); } catch { Console.WriteLine("\a\n INVALID - PLEASE TRY AGAIN"); Console.Write("Please press enter to continue...."); userEntry = Console.ReadLine(); year = GetYrInSchool(); // this is the recursion - calling myself } return year; } static double CalculateFine(int speed, int year) { const double COST_PER_5_OVER = 87.50; const int SPEED_LIMIT = 15; const double INITIAL_FEE = 75.00; double fine = 0; if (((year == 1) && (speed >= 15) || (speed <= 19))) { fine = INITIAL_FEE - 50.00; } else if (((year == 1) && (speed >= 20) || (speed >= 24))) { fine += (INITIAL_FEE - 50.00) + COST_PER_5_OVER; } else if (((year == 1) && (speed >= 25) || (speed <= 29))) { fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 2); } else if (((year == 1) && (speed >= 30) || (speed <= 34))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 3); else if (((year == 1) && (speed >= 35) || (speed <= 39))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 4); else if (((year == 1) && (speed >= 40) || (speed <= 44))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 5); else if (((year == 1) && (speed >= 45) || (speed <= 49))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 6); if (((year == 1) && (speed >= 50) || (speed <= 54))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 7); if (((year == 1) && (speed >= 55) || (speed <= 59))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 8); if (((year == 1) && (speed >= 60) || (speed <= 64))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 9); if (((year == 1) && (speed >= 65) || (speed <= 69))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 10); if (((year == 1) && (speed >= 70) || (speed <= 74))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 11); if (((year == 1) && (speed >= 75) || (speed <= 79))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 12); if (((year == 1) && (speed >= 80) || (speed <= 84))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 13); if (((year == 1) && (speed >= 85) || (speed <= 89))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 14); if (((year == 1) && (speed >= 90) || (speed <= 94))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 15); if (((year == 1) && (speed >= 95) || (speed <= 99))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 16); if (((year == 1) && (speed >= 100) || (speed <= 104))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 17); if (((year == 1) && (speed >= 105) || (speed <= 109))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 18); if (((year == 1) && (speed >= 110) || (speed <= 114))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 19); if (((year == 1) && (speed >= 115) || (speed <= 119))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 20); if (((year == 1) && (speed >= 120) || (speed <= 124))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 21); else if (((year == 2) && (speed >= 16) || (speed <= 19))) fine = INITIAL_FEE; if (((year == 2) && (speed >= 20) || (speed <= 24))) fine = INITIAL_FEE + (COST_PER_5_OVER); if (((year == 2) && (speed >= 25) || (speed <= 29))) fine = INITIAL_FEE + (COST_PER_5_OVER * 2); if (((year == 2) && (speed >= 30) || (speed <= 34))) fine = INITIAL_FEE + (COST_PER_5_OVER * 3); if (((year == 2) && (speed >= 35) || (speed <= 39))) fine = INITIAL_FEE + (COST_PER_5_OVER * 3); if (((year == 2) && (speed >= 40) || (speed <= 44))) fine = INITIAL_FEE + (COST_PER_5_OVER * 4); if (((year == 2) && (speed >= 45) || (speed <= 49))) fine = INITIAL_FEE + (COST_PER_5_OVER * 5); if (((year == 2) && (speed >= 50) || (speed <= 54))) fine = INITIAL_FEE + (COST_PER_5_OVER * 6); if (((year == 2) && (speed >= 55) || (speed <= 59))) fine = INITIAL_FEE + (COST_PER_5_OVER * 7); if (((year == 2) && (speed >= 60) || (speed <= 64))) fine = INITIAL_FEE + (COST_PER_5_OVER * 8); if (((year == 2) && (speed >= 65) || (speed <= 69))) fine = INITIAL_FEE + (COST_PER_5_OVER * 9); if (((year == 2) && (speed >= 70) || (speed <= 74))) fine = INITIAL_FEE + (COST_PER_5_OVER * 10); if (((year == 2) && (speed >= 75) || (speed <= 79))) fine = INITIAL_FEE + (COST_PER_5_OVER * 11); if (((year == 2) && (speed >= 80) || (speed <= 84))) fine = INITIAL_FEE + (COST_PER_5_OVER * 12); if (((year == 2) && (speed >= 85) || (speed <= 89))) fine = INITIAL_FEE + (COST_PER_5_OVER * 13); if (((year == 2) && (speed >= 90) || (speed <= 94))) fine = INITIAL_FEE + (COST_PER_5_OVER * 14); if (((year == 2) && (speed >= 95) || (speed <= 99))) fine = INITIAL_FEE + (COST_PER_5_OVER * 15); if (((year == 2) && (speed >= 100) || (speed <= 104))) fine = INITIAL_FEE + (COST_PER_5_OVER * 16); if (((year == 2) && (speed >= 105) || (speed <= 109))) fine = INITIAL_FEE + (COST_PER_5_OVER * 17); if (((year == 2) && (speed >= 110) || (speed <= 114))) fine = INITIAL_FEE + (COST_PER_5_OVER * 18); if (((year == 2) && (speed >= 115) || (speed <= 119))) fine = INITIAL_FEE + (COST_PER_5_OVER * 19); if (((year == 2) && (speed >= 120) || (speed <= 124))) fine = INITIAL_FEE + (COST_PER_5_OVER * 20); if (((year == 2) && (speed >= 125) || (speed <= 129))) fine = INITIAL_FEE + (COST_PER_5_OVER * 21); else if (((year == 3) && (speed >= 16) || (speed <= 19))) fine = INITIAL_FEE + 50.00; if (((year == 3) && (speed >= 20) || (speed <= 24))) fine = INITIAL_FEE + 50.00 + (COST_PER_5_OVER); if (((year == 3) && (speed >= 25) || (speed <= 29))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 2); if (((year == 3) && (speed >= 30) || (speed <= 34))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 3); if (((year == 3) && (speed >= 35) || (speed <= 39))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 4); if (((year == 3) && (speed >= 40) || (speed <= 44))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 5); if (((year == 3) && (speed >= 45) || (speed <= 49))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 6); if (((year == 3) && (speed >= 50) || (speed <= 54))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 7); if (((year == 3) && (speed >= 55) || (speed <= 59))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 8); if (((year == 3) && (speed >= 60) || (speed <= 64))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 9); if (((year == 3) && (speed >= 65) || (speed <= 69))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 10); if (((year == 3) && (speed >= 70) || (speed <= 74))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 11); if (((year == 3) && (speed >= 75) || (speed <= 79))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 12); if (((year == 3) && (speed >= 80) || (speed <= 84))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 13); if (((year == 3) && (speed >= 85) || (speed <= 89))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 14); if (((year == 3) && (speed >= 90) || (speed <= 94))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 15); if (((year == 3) && (speed >= 95) || (speed <= 99))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 16); if (((year == 3) && (speed >= 100) || (speed <= 104))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 17); if (((year == 3) && (speed >= 105) || (speed <= 109))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 18); if (((year == 3) && (speed >= 110) || (speed <= 114))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 19); if (((year == 3) && (speed >= 115) || (speed <= 119))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 20); if (((year == 3) && (speed >= 120) || (speed <= 124))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 21); else if (((year == 4) && (speed >= 16) || (speed <= 19))) fine = INITIAL_FEE + 100.00; if (((year == 4) && (speed >= 20) || (speed <= 24))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER); if (((year == 4) && (speed >= 25) || (speed <= 29))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 2); if (((year == 4) && (speed >= 30) || (speed <= 34))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 3); if (((year == 4) && (speed >= 35) || (speed <= 39))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 4); if (((year == 4) && (speed >= 40) || (speed <= 44))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 5); if (((year == 4) && (speed >= 45) || (speed <= 49))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 6); if (((year == 4) && (speed >= 100) || (speed <= 54))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 7); if (((year == 4) && (speed >= 55) || (speed <= 59))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 8); if (((year == 4) && (speed >= 60) || (speed <= 64))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 9); if (((year == 4) && (speed >= 65) || (speed <= 69))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 10); if (((year == 4) && (speed >= 70) || (speed <= 74))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 11); if (((year == 4) && (speed >= 75) || (speed <= 79))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 12); if (((year == 4) && (speed >= 80) || (speed <= 84))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 13); if (((year == 4) && (speed >= 85) || (speed <= 89))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 14); if (((year == 4) && (speed >= 90) || (speed <= 94))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 15); if (((year == 4) && (speed >= 95) || (speed <= 99))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 16); if (((year == 4) && (speed >= 100) || (speed <= 104))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER); if (((year == 4) && (speed >= 105) || (speed <= 109))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 18); if (((year == 4) && (speed >= 110) || (speed <= 114))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 19); if (((year == 4) && (speed >= 115) || (speed <= 119))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 20); if (((year == 4) && (speed >= 120) || (speed <= 124))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 21); // finish coding this method return fine; } static void DisplayFine(double fine) { Console.WriteLine("Fine: {0:C}", fine); } static char GetUserChoice() { Console.Write ("\nPress \"C\" to [C]ontinue or \"Q\" to [Q]uit: "); string userEntry = Console.ReadLine(); char choice = Convert.ToChar(userEntry); Console.WriteLine("------------------------------------"); return choice; } } }
我有一个完整的列表,这些声明高达125英里/小时和不同年份:1到4.我正在尝试制作一个程序,接受车辆的速度输入,然后根据速度提供适当的票务信息.
限速为15英里/小时.每超过5英里的速度限制,总计增加87.50美元.2年级是大二学生,因此需要50.00美元的折扣.然而,就像第4年一样,总额增加了100.00美元的费用.我为每个速度得到相同的总数.为什么?
其他人对运营商优先权的看法似乎是正确的,但我认为这里有一个更大的问题.你真的不需要数以万计的if语句来模拟这个问题.我相信你现在已经发现这种方法不可维护.试图改变那堆ifs中的任何东西将是一个真正的痛苦,非常容易出错.
我要做的第一件事是将折扣的计算与罚款的计算分开.这样您就不必手动计算折扣和罚款的每种可能组合.这就是我在说的:
static double CalculateFine(int speed, int year) { const double COST_PER_5_OVER = 87.5; const int SPEED_LIMIT = 15; const double INITIAL_FEE = 75; // Should there be a fine at all? if (speed > SPEED_LIMIT) { // The discount is 50 for each year, scaled so that year 1 is // -50, year 2 is 0, and so on. double discount = year * 50 - 100; // Now calculate the standard fee. int feeMultiplier = (speed - SPEED_LIMIT) / 5; double fine = feeMultiplier * COST_PER_5_OVER + INITIAL_FEE; return discount + fine; } return 0.; }
最后,折扣和罚款仅合并一次.真的,这只是弄清楚用什么公式来计算罚款,然后实施.如果以更随意的方式定义罚款,那么表格可能是有用的.