Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: What is wrong with my code? JAVA-Eclipse

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code? JAVA-Eclipse

    import java.util.Random;
     
    public class Assignment_5_1 {
     
    	public int getDice(){
    		Random r = new Random();
    		return r.nextInt(6)+1;
    	}
    }
     
    	public class Craps {
     
     
    			public int getDice(){
    				Random r = new Random();
    				return r.nextInt(6)+1;
    			}
     
    			public static void main(String[] args) {
     
    				Dice dice1 = new Dice();
    				Dice dice2 = new Dice();
     
    				int sum;
    				int d1,d2;
     
    				d1=dice1.getDice();
    				d2=dice2.getDice();
     
    				sum = d1+d2;
    				boolean game = true;
    				System.out.println("You rolled "+d1+"+"+d2+"="+sum);
    				if ((sum==2)||(sum==3)||(sum==12)) {
    					game = false;
    					System.out.println("You lose");
    				} else
     
    				if ((sum==7)||(sum==11)){
    					game = false;
    					System.out.println("You win");
    				} else System.out.println("point is "+sum);
     
    				int newSum;
    				while (game){
    					d1 = dice1.getDice();
    					d2 = dice2.getDice();
    					newSum = d1+d2;
    					System.out.println("You rolled "+d1+"+"+d2+"="+newSum);
    					if (newSum==sum){
    						game = false;
    						System.out.println("You win");
    					} else
    					if (newSum==7){
    						game =false;
    						System.out.println("You lose");
    					} else {
    						System.out.println("point is "+sum);
    					}
    				}
    			}
    		}


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? JAVA-Eclipse

    (Game: craps) Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows: Use a class to define the method getDice, and make sure that the method is overloadable. Roll two dice. Each die has six faces representing values 1, 2, …, and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (that is, 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win.

    Your program acts as a single player. Here are some sample runs.

    You rolled 5 + 6 = 11
    You win

    You rolled 1 + 2 = 3
    You lose

    You rolled 4 + 4 = 8
    point is 8
    You rolled 6 + 2 = 8
    You win

    You rolled 3 + 2 = 5
    point is 5
    You rolled 2 + 5 = 7
    You lose

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: What is wrong with my code? JAVA-Eclipse

    Is there a question associated with this?

    No one is going to compile your code then attempt to work out if it meets your assignments specification or not.
    It's good that you have attempted this code. Now can you please tell us exactly where you are stuck. Pinpoint your issues and give us some descriptive information regarding your problem.

    You need to break it down so we can help you take this one step at a time.
    The easier you make it for the people reading this thread, the better your responses will be.

    Note: Please don't post the same thread twice. Your other thread has been removed.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? JAVA-Eclipse

    When I enter this code in Eclipse I get and error at these two lines

    Dice dice1 = new Dice();
    Dice dice2 = new Dice();

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with my code? JAVA-Eclipse

    Quote Originally Posted by bespinoz View Post
    When I enter this code in Eclipse I get and error
    Well that's a shame. I guess you now have to fix it. Since we do not read minds and have no idea what the error is, nobody can help you.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? JAVA-Eclipse

    You can try this code!
    //Filename: CrapsGame.java

    import java.util.*;

    public class CrapsGame
    {
    public static void main(String[]args)
    {
    int die1,die2;

    die1 = (int)(Math.random()*6 + 1);
    die2 = (int)(Math.random()*6 + 1);

    int marker = die1 +die2;
    System.out.println("Initial roll " + marker);

    if(marker == 7 || marker ==11)
    {
    System.out.println(" You win! ");
    }
    else if(marker == 2|| marker ==3 || marker ==12)
    {
    System.out.println("Craps! You lose.");
    }
    else
    {
    int roll;
    // int r = 0;
    // int[] s = new int[5];
    Vector dice = new Vector(5,3);

    do
    {
    die1 = (int)(Math.random()*6 + 1);
    die2 = (int)(Math.random()*6 + 1);
    roll = die1 + die2;
    dice.addElement(new Integer(roll));


    }while(roll != marker && roll != 7);

    System.out.println();

    System.out.println("You diced" + " " + dice.size() + " times");
    System.out.println();
    System.out.println("You rolled as follow :" + dice);

    System.out.println();
    System.out.println("Capacity of Vector is" + " "+ dice.capacity());
    System.out.println();


    if(roll == marker)
    {
    System.out.println("You win!");
    }
    else
    {
    System.out.println("Craps! You lose!");
    }

    }
    }
    }
    Last edited by bogben; September 10th, 2011 at 07:34 AM.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? JAVA-Eclipse

    The public type Graps must be defined in its own file!

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? JAVA-Eclipse

    Dude,
    You trying to create instance of class that don't exits thatsy it showing error.
    Dice dice1 = new Dice();
    Dice dice2 = new Dice();
    There is no Dice class present .....think about it then proceed ????

    It should be like this to get random number.
    import java.util.Random;

    public class tempp
    {

    public int getDice(){
    int a=(int)(Math.random()*6+2);
    return a;
    }
    }
    class Craps
    {
    public static void main(String[] args) {

    tempp t=new tempp();
    int sum,d1,d2;
    d1=t.getDice();
    //d2=t.getDice();
    System.out.println("You rolled "+d1);
    }
    }

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What is wrong with my code? JAVA-Eclipse

    Quote Originally Posted by Roland1982 View Post
    Here's a working version
    Please read the forum rules and the following:
    http://www.javaprogrammingforums.com...n-feeding.html
    I would also take not of the date of the original post - you have resurrected a year and a half old post to spoonfeed an answer. Please avoid doing this in the future.

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? JAVA-Eclipse

    Sorry

Similar Threads

  1. Replies: 24
    Last Post: August 4th, 2014, 12:49 PM
  2. Java Tip Jul 5, 2010 - [Eclipse IDE] Navigating through code
    By helloworld922 in forum Java JDK & IDE Tutorials
    Replies: 1
    Last Post: July 5th, 2010, 06:28 AM
  3. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 02:00 AM
  4. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 08:38 PM