• 0

Help with Logic ( JAVA )


Question

I am working on a small program for school "vending machine". The basics of it are, you must pay for items with 1 dollar ( 100 ), and all items cost between 25 - 100 in increments of 5. The program must return the change in the following format:

X Quarters ( if not 0 )

X Dimes ( if not 0 )

X Nickels ( if not 0 )

If 0, then it skips the line, prints nothing and continues on.

I cant get the logic right:

	int Change = (100 - ItemCost);
				int Quarters = (Change / 25);
	 			int Dimes = (insert logic help neowinians!);
				int Nickels = (insert logic help neowinians!);

		 System.out.printf("Your change is %s cents.\n", Change);

			if(Quarters != 0)
		   System.out.printf("%s Quarter(s)\n", Quarters);
			else System.out.println();

			if(Dimes != 0)
		   System.out.printf("%sDime(s)\n", Dimes);
			else System.out.println();

			if(Nickels != 0)
		   System.out.printf("%sNickel(s)\n", Nickels);
			else System.out.println();

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

i would do something like this

while(change-25 >= 0)

{

change = change - 25;

quarter++;

}

then do it for dimes

then nickles

then your done!

sorry lol, just a quick solution

Edited by ekw
Link to comment
Share on other sites

  • 0

well implementation wise...

assuming if i understand the problem correctly, you want to give back change using the least coins possible right?

change = 100-itemcost;

int quarters = 0;

int dimes = 0;

int nickels = 0;

//this one subtracts 25 as much as it can. for example if change is 55 cents, it will go through this loop twice,

// thus 2 quarters. and change is down to 5 cents

while(change - 25 >= 0)

{

change = change - 25;

quarter++;

}

//using the above example 5 - 10 is less than 0 so it just skips this loop

while(change - 10 >= 0)

{

change = change - 10;

dime++;

}

//5 - 5 is 0, great! runs through this loop once

while(change - 5 >= 0)

{

change = change - 5;

nickel++;

}

//thus the change will be 2 quarters and 1 nickle

everything else is just printing that info out!

edit whoops subtracted 25 in the dimes section :p

Edited by ekw
Link to comment
Share on other sites

  • 0
This is a good place to use modulus (%). It returns the remainder of a division.

int Quarters = (Change / 25);
int Dimes = (Change % 25 / 10);
int Nickels = (Change % 25 % 10 / 5);

haha win

totally beats my halfass solution

Link to comment
Share on other sites

  • 0
This is a good place to use modulus (%). It returns the remainder of a division.

int Quarters = (Change / 25);
int Dimes = (Change % 25 / 10);
int Nickels = (Change % 25 % 10 / 5);

That implementation is brilliant.

Link to comment
Share on other sites

  • 0
This is a good place to use modulus (%). It returns the remainder of a division.

int Quarters = (Change / 25);
int Dimes = (Change % 25 / 10);
int Nickels = (Change % 25 % 10 / 5);

That is going to save SO much time in my next assignment. Thank you very much!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.