methods - vending machine calling a variable :: java -
methods - vending machine calling a variable :: java -
hey i'm writing console app in java vending machine. have have 2 vend() methods , insertmoney() method. right have insertmoney() method , i'm having problem calling it. error when runs through can't variable 'credit' update amount entered user.
bundle javaapplication3; import java.util.scanner; public class candymachine { scanner getinput = new scanner(system.in); //variables money static int quarters; static int dollars; static int dimes; static int nickels; public static int credit; //variables stock of candy static int twix = 10; static int snickers = 5; static int skittles = 8; public static void main(string[] args) { scanner input = new scanner(system.in); while (true) { system.out.println("*******************************************"); system.out.println("vending options follows..."); system.out.println("0: come in money: "); system.out.println("1: twix 2.00 dollars"); system.out.println("2 snickers 1.00 dollars"); system.out.println("3 skittles 2.50 dollars"); system.out.println("4 homecoming change"); system.out.println("*********************************************"); int userselection = input.nextint(); switch (userselection) { case 0: insertmoney(); break; case 1: system.out.println("enter 2.00 dollars please"); if (credit >= 200) { twix -= 1; } else { system.out.println("enter more money"); } break; case 2: system.out.println("eneter 1.00 dollar please"); if (credit >= 200) { snickers -= 1; } else { system.out.println("enter more money"); } break; case 3: system.out.println("enter 2.50 dollars please"); if (credit >= 250) { skittles -= 1; } else { system.out.println("enter more money"); } break; case 4: if (credit > 0) { system.out.println("hack me money :) "); } break; default: system.out.println("please come in right number anarchist."); break; } } } public static void insertmoney() { int moneyselection; scanner getinput = new scanner(system.in); { system.out.println("1: come in quarters"); system.out.println("2: come in dimes"); system.out.println("3: come in nickels"); system.out.println("4: come in dollars"); system.out.println("5: when money entered"); moneyselection = getinput.nextint(); switch (moneyselection) { case 1: system.out.println("how many quarters? "); int userquarters = getinput.nextint(); credit = quarters * 25; //break; case 2: system.out.println("how many dimes?"); int userdimes = getinput.nextint(); credit = dimes * 10; //break; case 3: system.out.println("how many nickels?"); int usernickels = getinput.nextint(); credit = nickels * 5; //break; case 4: system.out.println("how many dollars?"); int userdollar = getinput.nextint(); credit = credit * 100; //break; case 5: system.out.println("thank you"); break; } //return credit; } while (moneyselection != 5); }
}
you error because insertmoney() not static , phone call without specifying instance. can either create static or phone call referring instance of class e.g.
input.insertmoney()
everything static within class exists 1 time in application. non-static exists 1 time per instance of class. have take if want class used in static way or not. main method of application entry point of application , there must 1 of them must static.
i note first line of main method is:
scanner input = new scanner(system.in);
this creates instance of class. if want static not need line not need instance phone call static functions or access static variables. if create instance access each function writing instancename.function e.g.
int userselection = input.nextint();
this way of doing uses specific instance of scanner. every variable within each instance separate if had following:
scanner input1 = new scanner(system.in); scanner input2 = new scanner(system.in); scanner input3 = new scanner(system.in);
then can call:
int userselection = input1.nextint(); int userselection = input2.nextint(); int userselection = input3.nextint();
and values of internal variables (quarters, dollars, etc.) can different in each instance. useful if wanted model multiple vending machines. (they can hold different coins , have different stock levels)
the normal way of doing have every variable , method in class non-static. (apart main function). makes code more expandable in future.
java methods
Comments
Post a Comment