I have this script to simulate 2 die rolls in java. This is done twice, once by user and once by computer(both automatically). The program outputs the rolls and sums them up. I however cannot get an if/else statement to work to compare the rolls and determine the winner/ or tie. So far I have:
import java.util.Scanner;
import java.util.Random;
import java.text.DecimalFormat;
/*
Program to simulate die roll
*/
public class Dice
{
Scanner scan = new Scanner (System.in);
Random generator= new Random();
int roll1;
int roll2;
int roll3;
int roll4;
int addroll1;
int addroll2;
public void userdieroll() // Simulates users role
{
roll1 = (generator.nextInt(7) +1); // Generate number from 1-6
System.out.println("Your first roll is "+roll1+"");// Says users first role
roll2 = (generator.nextInt(7) +1); // Generate number from 1-6
System.out.println("Your second roll is "+roll2+"");// Says users second roll
addroll1= roll1 +roll2;// Sums users roles
System.out.println("The sum of your two roles is "+addroll1+" \n");
}
public void compdieroll()// Simulates computers role
{
roll3 = (generator.nextInt(7) +1); // Generate number from 1-6
System.out.println("The computers first role is "+roll3+""); // Says computers first role
roll4 = (generator.nextInt(7) +1); // Generate number from 1-6
System.out.println("The computers second role is "+roll4+""); // Says computers second role
addroll2= roll3 +roll4;// Sums computers roles
System.out.println("The sum of the computers roles is "+addroll2+"");
}
public void findwinner()
{
if (addroll1 == addroll2)
{
System.out.println("Its a tie!");
}
else
{
if (addroll1 > addroll2)
{
System.out.println("You Won!");
}
else
{
System.out.println("You lost!");
}
}
}
public static void main(String[] args)
{
Dice userroll = new Dice();
userroll.userdieroll();
Dice comproll = new Dice();
comproll.compdieroll();
Dice looper = new Dice();
looper.findwinner();
}
}
roll1 = (generator.nextInt(7) +1); // Generate number from 1-6
, um, no. Generate a number from 0 to 6, then add one, will get you numbers 1 to 7 (inclusive).