Skip to main content
gamedev.net
🔒 Locked 🎲 GameMaker

Trouble with a simple program - java

Started by DribbleDribble Nov 23, 2010 at 6:21 PM 5 replies 1.1k views
Original Post
DribbleDribble
DribbleDribble
HEllo, im having trouble with a part that should probably be pretty simple.

The program should take the users input, then output the amount in change is left over, and how many of each coin will be dispensed as change (cannot output dollors, if its a dollor it should be represented as 4 quarters)


Here is my code thus far:

   import java.util.*;   import java.io.*;	    public class VendMach   {       public static void main(String [] args)      {         int   chooseProduct;         int	moneyInput;         int	calculateChange;                  }   	       public static void chooseProduct()      {         Scanner scanner = new Scanner(System.in);         System.out.println("");         System.out.println("Please enter your choice - 100: Coke, 110: Diet Coke, 120: Sprite, 130: Diet Sprite, 140: Milk, 150: Water ");         System.out.print("Your choice: ");         Global.choice = scanner.nextInt();      	      	         switch(Global.choice){            case 100:            case 110:               Global.price = 1.25;               break;            case 120:            case 130:               Global.price = 1.75;               break;            case 140:               Global.price = 1.15;               break;            case 150:               Global.price = 0.55;               break;            default: System.out.println("Please try again.");               chooseProduct();                  }            }       public static void moneyInput()      {         Scanner scanner = new Scanner(System.in);               System.out.println("Please deposit money:");         System.out.print("Number of $1.00 bils: ");         Global.numDollar = scanner.nextInt();         System.out.print("Number of Quarters: ");         Global.numQuart = scanner.nextInt();         System.out.print("Number of Dimes: ");         Global.numDimes = scanner.nextInt();         System.out.print("Number of Nickels: ");         Global.numNickel = scanner.nextInt();            }       public static void calculateChange()      {         double change = (((Global.numDollar * Global.Dollar) + (Global.numQuart * Global.Quart) + (Global.numDimes * Global.Dimes) + (Global.numNickel * Global.Nickel)) - Global.price);               System.out.println("Your change is: $" + change);            int amount = (int) (100 * (change - Global.price));				Global.numQuart = amount /25;				amount -= Global.numQuart * 25;				Global.numDimes = amount / 10;				amount -= Global.numDimes * 10;				Global.numNickel = amount / 5;				                     System.out.println("Please recieve " + Global.numQuart + " quarters " + Global.numDimes + " Dimes " + Global.numNickel + " Nickels.");                                 if (Global.choice == 100)         {            System.out.println("Your Coke is vending.");         }         else if (Global.choice == 110)         {            System.out.println("Your Diet Coke is vending.");         }         else if (Global.choice == 120)         {            System.out.println("Your Sprite is vending.");         }         else if (Global.choice == 130)         {            System.out.println("Your Diet Sprite is vending.");         }         else if (Global.choice == 140)         {            System.out.println("Your Milk is vending.");         }         else if (Global.choice == 150)         {            System.out.println("Your Water is vending.");         }         else         {                  }      }   }//----- end of methods program/main program//-------- this is the program that calls on these methods    public class vendTes   {       public static void main(String [] args)      {      		VendMach mach1 = new VendMach();				mach1.chooseProduct();		mach1.moneyInput();		mach1.calculateChange();            }   }


[Edited by - Zahlman on November 24, 2010 7:04:17 AM]
DribbleDribble
DribbleDribble
sorry for the double post, but basically I need help with the
calculateChange() method.
VildNinja
VildNinja
Where exactly does it fail - what is the output? and can i see the Global class.
use source tags! otherwise it is hard to read.
Zahlman
Zahlman
Moved to For Beginners.
boogyman19946
boogyman19946
Your program doesn't do anything. It just initialized 3 variables and quits. If you want the functions to run, you need to call them at some point.
Yo dawg, don't even trip.
JoeCooper
JoeCooper
It starts in main and when it reaches the end of main, it exits.
fireside7
fireside7
Actually there are two static main functions in the code provided. The one that you use to call the methods at the end is the only one that should be there. Also, always post the error that was given when you post code, unless it's a logic error, then give specifically what is happening as opposed to what you want to happen.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.