Results 1 to 4 of 4
- 10-31-2011, 03:09 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
having trouble with global variables
i cant seem to get my global variable to work. what am i doing wrong
Java Code:import java.util.Scanner; public class HW1 { /** * global credits used through out the program */ public double Credits = 0.00; public static void main(String[] args) { Scanner input = new Scanner(System.in); String Item[] = {"","Coke","Diet Coke","Orange","Dr Pepper","Caffeine-Free Coke", "Coors","Rum and Coke","Doritos","Crackers","Patch Cable","Coffie","Espresso"}; int item[] = {0,10,10,10,10,10,10,10,10,10,10,0,10}; double price[] = {0,1.00,1.00,1.00,1.00,1.00,1.00,1.00,1.00,1.00,1.00,1.00,1.00}; String out = "Sold Out"; int marker = 1; String Card = "Insert Card"; double CashBox = 0.00; int MenuSelection = 0; System.out.println("-----------------------------------"); System.out.println("Welcome to Shane's Vending Machine"); while( marker <= 12){ if( item[marker] > 0 ){ System.out.printf(marker+". " +Item[marker]+ " $%5.2f\n", price[marker]); marker ++; } else if( item[marker] == 0){ System.out.println(+marker+". "+out); marker ++; } } marker = 1; System.out.printf("Current Credit: $%5.2f\n", getCredits()); System.out.println("Card Balance: "+Card); System.out.println("-----------------------------------"); while ( MenuSelection != 6){ System.out.println("Main Menu"); System.out.println("Deposite Coin: 1"); System.out.println("Insert Card: 2"); System.out.println("Purchase Item: 3"); System.out.println("Cancel: 4"); System.out.println("Service Mode: 5"); System.out.println("End: 6"); MenuSelection = input.nextInt(); switch(MenuSelection){ case 1: DepositeCoin(); break; } } } private static void DepositeCoin() { Scanner input = new Scanner(System.in); int Selection = 0; while( Selection != 5 ){ System.out.println("Insert 1 Dollar: 1"); System.out.println("Insert 1 Quarter: 2"); System.out.println("Insert 1 Dime: 3"); System.out.println("Insert 1 Nickel: 4"); System.out.println("Return to Main Menu: 5"); Selection = input.nextInt(); switch(Selection){ case 1: setCredits(getCredits() + 1.00); break; case 2: setCredits(getCredits() + .25); break; case 3: setCredits(getCredits() + .1); break; case 4: setCredits(getCredits() + .05); break; } System.out.println("amount "+getCredits()); } return Credits(); } /** * @return the Credits */ public double getCredits() { return Credits; } /** * @param Credits the Credits to set */ public void setCredits(double Credits) { this.Credits = Credits; } }
- 10-31-2011, 12:16 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Re: having trouble with global variables
What does "doesn't work" mean, exactly?
- 10-31-2011, 06:17 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
Re: having trouble with global variables
Java Code:private static void DepositeCoin() { Scanner input = new Scanner(System.in); int Selection = 0; while( Selection != 5 ){ System.out.println("Insert 1 Dollar: 1"); System.out.println("Insert 1 Quarter: 2"); System.out.println("Insert 1 Dime: 3"); System.out.println("Insert 1 Nickel: 4"); System.out.println("Return to Main Menu: 5"); Selection = input.nextInt(); switch(Selection){ case 1: setCredits(getCredits() + 1.00); break; case 2: setCredits(getCredits() + .25); break; case 3: setCredits(getCredits() + .1); break; case 4: setCredits(getCredits() + .05); break; } System.out.println("amount "+getCredits()); } return Credits(); }
First problem i noticed was with this. Give this a look. Hint: The problem is solvable at the very start of the method, or at the very end.
- 10-31-2011, 06:22 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: having trouble with global variables
Java does not have global variables per se. The closes thing to them are constants, which are static and final, and probably not what you need. If you aren't going to go full oop, be cautious about using instance variables as 'global' variables.
Similar Threads
-
Global variables
By nikkka in forum New To JavaReplies: 6Last Post: 03-16-2011, 09:10 AM -
1 of 8 global variables not visible
By Bobbo in forum New To JavaReplies: 7Last Post: 12-02-2010, 05:07 AM -
global variables
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-08-2009, 07:19 PM -
Declaring global variables
By eva in forum New To JavaReplies: 3Last Post: 12-23-2007, 12:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks