if else statements

beginner question

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2370 Views - Last Post: 26 February 2010 - 07:49 PM Rate Topic: -----

#1 Guest_DomS*


Reputation:

if else statements

Posted 26 February 2010 - 06:03 PM

im trying to write a program that asks the user for the amount of breakfast, lunch , and dinner they want. depending on if they enter a certain amount (breakfast > 10) they receive a discount. well for some reason i dont think all my if else statements are working correctly because im not getting the right total.

		if (breakfast > 10)
		   {finalcost1 = breakfast *  cost_of_breakfast - (cost_of_breakfast * discount_of_breakfast);
		   }
		else
		   {finalcost1 = breakfast * cost_of_breakfast;
	    	}
		if (lunch > 15)
		   {finalcost2 = lunch * cost_of_lunch - (cost_of_lunch * discount_of_lunch);
		   }
		else
		   {finalcost2 = lunch * cost_of_lunch;
		   }
		if (dinner > 8)
		   {finalcost3 = dinner * cost_of_dinner - (cost_of_dinner * discount_of_dinner);
		   }
		else
		   {finalcost3 = dinner * cost_of_dinner;
		   }


what should i do??
thanks a lot.

Is This A Good Question/Topic? 0

Replies To: if else statements

#2 redhotfire0   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: if else statements

Posted 26 February 2010 - 06:07 PM

Edit: Sorry, I couldn't help.

This post has been edited by redhotfire0: 26 February 2010 - 06:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: if else statements

Posted 26 February 2010 - 06:08 PM

You need to put else if instead of if and that should work.

 if (breakfast > 10)
                   {finalcost1 = breakfast *  cost_of_breakfast - (cost_of_breakfast * discount_of_breakfast);
                   }
                
                else if (lunch > 15)
                   {finalcost2 = lunch * cost_of_lunch - (cost_of_lunch * discount_of_lunch);
                   }
               
                else if (dinner > 8)
                   {finalcost3 = dinner * cost_of_dinner - (cost_of_dinner * discount_of_dinner);
                   }
                else
                   {finalcost3 = dinner * cost_of_dinner;
                   }




edit: forgot to adjust code

This post has been edited by m-e-g-a-z: 26 February 2010 - 06:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_DomS*


Reputation:

Re: if else statements

Posted 26 February 2010 - 06:11 PM

View Postm-e-g-a-z, on 26 February 2010 - 05:08 PM, said:

You need to put else if instead of if and that should work.

 if (breakfast > 10)
                   {finalcost1 = breakfast *  cost_of_breakfast - (cost_of_breakfast * discount_of_breakfast);
                   }
                else
                   {finalcost1 = breakfast * cost_of_breakfast;
                }
                else if (lunch > 15)
                   {finalcost2 = lunch * cost_of_lunch - (cost_of_lunch * discount_of_lunch);
                   }
                else
                   {finalcost2 = lunch * cost_of_lunch;
                   }
                else if (dinner > 8)
                   {finalcost3 = dinner * cost_of_dinner - (cost_of_dinner * discount_of_dinner);
                   }
                else
                   {finalcost3 = dinner * cost_of_dinner;
                   }




when i try that i get two errors saying
java:56: 'else' without 'if'
else if (lunch > 15)
^
Was This Post Helpful? 0

#5 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: if else statements

Posted 26 February 2010 - 06:13 PM

ooops..i forgot about that.

You only need one else statement
if(...) 

{
//...
}

else if(..)
{
//...
}

else {
//do something///
}


This post has been edited by m-e-g-a-z: 26 February 2010 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_DomS*


Reputation:

Re: if else statements

Posted 26 February 2010 - 06:18 PM

im sorry this is my first time using if else statements so i'm a little confused

something like this?
 if (breakfast > 10)
                   {finalcost1 = breakfast *  cost_of_breakfast - (cost_of_breakfast * discount_of_breakfast);
                   }
                else if 
                   {finalcost1 = breakfast * cost_of_breakfast;
                }
                else if (lunch > 15)
                   {finalcost2 = lunch * cost_of_lunch - (cost_of_lunch * discount_of_lunch);
                   }
                else if
                   {finalcost2 = lunch * cost_of_lunch;
                   }
                else if (dinner > 8)
                   {finalcost3 = dinner * cost_of_dinner - (cost_of_dinner * discount_of_dinner);
                   }
                else if
                   {finalcost3 = dinner * cost_of_dinner;
                   }


Was This Post Helpful? 0

#7 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: if else statements

Posted 26 February 2010 - 06:20 PM

theres a few things wrong plus last condition needs to be an else.

else 
         {
             finalcost3 = dinner * cost_of_dinner;
                   }


This post has been edited by m-e-g-a-z: 26 February 2010 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_DomS*


Reputation:

Re: if else statements

Posted 26 February 2010 - 06:24 PM

based on the number of dinners lunches and breakfasts ordered there could be a discount applied to each order. so wouldn't i need 3 sets of if/else statements that could work independently? this is all super confusing haha. i apologize for being such a bother! thanks.
Was This Post Helpful? 0

#9 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: if else statements

Posted 26 February 2010 - 06:27 PM

It would be like this

 if (breakfast > 10)
                   {finalcost1 = breakfast *  cost_of_breakfast - (cost_of_breakfast * discount_of_breakfast);
                   }
                else if (breakfast < 10)
                   {finalcost1 = breakfast * cost_of_breakfast;
                }
                else if (lunch > 15)
                   {finalcost2 = lunch * cost_of_lunch - (cost_of_lunch * discount_of_lunch);
                   }
                else if (lunch < 15)
                   {finalcost2 = lunch * cost_of_lunch;
                   }
                else if (dinner > 8)
                   {finalcost3 = dinner * cost_of_dinner - (cost_of_dinner * discount_of_dinner);
                   }
                else  
                   {finalcost3 = dinner * cost_of_dinner;
                   }




This post has been edited by m-e-g-a-z: 26 February 2010 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#10 Guest_DomS*


Reputation:

Re: if else statements

Posted 26 February 2010 - 06:32 PM

thanks a lot! now im running into a problem i didnt have before haha

java:71: variable finalcost1 might not have been initialized
total = (finalcost1 + finalcost2 + finalcost3);
^
java:71: variable finalcost2 might not have been initialized
total = (finalcost1 + finalcost2 + finalcost3);
^
java:71: variable finalcost3 might not have been initialized
total = (finalcost1 + finalcost2 + finalcost3);
^
Was This Post Helpful? 0

#11 Guest_DomS*


Reputation:

Re: if else statements

Posted 26 February 2010 - 06:33 PM

View PostDomS, on 26 February 2010 - 05:32 PM, said:

thanks a lot! now im running into a problem i didnt have before haha

java:71: variable finalcost1 might not have been initialized
total = (finalcost1 + finalcost2 + finalcost3);
^
java:71: variable finalcost2 might not have been initialized
total = (finalcost1 + finalcost2 + finalcost3);
^
java:71: variable finalcost3 might not have been initialized
total = (finalcost1 + finalcost2 + finalcost3);
^


each of the ^ are pointing at a finalcost. sorry
Was This Post Helpful? 0

#12 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: if else statements

Posted 26 February 2010 - 06:38 PM

You need to initialize the variable finalcost1 to a value when you create it.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_DomS*


Reputation:

Re: if else statements

Posted 26 February 2010 - 06:41 PM

can i just do

double finalcost1 = console.nextDouble();?


i tried that and it said the symbol cannot be found referring to the period between console and next
Was This Post Helpful? 0

#14 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: if else statements

Posted 26 February 2010 - 06:52 PM

post the code and il be able to see where the problem is.
Was This Post Helpful? 0
  • +
  • -

#15 Guest_DomS*


Reputation:

Re: if else statements

Posted 26 February 2010 - 06:54 PM

import java.util.*;

public class SampsonAssign5
{
	static Scanner console = new Scanner(System.in);
	
	static final double cost_of_breakfast = 5.50;
	static final double cost_of_lunch = 9.50;
	static final double cost_of_dinner = 16.50;
	
	static final double sales_tax = .1;
	
	static final double discount_of_breakfast = .1; 
	static final double discount_of_lunch = .15;
	static final double discount_of_dinner = .12;
	
	public static void main(String[] args)
	{
		int breakfast;
		int lunch;
		int dinner;
		double discount1;
		double discount2;
		double discount3;
		double finalcost1; 
		double finalcost2;
		double finalcost3;
		double total;
		double totaldue;
		double salestax;
		
		finalcost1 = console.nextDouble;
		finalcost2 = console.nextDouble;
		finalcost3 = console.nextDouble;
		
		System.out.println("This program computes "
		                 + "a catering bill.");
							  
		System.out.print("Enter the amount "
		               + "of breakfasts: ");
		breakfast = console.nextInt();
		System.out.println();
		
		System.out.print("Enter the amount "
		               + "of lunches: ");
		lunch = console.nextInt();
		System.out.println();
		
		System.out.print("Enter the amount "
		               + "of dinners: ");
		dinner = console.nextInt();
		System.out.println();
		
      if (breakfast > 10)
                   {finalcost1 = breakfast *  cost_of_breakfast - (cost_of_breakfast * discount_of_breakfast);
                   }
                else if (breakfast < 10) 
                   {finalcost1 = breakfast * cost_of_breakfast;
                }
                else if (lunch > 15)
                   {finalcost2 = lunch * cost_of_lunch - (cost_of_lunch * discount_of_lunch);
                   }
                else if (lunch < 15)
                   {finalcost2 = lunch * cost_of_lunch;
                   }
                else if (dinner > 8)
                  {finalcost3 = dinner * cost_of_dinner - (cost_of_dinner * discount_of_dinner);
                   }
                else 
                   {finalcost3 = dinner * cost_of_dinner;
                   }
	
		total = (finalcost1 + finalcost2 + finalcost3);
		salestax = total * .10;
		totaldue = total + salestax;
		
					
		System.out.printf("Amount due = $%.2f %n", 
                         totaldue);	
	} 
}


Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2