Help putting my codes into methods

Cant seem to figure out how to put this code into methods.

Page 1 of 1

2 Replies - 528 Views - Last Post: 05 December 2010 - 02:37 PM Rate Topic: -----

#1 sabers012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-December 10

Help putting my codes into methods

Posted 05 December 2010 - 02:15 AM

Hey, I'm currently working on a craps game for school, and im told to use a class with methods. I began writing the code and finished, but i think im doing something wrong with the main and the methods. Below i will post the code for the main. I'll also post the code for the methods. When i run it, it doesnt run the way i want it to run. Can someone please look over it and guide me in fixing it.

Main:
package crapstest;

import java.util.Scanner;
public class Main {
     public static void main(String[] args) {
         rules game = new rules ();
         Scanner reader = new Scanner(System.in);
   
         System.out.println("Press any key to roll dice.");
         reader.next();


    }
}



Below is the code for my methods(this maybe incorrect syntax for it, please help me syntax it properly)

package crapstest;


public class rules {



public boolean getRoll(){
         int d1 = (int)(Math.random()* 6 + 1);
         int d2 = (int)(Math.random()* 6 + 1);
         int roll = d1 + d2;
         int roll2 = 0;
         boolean craps = false;
         
      //Winning Roll
      if(roll == 7||roll == 11){
          System.out.println("You Win!");
        }
     //Losing Roll
    else if(roll == 2 || roll == 3 || roll == 12){
        System.out.println("Sorry, you lost!");
    }        
         do{
          int  die1 = (int)(Math.random()* 6 + 1);
          int  die2 = (int)(Math.random()* 6 + 1);
          int  rolls2 = die1 + die2;
        
             if (roll == rolls2){
                 System.out.println("You win!");
                 craps = true;
                 }
            else if(rolls2 == 7){
                System.out.println("Sorry, You lose!");
                craps = true;
            }
       }

    while (craps != true);

         return true;
    }
   }




It is a craps game so it generates a random number and uses that to determine weather or not it is a win roll or a lose roll. Im kinda stuck if i should put the segment of code in which it generates the number in my main class or the methods class. All help would be greatly appreciated. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Help putting my codes into methods

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Help putting my codes into methods

Posted 05 December 2010 - 05:52 AM

It seems you don't call your getRoll method.
To call it simply use that syntax:
game.getRoll();

inside the main method of the Main class.
The method returns a boolean value to indicate if the player won, so you'll have to check that returned value and print a message accordingly.
package crapstest;

import java.util.Scanner;
public class Main {
     public static void main(String[] args) {
         rules game = new rules ();
         Scanner reader = new Scanner(System.in);
   
         System.out.println("Press any key to roll dice.");
         reader.next();
         boolean won = game.getRoll();
         if(won == true){
           System.out.println("You won");
         }
         else {
           System.out.println("You lost");
         }
    }
}


as for:

Quote

Below is the code for my methods(this maybe incorrect syntax for it, please help me syntax it properly)

syntax error will usually give you compile errors, do you recieve any error messages? if yes, please post them.
Was This Post Helpful? 0
  • +
  • -

#3 sabers012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-December 10

Re: Help putting my codes into methods

Posted 05 December 2010 - 02:37 PM

For my methods class i dont have any errors, im just wondering if i wrote it correctly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1