Results 1 to 4 of 4
  1. #1
    cloutier172 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default 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;
        }
            }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: having trouble with global variables

    What does "doesn't work" mean, exactly?

  3. #3
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default 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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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

  1. Global variables
    By nikkka in forum New To Java
    Replies: 6
    Last Post: 03-16-2011, 09:10 AM
  2. 1 of 8 global variables not visible
    By Bobbo in forum New To Java
    Replies: 7
    Last Post: 12-02-2010, 05:07 AM
  3. global variables
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-08-2009, 07:19 PM
  4. Declaring global variables
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 12:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •