• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java help(homework- specific question)

nododgy

n00b
Joined
May 22, 2008
Messages
30
PROBLEM SOLVED. STUPID MISTAKE.


I hate to ask on here, but Im completely stumped. I have to alter a Craps program to return a value of 1 or -1 depending on a win or a loss. The issue I'm having is with the method that has to return the value: play(). It was set to void initially, but I changed it to int. I've been trying to get it so that I can add the return to an int bankroll in the Main of a separate .java file. However, Netbeans says that an int is required, but void is found. I'm sure this is something really simple I'm overlooking, but my brain is fried and I just can't seem to find the issue.

Side note: I also added in the WIN_BET/LOSE_BET part, it originally only did a S.O.P. saying win or lose.

Craps:
Code:
// Craps.java
// Craps class simulates the dice game craps.
// Adapted from Deitel & Deitel JAVA text

import java.util.Random;

public class Craps 
{
   // create random number generator for use in method rollDice
   private Random randomNumbers = new Random(); 

   // enumeration with constants that represent the game status
   private enum Status { CONTINUE, WON, LOST };

   // constants that represent common rolls of the dice
   private final static int SNAKE_EYES = 2;
   private final static int TREY = 3;
   private final static int SEVEN = 7;
   private final static int YO_LEVEN = 11;
   private final static int BOX_CARS = 12;

   // constant win value based on $1 bets
   private int WIN_BET = 1;
   private int LOSE_BET = -1;

   // plays one game of craps
   public int play()
   {
      int myPoint = 0; // point if no win or loss on first roll
      Status gameStatus; // can contain CONTINUE, WON or LOST

      int sumOfDice = rollDice(); // first roll of the dice

      // determine game status and point based on first roll 
      switch ( sumOfDice ) 
      {
         case SEVEN: // win with 7 on first roll
         case YO_LEVEN: // win with 11 on first roll           
            gameStatus = Status.WON;
            break;
         case SNAKE_EYES: // lose with 2 on first roll
         case TREY: // lose with 3 on first roll
         case BOX_CARS: // lose with 12 on first roll
            gameStatus = Status.LOST;
            break;
         default: // did not win or lose, so remember point         
            gameStatus = Status.CONTINUE; // game is not over
            myPoint = sumOfDice; // remember the point
//temp            System.out.printf( "Point is %d\n", myPoint );
            break; // optional at end of switch
      } // end switch 

      // while game is not complete
      while ( gameStatus == Status.CONTINUE ) // not WON or LOST
      { 
         sumOfDice = rollDice(); // roll dice again

         // determine game status
         if ( sumOfDice == myPoint ) // win by making point
            gameStatus = Status.WON;
         else
            if ( sumOfDice == SEVEN ) // lose by rolling 7 before point
               gameStatus = Status.LOST;
      } // end while 

      // display won or lost message
      if ( gameStatus == Status.WON )
      {
//temp         System.out.println( "Player wins" );
         return WIN_BET;
       }
      else
      {
//temp         System.out.println( "Player loses" );
         return LOSE_BET;
       }

   } // end method play



   // roll dice, calculate sum and display results
   public int rollDice()
   {
      // pick random die values
      int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll
      int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll

      int sum = die1 + die2; // sum of die values

      // display results of this roll
      System.out.printf( "Player rolled %d + %d = %d\n", 
         die1, die2, sum );

      return sum; // return sum of dice
   } // end method rollDice
} // end class Craps

CrapsTest
Code:
package crapstest;


public class CrapsTest
{

    public static void main(String[] args)
    {
        int bankroll = 1000;
        Craps c1 = new Craps();
        
        for (int i = 0; i < 10; i++)
        {
            bankroll += c1.play();


        }
    }

}

Perhaps a better question would be, is the error in CrapsTest? or in how I did the return? I'm not looking for anyone to solve this for me, just a push in the right direction.
 
Last edited:
Oh for fucks sake. I had 2 copies of "Craps.java" and the wrong one was open in Netbeans. PROBLEM SOLVED.
 
Oh for fucks sake. I had 2 copies of "Craps.java" and the wrong one was open in Netbeans. PROBLEM SOLVED.

Haha, I remember working on an assignment for 20 minutes and I was compiling without renaming the output file... I thought I had the correct command line flags, but must have been tired and not paying attention.

I was changing the file, recompiling, and nothing was happening. I was running "gnu.exe" and not "a.exe" which is the default name if you don't do "-o gnu.exe"... so I was compiling to a.exe but running an old broken gnu.exe... I was like... "There is no way this is possible, I commented all that code out but it still runs... there must be a bug with the compiler :confused: " Nope... bug with the stupid user...

This was with GNU compiler... when I figured that out. :eek:
 
Back
Top