I'm very new to java and my knowledge is very limited. Anyways I have to the little following project :
Write a Java program that reads in an integer representing the number of pennies and prints out the best way (i.e. the fewest number of coins) to make change using British coins (50p, 20p, 10p, 5p, 2p and 1p only)1. For example, if the number of pennies entered is 73, then print out:
it must be as simple as possible.I was gonna make a condition, if the input is greater than 50 divide by 50 until it is less than 50p( I could not figure out how to make "UNTIL IT IS LESS THAN50p" part of condition)
then I started the following cod which gives me error in line 20.
Can sombody please tell me why i get this error and how I can do this in a very simple way using modular?
Find out how to use a Scanner for keyboard input. It is much easier than a buffered reader.
You have made the mistake of writing code before you worked out what to do. Start by writing down with a pencil (you will need a rubber to destroy the evidence when you get it wrong) exactly how you would calculate the change. Start with 73p as before then try it with other amounts e.g. 146p 219p (which are multiples of 73). Don't use floating point arithmetic; only use integer arithmetic. Don't use floating point numbers at all. Otherwise things will go wrong if you enter 0.73 and it turns out the nearest floating point number is 0.72999999999999...
You don't appear to have shown the static code with non‑static references in, but I shall guess that it is the main method. What most people do is add static to variables, but that is quite the wrong way to do things. The right way to do things is to move as much code as possible out of the main method.
You cannot use non static fields without instantiating the class. But it is not the main problem you need to solve now.
Read once again CR (Campbell Ritchie) post and concentrate on the technique has been mentioned in it.
saeid jamali wrote:This is my plan but I have problem with the operation part as it only works for 50p changes so far I couldn't add other changes to my result.
A good start, but you are trying to do too much all at once. Forget about get the data. Any lines which look at all like Java® code must be struck out. So lines 5 6 and 13 go. I am sure you know how to get input and print the results anyway, so you can concentrate on the bit causing the problems.
What you are short of is how to convert 73p into 1×50p 1×20p 1×2p and 1×1p. And why there are no 10ps or 5ps in that output. You are simply guessing with %7. Where does 7 come from in the first place? That is the bit you have to work out, and everything else will be easy. Why is there exactly 1×50p? And if we doubled it up to £1.46 why would there be 2×50p?
We have rules about Cross Posting to other places on the internet. Please read our page -> BeForthrightWhenCrossPostingToOtherSites. The short version is, we don't mind if you cross post, but we do ask that you state that you have done so and cite where.