What is wrong with my code? JAVA-Eclipse
Code Java:
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);
}
}
}
}
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
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.
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();
Re: What is wrong with my code? JAVA-Eclipse
Quote:
Originally Posted by
bespinoz
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.
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!");
}
}
}
}
Re: What is wrong with my code? JAVA-Eclipse
The public type Graps must be defined in its own file!
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);
}
}
Re: What is wrong with my code? JAVA-Eclipse
Quote:
Originally Posted by
Roland1982
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.
Re: What is wrong with my code? JAVA-Eclipse