You don't need BigDecimals if you define a penny as a monetary unit (i.e. a penny is worth 'one' and a dollar is worth 'hundred').
kind regards,
Jos
Printable View
Did some reading, this code compiles but when I test it, it gives me the wrong answer :(
Code:userInput = JOptionPane.showInputDialog("How many quarters are there?");
double quarter = Double.parseDouble(userInput);
BigDecimal q = new BigDecimal("4");
BigDecimal d6 = new BigDecimal(quarter);
BigDecimal d2 = d6.divide(q, 2, BigDecimal.ROUND_HALF_UP);
double quarters = d2.doubleValue();
If you want get a user input from a user, you will have to import the 'Scanner' utility by:
and then by creating a scanner:Code:import java.util.Scanner;
and then putting the user input in a variable:Code:// input is the new scanner name and you can name it whatever you prefer
Scanner input = new Scanner(System.in);
This is the most simplest way I know how you can get inputs from users. Peace&CheersCode:// for example the user entry is an int and the variable name is number
// nextInt(); refers to the user entry after the 'enter' key is pressed.
int number = input.nextInt();
Does this work independent of the command line? I was using JOptionPane so I could make an app...
What results are you getting from that?
You might find it easier to go Jos' route of working in cents up until you give the final answer.
Skip the BigDecimal entirely.
So the above would be something like:
Code:userInput = JOptionPane.showInputDialog("How many quarters are there?");
int quarters = Integer.parseInt(userInput);
int quartersAsCents = quarters * 25;
it gives me what I input...no calculations are done to it...
The following (which is the same as your code above, but with a fixed value for 'quarter'):
Gives the expected 0.25 answer.Code:public static void main(String args[]) {
double quarter = 1.0;
BigDecimal q = new BigDecimal("4");
BigDecimal d6 = new BigDecimal(quarter);
BigDecimal d2 = d6.divide(q, 2, BigDecimal.ROUND_HALF_UP);
double quarters = d2.doubleValue();
System.out.println(quarters);
}
So I don't know what else is up with your code, because that bit works.
Did away with BigDecimal and went with the suggested method. Got it working. Now, how do I get all of this on one JOptionPane?
Code:JOptionPane.showMessageDialog(null, "Money in hundreds: $" + hundred);
JOptionPane.showMessageDialog(null, "Money in fifties: $" + fifty);
JOptionPane.showMessageDialog(null, "Money in twenties: $" + twenty);
JOptionPane.showMessageDialog(null, "Money in tens: $" + ten);
JOptionPane.showMessageDialog(null, "Money in fives: $" + five);
JOptionPane.showMessageDialog(null, "Money in ones: $" + one);
JOptionPane.showMessageDialog(null, "Money in quarters: $" + quarter);
JOptionPane.showMessageDialog(null, "Money in dimes: $" + dime);
JOptionPane.showMessageDialog(null, "Money in nickels: $" + nickel);
JOptionPane.showMessageDialog(null, "Money in pennies: $" + penny);
Here's a hint: JOptionPane handles a String[] array (or any other array) really well.
db
Alright! I got it working. What would be the best way to make it tell me how many of each bill to take? I don't need syntax, just an alg.
Also, how do I make it an app that will run on a computer without java jdk?