|
-
Nov 16th, 2008, 10:10 PM
#1
Thread Starter
New Member
New Java Program
Hi i'm stuck on a program that i'm trying to complete. What I'm trying to do is write a Java program that breaks down money into smaller units. Displaying the non-zero denominations only. Display singular words for single units like 1 dollar and 1 penny, and display plural words for more than one unit like 2 dollars and 3 pennies. (Use 23.67 to test your programs.)
If the user enters zero or a negative amount, your program should exit properly and display a message stating that the amount entered by the user was zero or negative. I almost finish with the program but i'm stuck on trying to get the if else statement for the amount less than or equla to zero here is my program:
// ComputeChange.java: Break down an amount into smaller units
import javax.swing.JOptionPane;
public class ComputeChange {
/** Main method */
public static void main(String[] args) {
double amount; // Amount entered from the keyboard
// Receive the amount entered from the keyboard
String amountString = JOptionPane.showInputDialog(null,
"Enter an amount in double, for example 11.56",
"Example 2.4 Input", JOptionPane.QUESTION_MESSAGE);
// Convert string to double
amount = Double.parseDouble(amountString);
int remainingAmount = (int)(amount * 100);
// Find the number of one dollars
int numOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
// Find the number of quarters in the remaining amount
int numOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
// Find the number of dimes in the remaining amount
int numOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
// Find the number of nickels in the remaining amount
int numOfNickels = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
// Find the number of pennies in the remaining amount
int numOfPennies = remainingAmount;
// If the number is zero or less<-------
if (input ==0); <-------
JOptionPane.showMessageDialog (null, "The amount is zero");<-------
if (input < 0); <-------
JOptionPane.showMessageDialog (null, "The amount is negative");<-------
if (else input > 0 = 23.67); <-------
// Display results
String output = "Your amount " + amount + " consists of \n" +
numOfOneDollars + " dollars\n" +
numOfQuarters + " quarters\n" +
numOfDimes + " dimes\n" +
numOfNickels + " nickel\n" +
numOfPennies + " pennies";
JOptionPane.showMessageDialog(null, output,
"Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE);
}//end main method
}// end Compute Change
I put the arrows is where the problem is. I think I can use the if...else statements but can't figure out where or when to use them.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|