63 066
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #8990 in Java | JSP | JSF for java dice

Question #8990
simple java dice game
Each player starts-out with a cumulative score of zero. Then each player rolls three
dices and a random number appears for each dice. The summation of each number is
added in each player’s score. The winner is the player that has the highest score in x
turns based on what the players initially decided.
Expert's answer
import java.util.Random;


public class DiceGame {

/**
& * @param args
& */

public static void main(String[] args) {
& // TODO Auto-generated method stub
& Player p1 = new Player("Player 1");
& Player p2 = new Player("Player 2");
& p1.play();
& p2.play();
& if(p1.getScore() > p2.getScore()){
& System.out.println("Player 1 win with: " + p1.getScore());
& } else
& if(p1.getScore() == p2.getScore()){
& System.out.println("Draw");
& } else
& if(p1.getScore() < p2.getScore()){
& System.out.println("Player 2 win with: " + p2.getScore());
& }
&
}

}



import java.util.Random;


public class Player {

private int score = 0;

public String name;

Player(String name){
& this.name = name;
}

private void shuffle(){
& Random rand = new Random();
& int p = 0;
& while(p==0){
& p = rand.nextInt(7);
& }
& score += p;
& System.out.println(name + " scored " + p);
}

public void play(){
& for(int i=0; i<3; i++)
& shuffle();
}

public int getScore(){
& return score;
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions