+ 2

Cannot make a static reference to a non static method

So I'm trying to make a slot machine type game, using a do while loop to get input to reset the code. This is the code: package programlearning; import java.util.Scanner; import java.awt.Choice; import java.util.Random; public class SlotmachineSimulator { private static Scanner Money; private static Random Reward; private static Scanner yn; public static void main(String[] args) { Money = new Scanner(System.in); yn = new Scanner(System.in); System.out.println("You start with one hundred dollars"); do{ System.out.println("How much money do you want to put in"); double Starting = 100; double input = Money.nextDouble(); Starting = Starting - input; Reward = new Random(6); int x = Reward.nextInt(); if(x == 3){ input *= 10; System.out.println("Jackpot! Ten times what you put in! You now have" +input + Starting); System.out.println("Do you want to continue?"); yn = new Scanner(System.in); String Choice = yn.nextLine(); } else{ System.out.println("You lose, you now have" + Starting); System.out.println("Do you want to continue?"); String Choice = yn.nextLine(); } }while(Choice.equals("Y")); } } what have I done wrong and how can I fix it?

3rd Sep 2016, 3:18 AM
Adam Lang
Adam Lang - avatar
2 odpowiedzi
+ 1
first of all you should write all variables with lower case like starting, choice, reward... you code will be then easier to read. you also dont need to have 2 scanners. you just need one to read from the command line, maybe rename the "Money" scanner to something more neutral like inputScanner or just scanner. then call inputScanner.nextDouble for the money and inputScanner.nextLine() for the yes no part. Your problem is the awt.Choice import. remove it(it doesnt have something to do with your String it is another class). the while method will then tell you it doesnt now thr choice variable thats because you declared it inside the if statement but you have to declared it outside. you missunderstood the new random() it sets a seed but the nextInt will return a int between -2^32 and 2^32 -1. pretty big huh so you will maybe never win. you have to use nextInt(6) to get a number between 0-5. also check if the player still has enough money to bet
11th Sep 2016, 12:53 AM
Tucura
Tucura - avatar
+ 1
your code should look like this import java.util.Scanner; import java.util.Random; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int startingMoney = 100; String choice = "y"; do{ int betMoney = scanner.nextInt(); if(betMoney <= startingMoney) startingMoney -= betMoney; else System.exit(0); int r = random.nextInt(6); if(r==3){ startingMoney = betMoney * 10; System.out.println("you won! money left:" + startingMoney); } else{ System.out.println("you lost! money left:" + startingMoney); } System.out.println("again?"); scanner.nextLine(); if(scanner.hasNextLine()) choice = scanner.nextLine(); } while(startingMoney > 0 && choice.equals("y")); System.out.println("You dont have money left or you didnt say y :( bye"); } }
11th Sep 2016, 1:35 AM
Tucura
Tucura - avatar