Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with homework... 1

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
US
I have always had problems with the % operator. I have to write a program that uses it to determin if a number entered is a multiple of 5. Here is my code:

public class Tasty {

public static void main(String[] args) {
ConsoleReader keyboard = new ConsoleReader(System.in);

String menu = "Number Item Price"
+ "\n-------------------------"
+ "\n [1] Snickers 65"
+ "\n [2] Chips 45"
+ "\n [3] Donuts 85"
+ "\n [4] Crackers 50"
+ "\n [5] Kit Kat 65"
+ "\n [6] Gum 35"
+ "\n\nEnter an item number: ";

System.out.print("Welcome to Tasty Treats vending machine."
+ "\n\nDeposit some money: ");
String test = keyboard.readLine();
int pay = Integer.parseInt(test);

if (test.substring(test.length() - 1, test.length()).equals("0")) {
System.out.print(menu);
int choice = keyboard.readInt();

if (choice == 1) {
if (pay >= 65) {
System.out.print("\nHere's your Tasty Snickers and "
+ (pay - 65) + " cents in change. "
+ "\nGood-bye from Tasty Treats!");
}

else {
System.out.print("\nYou have not entered enough "
+ "money for Tasty Snickers.\nI am returning your "
+ pay + " cents.\nGood-bye from Tasty Treats!");
}
}

if (choice == 2) {
if (pay >= 45) {
System.out.print("\nHere's your Tasty Chips and "
+ (pay - 45) + " cents in change. "
+ "\nGood-bye from Tasty Treats!");
}

else {
System.out.print("\nYou have not entered enough "
+ "money for Tasty Chips.\nI am returning your "
+ pay + " cents.\nGood-bye from Tasty Treats!");
}
}

if (choice == 3) {
if (pay >= 85) {
System.out.print("\nHere's your Tasty Donuts and "
+ (pay - 85) + " cents in change. "
+ "\nGood-bye from Tasty Treats!");
}

else {
System.out.print("\nYou have not entered enough "
+ "money for Tasty Donuts.\nI am returning your "
+ pay + " cents.\nGood-bye from Tasty Treats!");
}
}

if (choice == 4) {
if (pay >= 50) {
System.out.print("\nHere's your Tasty Crackers and "
+ (pay - 50) + " cents in change. "
+ "\nGood-bye from Tasty Treats!");
}

else {
System.out.print("\nYou have not entered enough "
+ "money for Tasty Crackers.\nI am returning your "
+ pay + " cents.\nGood-bye from Tasty Treats!");
}
}

if (choice == 5) {
if (pay >= 45) {
System.out.print("\nHere's your Tasty Kit Kat and "
+ (pay - 45) + " cents in change. "
+ "\nGood-bye from Tasty Treats!");
}

else {
System.out.print("\nYou have not entered enough "
+ "money for Tasty Kit Kat.\nI am returning your "
+ pay + " cents.\nGood-bye from Tasty Treats!");
}
}

if (choice == 6) {
if (pay >= 35) {
System.out.print("\nHere's your Tasty Gum and "
+ (pay - 35) + " cents in change. "
+ "\nGood-bye from Tasty Treats!");
}

else {
System.out.print("\nYou have not entered enough "
+ "money for Tasty Gum.\nI am returning your "
+ pay + " cents.\nGood-bye from Tasty Treats!");
}
}
}

else {
System.out.print("\nSorry, I cannot accept pennies."
+ "\nI am returning your " + pay + " cents."
+ "\nGood-bye from Tasty Treats!\n\n\n");
}
}
}


Please help me. Also, I'd perfer that you'd do so without directly giving me the answer. It would help my comprehension of the material. :) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Modulus (%) is easy. It just returns the remainder of a division operation. Remember remainders? Those things everybody hated in the third grade because they screwed up what was sure to be an easy calculation.

Simply put, to determine if a number is a multiple of 5 all you need to do is to get the number modulus 5. If the resulting remainder is 0 then it is a multiple, if not then no. Simple example:
Code:
int x = 12;
if (x%5 == 0) {
  /* Multiple of 5 */
}
else {
  /* Not a multiple of 5 */
}
I didn't look at the rest of the code so I am not going to comment on it.
 
Thanks, whatever idiot tried to answer my question at the Sun forum just confused me. Thanks for the help. :) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top