GT500FOMOCO
Programmer
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
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