Note that there are some explanatory texts on larger screens.

plurals
  1. POReducing the number of if statements
    text
    copied!<p>I have the following program so far:</p> <pre><code>using System; namespace ParkingTicket { class Program { static void Main() { int speed; int yrInSchool; double fine; char choice = ' '; do { Console.Clear(); speed = GetSpeed(); if (speed &lt;= 15) Console.WriteLine("No speeding fine to pay."); else { yrInSchool = GetYrInSchool(); fine = CalculateFine(speed, yrInSchool); DisplayFine(fine); } choice = GetUserChoice(); } while (choice != 'Q' &amp;&amp; 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) &amp;&amp; (speed &gt;= 15) || (speed &lt;= 19))) { fine = INITIAL_FEE - 50.00; } else if (((year == 1) &amp;&amp; (speed &gt;= 20) || (speed &gt;= 24))) { fine += (INITIAL_FEE - 50.00) + COST_PER_5_OVER; } else if (((year == 1) &amp;&amp; (speed &gt;= 25) || (speed &lt;= 29))) { fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 2); } else if (((year == 1) &amp;&amp; (speed &gt;= 30) || (speed &lt;= 34))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 3); else if (((year == 1) &amp;&amp; (speed &gt;= 35) || (speed &lt;= 39))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 4); else if (((year == 1) &amp;&amp; (speed &gt;= 40) || (speed &lt;= 44))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 5); else if (((year == 1) &amp;&amp; (speed &gt;= 45) || (speed &lt;= 49))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 6); if (((year == 1) &amp;&amp; (speed &gt;= 50) || (speed &lt;= 54))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 7); if (((year == 1) &amp;&amp; (speed &gt;= 55) || (speed &lt;= 59))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 8); if (((year == 1) &amp;&amp; (speed &gt;= 60) || (speed &lt;= 64))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 9); if (((year == 1) &amp;&amp; (speed &gt;= 65) || (speed &lt;= 69))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 10); if (((year == 1) &amp;&amp; (speed &gt;= 70) || (speed &lt;= 74))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 11); if (((year == 1) &amp;&amp; (speed &gt;= 75) || (speed &lt;= 79))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 12); if (((year == 1) &amp;&amp; (speed &gt;= 80) || (speed &lt;= 84))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 13); if (((year == 1) &amp;&amp; (speed &gt;= 85) || (speed &lt;= 89))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 14); if (((year == 1) &amp;&amp; (speed &gt;= 90) || (speed &lt;= 94))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 15); if (((year == 1) &amp;&amp; (speed &gt;= 95) || (speed &lt;= 99))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 16); if (((year == 1) &amp;&amp; (speed &gt;= 100) || (speed &lt;= 104))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 17); if (((year == 1) &amp;&amp; (speed &gt;= 105) || (speed &lt;= 109))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 18); if (((year == 1) &amp;&amp; (speed &gt;= 110) || (speed &lt;= 114))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 19); if (((year == 1) &amp;&amp; (speed &gt;= 115) || (speed &lt;= 119))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 20); if (((year == 1) &amp;&amp; (speed &gt;= 120) || (speed &lt;= 124))) fine = (INITIAL_FEE - 50.00) + (COST_PER_5_OVER * 21); else if (((year == 2) &amp;&amp; (speed &gt;= 16) || (speed &lt;= 19))) fine = INITIAL_FEE; if (((year == 2) &amp;&amp; (speed &gt;= 20) || (speed &lt;= 24))) fine = INITIAL_FEE + (COST_PER_5_OVER); if (((year == 2) &amp;&amp; (speed &gt;= 25) || (speed &lt;= 29))) fine = INITIAL_FEE + (COST_PER_5_OVER * 2); if (((year == 2) &amp;&amp; (speed &gt;= 30) || (speed &lt;= 34))) fine = INITIAL_FEE + (COST_PER_5_OVER * 3); if (((year == 2) &amp;&amp; (speed &gt;= 35) || (speed &lt;= 39))) fine = INITIAL_FEE + (COST_PER_5_OVER * 3); if (((year == 2) &amp;&amp; (speed &gt;= 40) || (speed &lt;= 44))) fine = INITIAL_FEE + (COST_PER_5_OVER * 4); if (((year == 2) &amp;&amp; (speed &gt;= 45) || (speed &lt;= 49))) fine = INITIAL_FEE + (COST_PER_5_OVER * 5); if (((year == 2) &amp;&amp; (speed &gt;= 50) || (speed &lt;= 54))) fine = INITIAL_FEE + (COST_PER_5_OVER * 6); if (((year == 2) &amp;&amp; (speed &gt;= 55) || (speed &lt;= 59))) fine = INITIAL_FEE + (COST_PER_5_OVER * 7); if (((year == 2) &amp;&amp; (speed &gt;= 60) || (speed &lt;= 64))) fine = INITIAL_FEE + (COST_PER_5_OVER * 8); if (((year == 2) &amp;&amp; (speed &gt;= 65) || (speed &lt;= 69))) fine = INITIAL_FEE + (COST_PER_5_OVER * 9); if (((year == 2) &amp;&amp; (speed &gt;= 70) || (speed &lt;= 74))) fine = INITIAL_FEE + (COST_PER_5_OVER * 10); if (((year == 2) &amp;&amp; (speed &gt;= 75) || (speed &lt;= 79))) fine = INITIAL_FEE + (COST_PER_5_OVER * 11); if (((year == 2) &amp;&amp; (speed &gt;= 80) || (speed &lt;= 84))) fine = INITIAL_FEE + (COST_PER_5_OVER * 12); if (((year == 2) &amp;&amp; (speed &gt;= 85) || (speed &lt;= 89))) fine = INITIAL_FEE + (COST_PER_5_OVER * 13); if (((year == 2) &amp;&amp; (speed &gt;= 90) || (speed &lt;= 94))) fine = INITIAL_FEE + (COST_PER_5_OVER * 14); if (((year == 2) &amp;&amp; (speed &gt;= 95) || (speed &lt;= 99))) fine = INITIAL_FEE + (COST_PER_5_OVER * 15); if (((year == 2) &amp;&amp; (speed &gt;= 100) || (speed &lt;= 104))) fine = INITIAL_FEE + (COST_PER_5_OVER * 16); if (((year == 2) &amp;&amp; (speed &gt;= 105) || (speed &lt;= 109))) fine = INITIAL_FEE + (COST_PER_5_OVER * 17); if (((year == 2) &amp;&amp; (speed &gt;= 110) || (speed &lt;= 114))) fine = INITIAL_FEE + (COST_PER_5_OVER * 18); if (((year == 2) &amp;&amp; (speed &gt;= 115) || (speed &lt;= 119))) fine = INITIAL_FEE + (COST_PER_5_OVER * 19); if (((year == 2) &amp;&amp; (speed &gt;= 120) || (speed &lt;= 124))) fine = INITIAL_FEE + (COST_PER_5_OVER * 20); if (((year == 2) &amp;&amp; (speed &gt;= 125) || (speed &lt;= 129))) fine = INITIAL_FEE + (COST_PER_5_OVER * 21); else if (((year == 3) &amp;&amp; (speed &gt;= 16) || (speed &lt;= 19))) fine = INITIAL_FEE + 50.00; if (((year == 3) &amp;&amp; (speed &gt;= 20) || (speed &lt;= 24))) fine = INITIAL_FEE + 50.00 + (COST_PER_5_OVER); if (((year == 3) &amp;&amp; (speed &gt;= 25) || (speed &lt;= 29))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 2); if (((year == 3) &amp;&amp; (speed &gt;= 30) || (speed &lt;= 34))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 3); if (((year == 3) &amp;&amp; (speed &gt;= 35) || (speed &lt;= 39))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 4); if (((year == 3) &amp;&amp; (speed &gt;= 40) || (speed &lt;= 44))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 5); if (((year == 3) &amp;&amp; (speed &gt;= 45) || (speed &lt;= 49))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 6); if (((year == 3) &amp;&amp; (speed &gt;= 50) || (speed &lt;= 54))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 7); if (((year == 3) &amp;&amp; (speed &gt;= 55) || (speed &lt;= 59))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 8); if (((year == 3) &amp;&amp; (speed &gt;= 60) || (speed &lt;= 64))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 9); if (((year == 3) &amp;&amp; (speed &gt;= 65) || (speed &lt;= 69))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 10); if (((year == 3) &amp;&amp; (speed &gt;= 70) || (speed &lt;= 74))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 11); if (((year == 3) &amp;&amp; (speed &gt;= 75) || (speed &lt;= 79))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 12); if (((year == 3) &amp;&amp; (speed &gt;= 80) || (speed &lt;= 84))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 13); if (((year == 3) &amp;&amp; (speed &gt;= 85) || (speed &lt;= 89))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 14); if (((year == 3) &amp;&amp; (speed &gt;= 90) || (speed &lt;= 94))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 15); if (((year == 3) &amp;&amp; (speed &gt;= 95) || (speed &lt;= 99))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 16); if (((year == 3) &amp;&amp; (speed &gt;= 100) || (speed &lt;= 104))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 17); if (((year == 3) &amp;&amp; (speed &gt;= 105) || (speed &lt;= 109))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 18); if (((year == 3) &amp;&amp; (speed &gt;= 110) || (speed &lt;= 114))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 19); if (((year == 3) &amp;&amp; (speed &gt;= 115) || (speed &lt;= 119))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 20); if (((year == 3) &amp;&amp; (speed &gt;= 120) || (speed &lt;= 124))) fine = (INITIAL_FEE + 50.00) + (COST_PER_5_OVER * 21); else if (((year == 4) &amp;&amp; (speed &gt;= 16) || (speed &lt;= 19))) fine = INITIAL_FEE + 100.00; if (((year == 4) &amp;&amp; (speed &gt;= 20) || (speed &lt;= 24))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER); if (((year == 4) &amp;&amp; (speed &gt;= 25) || (speed &lt;= 29))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 2); if (((year == 4) &amp;&amp; (speed &gt;= 30) || (speed &lt;= 34))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 3); if (((year == 4) &amp;&amp; (speed &gt;= 35) || (speed &lt;= 39))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 4); if (((year == 4) &amp;&amp; (speed &gt;= 40) || (speed &lt;= 44))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 5); if (((year == 4) &amp;&amp; (speed &gt;= 45) || (speed &lt;= 49))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 6); if (((year == 4) &amp;&amp; (speed &gt;= 100) || (speed &lt;= 54))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 7); if (((year == 4) &amp;&amp; (speed &gt;= 55) || (speed &lt;= 59))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 8); if (((year == 4) &amp;&amp; (speed &gt;= 60) || (speed &lt;= 64))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 9); if (((year == 4) &amp;&amp; (speed &gt;= 65) || (speed &lt;= 69))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 10); if (((year == 4) &amp;&amp; (speed &gt;= 70) || (speed &lt;= 74))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 11); if (((year == 4) &amp;&amp; (speed &gt;= 75) || (speed &lt;= 79))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 12); if (((year == 4) &amp;&amp; (speed &gt;= 80) || (speed &lt;= 84))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 13); if (((year == 4) &amp;&amp; (speed &gt;= 85) || (speed &lt;= 89))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 14); if (((year == 4) &amp;&amp; (speed &gt;= 90) || (speed &lt;= 94))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 15); if (((year == 4) &amp;&amp; (speed &gt;= 95) || (speed &lt;= 99))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 16); if (((year == 4) &amp;&amp; (speed &gt;= 100) || (speed &lt;= 104))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER); if (((year == 4) &amp;&amp; (speed &gt;= 105) || (speed &lt;= 109))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 18); if (((year == 4) &amp;&amp; (speed &gt;= 110) || (speed &lt;= 114))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 19); if (((year == 4) &amp;&amp; (speed &gt;= 115) || (speed &lt;= 119))) fine = (INITIAL_FEE + 100.00) + (COST_PER_5_OVER * 20); if (((year == 4) &amp;&amp; (speed &gt;= 120) || (speed &lt;= 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; } } } </code></pre> <p>I have a whole list of these statements going up to 125 mph and for different years: 1 through 4. I'm trying to make a program that takes input for speed of a vehicle, then gives appropriate ticket information according to speed.</p> <p>The speed limit is 15 mph. For every 5 miles per hour over speed limit, $87.50 is added towards the total. Year 2 is sophomore so a $50.00 discount applies. Yet for like year 4, a $100.00 fee is added towards the total. I am getting same total for each speed. Why? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload