else if statement

not calculating last else if?

Page 1 of 1

1 Replies - 2297 Views - Last Post: 28 February 2009 - 10:36 AM Rate Topic: -----

#1 billy81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-February 09

else if statement

Post icon  Posted 28 February 2009 - 09:57 AM

hi all, else if statement works for all entered values except the one "else if (d >= 56)".
i'm not sure why it wont return the value for this else if? It should return 175.0 but it always returns 100.0
Also, could this code be more efficient somehow?
thanks and any help is appreciated.
double fineCost = 0;		//fine
				double d = a[0];				//speed
				double InitialFine = 75.00;
				double Deduction = 50.00;
				double total = 0;
				a[0] = Console.readInt("Please enter student classification 1-4:");
				
				{
					 if (d > 35 && d <= 40)
						 
						fineCost =  InitialFine;
					 
					 else if(d >= 41 && d<= 45)
					 	{
						 fineCost = 25.50;
						 total = fineCost + InitialFine;
					 	}
					 else if (d >= 46 && d <= 50)
					 	{
						 fineCost = 51.0;
						 total = fineCost + InitialFine;
					 	}
					 else if (d >= 51 && d <= 55)
					 	{
						 fineCost = 76.50;
						 total = fineCost + InitialFine;
					 	}
					 else if (d >= 56)
			 			{
						 fineCost = 100.00;
						 total = fineCost + InitialFine;
			 			}
					 else if (d < 55)
					 	{	 	
						 fineCost = total - Deduction;
					 	}
				}
				System.out.println("The speeding fine is: "+fineCost );	
					 
				 return total;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: else if statement

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,253
  • Joined: 18-April 07

Re: else if statement

Posted 28 February 2009 - 10:36 AM

Your ranges are just a bit mixed up and yes, there is a much more efficient way to do this. First lets describe the problem...

Your last range is if d is greater than 56 is working fine. It is what you are printing is the problem. You are not to print costFine you are to print total. costFine is 100 for if they speed greater than 56. So change the last print statement from costFine to total and you will see it is printing 175

The next thing is your last else if that reads (d < 55). This statement will only be reached if actually the value of d is below 35. That is because your first 4 else if statements are all going to catch values less than 55 (but greater than 35). So you may want to adjust that. Plus remember that in that if statement total will equal 0 so minus the deduction fineCost will come out -50. Total is set in the other else ifs. You might want to make this a separate if statement instead of an else if.

Now to make this more efficient, try a little refactoring... meaning instead of calculating the same line total = fineCost + InitialFine, try setting total to initialFine and then in each statement set fineCost. At the end add them. You can also get rid of some of these compound tests.

if (d >= 56) { fineCost = 100.00; }
else if (d >= 51) { fineCost = 76.50; }
else if (d >= 46) { fineCost = 51.0;  }
else if (d >= 41) { fineCost = 25.50; }
else if (d > 35) { fineCost = 0; }

// Calculate here
total = InitialFine + fineCost;



Then all is left is to subtract out any deductions based on the speed.

Hope this helps!

"At DIC we be speed calculating code ninjas... we go 25,000 mph+ and achieve orbit!" :snap:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1