CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Posts
    0

    HELP please! I'm dying here...

    I'm trying to build a simple roulette game for an assignment. but i keep getting this stupid error :
    E:\RouletteGame.java:32: non-static variable this cannot be referenced from a static context
    Player player1 = new Player(name, iAmount);

    i have, for the love of me no idea how to solve this... Help please

    This is my code:

    import java.util.Scanner;

    public class RouletteGame
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    int num = (int)(Math.random()*37); //simulation of roulette wheel
    //int winnings = 0; //assumed test data for testing without player class
    //int bet = 10;
    int x = 0, choice, counter = 1, p = 1; //random variables for looping purposes
    int InOrOut; //choice for either inside bets or outside bets
    String name; //name of player
    int iAmount; //Initial Amount player brings to table

    System.out.println("Welcome to the Nick's Roulette game!");

    System.out.print("Please enter your name : ");
    name = scan.nextLine();

    //Validation for correct amount of initial amount
    while (p == 1) {
    System.out.print("\nHow much are you brigning to the table? ");
    iAmount = scan.nextInt();

    if (iAmount > 0)
    p++;
    else System.out.println("Please enter valid value!!");
    }

    //player contructor
    Player player1 = new Player(name, iAmount);

    //loop for the whole game so that it repeats when it ends
    while (counter == 1) {

    //Initial bet placing menu
    System.out.println("Would you like to place an inside bet or an ourside bet?");
    System.out.println("1. Inside bet");
    System.out.println("2. Outside bet");

    x = 0;//random variable for while looping
    //junction
    while (x == 0){ //looping of menu
    System.out.print("Please enter choice : ");
    InOrOut = scan.nextInt();
    if(InOrOut == 1 || InOrOut == 2) //validation of choice in tier 1
    {
    x = 1; //increment of x (because input data is correct) so the loop stops
    if(InOrOut == 1){ //inside bet choice
    System.out.println("You have chosen to place an inside bet.");
    insideBetDisplay(winnings, bet);

    }
    else if(InOrOut == 2){ //outside bet choice
    System.out.println("You have chosen to place an outside bet.");
    outsideBetDisplay(winnings, bet);
    }
    }
    else {System.out.println("Please enter valid selection"); //error message if choice entered is invalid
    }
    }

    System.out.println("\nWould you like to play again?");
    System.out.println("1. Yes");
    System.out.println("2. No");
    System.out.print("Please enter choice : ");
    choice = scan.nextInt();

    if (choice == 2)
    counter ++;
    //System.out.println(counter);
    }
    System.out.println(); //for beautification pureposes
    }

    public static void insideBetDisplay(int winnings, int bet) //*********************
    { //Inside bet method
    //*********************
    int num = (int)(Math.random()*37);
    Scanner scan1 = new Scanner(System.in);
    int numIn;

    System.out.println();
    System.out.println("Available inside bets : ");
    System.out.println("\tNumbers 0 to 36 - Payout [35 to 1]");
    System.out.print("\tPlease enter number of choice : ");

    numIn = scan1.nextInt();

    if(numIn < 0 || numIn > 36){
    System.out.println("Please enter valid number.");
    insideBetDisplay(winnings, bet);
    }

    if (numIn == num){
    System.out.println("Congratulations!! You Won!!");
    bet = bet * 35;
    System.out.println("You won " + bet + "!!");
    winnings = winnings + bet;
    System.out.println("Your current money in hand is " + winnings);
    }
    else {
    System.out.println("Sorry... You lost\n");
    }

    } //end of inside bet method

    public static void outsideBetDisplay(int winnings, int bet) //******************
    { //Outside bet method
    int num = (int)(Math.random()*37); //******************
    Scanner scan2 = new Scanner(System.in);
    int numOut;


    //display menu for ourside bets
    System.out.println();
    System.out.println("Available outside bets : ");
    System.out.println("\t1. 1st Dozen (1 to 12) - Payout [3 to 1]");
    System.out.println("\t2. 2nd Dozen (13 to 24) - Payout [3 to 1]");
    System.out.println("\t3. 3rd Dozen (25 to 36) - Payout [3 to 1]");
    System.out.println("\t4. Low Half (1 to 18) - Payout [2 to 1]");
    System.out.println("\t5. High Half (19 to 36) - Payout [2 to 1]");
    System.out.println("\t6. Blacks only - Payout [1 to 1]");
    System.out.println("\t7. Reds only - Payout [1 to 1]");
    System.out.println("\t8. Even numbers - Payout [1 to 1]");
    System.out.println("\t9. Odd numbers - Payout [1 to 1]");
    System.out.print("Please enter choice : ");

    //accepts user choice
    numOut = scan2.nextInt();

    //validation of chioce entered
    if(numOut < 1 || numOut > 9){
    System.out.println("Please enter valid choice.");
    outsideBetDisplay(winnings, bet);
    }
    System.out.println("The number rolled is " + num);

    //first dozen
    if (numOut == 1){
    if(num >= 1 && num <= 12){
    System.out.println("Congratulations! you won!\n");
    bet = bet * 3;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    //second dozen
    else if (numOut == 2){
    if (num >= 13 && num <= 24){
    System.out.println("Congratulations! you won!\n");
    bet = bet * 3;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    //third dozen
    else if (numOut == 3){
    if (num >= 25 && num <= 36){
    System.out.println("Congratulations! you won!\n");
    bet =
    bet * 3;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    //low half
    else if (numOut == 4){
    if (num >= 1 && num <= 18){
    System.out.println("Congratulations! you won!\n");
    bet = bet * 2;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    //high half
    else if (numOut == 5){
    if (num >= 19 && num <= 36){
    System.out.println("Congratulations! you won!\n");
    bet = bet * 2;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    //all red
    else if (numOut == 6){
    if (num==2 || num==4 || num==6 || num==8 || num==10 || num==11 || num==13 || num==15 || num==17 || num==20
    || num==22 || num==24 || num==26 || num==28 || num==29 || num==31 ||num==33 || num==35)
    {
    System.out.println("Congratulations! you won!\n");
    bet = bet * 2;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    //all black
    else if (numOut == 7){
    if (num==1 || num==3 || num==5 || num==7 || num==9 || num==12 || num==14 || num==16 || num==18 || num==19
    || num==21 || num==23 || num==25 || num==27 || num==30 || num==32 ||num==34 || num==36)
    {
    System.out.println("Congratulations! you won!\n");
    bet = bet * 2;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    //even
    else if (numOut == 8){
    if (num != 0 && num % 2 == 0){
    System.out.println("Congratulations! you won!\n");
    bet = bet * 2;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    //odd
    else if (numOut == 9){
    if (num != 0 && num % 2 == 1){
    System.out.println("Congratulations! you won!\n");
    bet = bet * 2;
    System.out.println("You won " + bet);
    winnings = winnings + bet;
    System.out.println("Your money in hand is " + winnings);

    }
    else System.out.println("Sorry, you have lost.\n");
    }
    } //end of outside bet method


    public class Player
    {

    /* Player's name */
    private String playerName;
    /* Initial amount a player brings to the table */
    private int initialAmount;
    /* Current amount a player has in hand*/
    private int currentAmount;

    public Player(String name, int iAmount)
    {
    playerName = name;
    setInitialAmount(iAmount);
    setCurrentAmount(iAmount);
    }

    public String getPlayerName()
    {
    return playerName;
    }

    public int getInitialAmount()
    {
    return initialAmount;
    }

    public int getCurrentAmount()
    {
    return currentAmount;
    }

    private void setInitialAmount(int amt)
    {
    if(amt >= 0)
    initialAmount = amt;
    else
    initialAmount = 0;
    }

    public void setCurrentAmount(int amt)
    {
    if(amt >= 0)
    currentAmount = amt;
    else
    currentAmount = 0;
    }

    public int calculateAmountDifference()
    {
    return currentAmount - initialAmount;
    }

    }

    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: HELP please! I'm dying here...

    The error message means what it says - you are trying to access a non-static (instance) variable from a static method, which can't be done. This is happening on line 32 of RouletteGame.java. See Understanding Instance and Class Members.

    If you post readable code (properly formatted between CODE tags), we can be more specific.

    Programs must be written for people to read, and only incidentally for machines to execute...
    H. Abelson and G. Sussman
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Feb 2008
    Posts
    966

    Re: HELP please! I'm dying here...

    First off, you need to post formatted code using the proper code tags.

    Secondly, you haven't posted all of the code. I tried to copy and paste your code into my IDE and it is missing some variables.

    Thirdly, the error says it all: "non-static variable this cannot be referenced from a static context".

    This means that you are trying to use a variable (probably a class level variable) inside of a static method (looks like you are doing most everything in main, which is static). Java is telling you that if you want to access this variable in the static method main, then you either need to declare it in main, or you need to declare it to be static.

    *are we starting this again dlorde*

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: HELP please! I'm dying here...

    Quote Originally Posted by ProgramThis View Post
    *are we starting this again dlorde*
    Groundhog Day... it's deja-vu all over again

    Learning how to learn is life's most important skill...
    T. Buzan
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Apr 2007
    Posts
    425

    Re: HELP please! I'm dying here...

    Quote Originally Posted by dlorde View Post
    Groundhog Day... it's deja-vu all over again

    Learning how to learn is life's most important skill...
    T. Buzan
    It's ok, it only lasts six weeks.
    ------
    If you are satisfied with the responses, add to the user's rep!

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured