Answer to Question #20405 in Java | JSP | JSF for shalenee

Question #20405
Write a simple two players dice game. Each player will roll the dice twice and the player with the highest score wins the game.
1
Expert's answer
2012-12-11T11:04:59-0500

import java.util.Random;
import java.util.Scanner;


public class DiceGameForTwoPlayer {

private static int totalsum1=0;
private static int totalsum2=0;
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
int ch=0;
for(int i=0;i<2;i++){
System.out.print("Roll player 1 (press 1): ");
ch=input.nextInt();
totalsum1+=roll();
System.out.print("Roll player 2 (press 2): ");
ch=input.nextInt();
totalsum2+=roll();
}
if(totalsum1>totalsum2){
System.out.print("Player 1 won");
}else if(totalsum1<totalsum2){
System.out.print("Player 2 won");
}else{
System.out.print("Draw");
}
}
private static int roll(){
int numberRolled;
Random r1 = new Random();
numberRolled =(int)(6*r1.nextDouble() + 1);
return numberRolled;
}
}

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 the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS