Jump to content

Hey I'm pretty new to programming and I'm supposed to make a program that simulates the rolling of two die and keeps displaying the output of the die until they add up to 7 or 11. Also, I'm supposed to use a sub routine.
 
Here's what I got... please keep in mind I'm really new and have no idea what I'm doing  :P

public class DiceGame {	public static int Dice1, Dice2, rollTotal;		public static void main(String[] args) 	{				rollDice();				rollTotal = Dice1 + Dice2;				System.out.println("Dice 1: " + Dice1);		System.out.println("Dice 2: " + Dice2);		System.out.println("Total Roll: " + rollTotal);				while (rollTotal != 7 || rollTotal != 11)		{						rollDice();						System.out.println("Dice 1: " + Dice1);			System.out.println("Dice 2: " + Dice2);			System.out.println("Total Roll: " + rollTotal);						rollTotal = Dice1 + Dice2;								}	} 		public static void rollDice()	{		Dice1 = (int)(Math.random()*6)+1;		Dice2 = (int)(Math.random()*6)+1;	}}
Link to comment
https://linustechtips.com/topic/152551-java-programming-help/
Share on other sites

Link to post
Share on other sites

 

Hey I'm pretty new to programming and I'm supposed to make a program that simulates the rolling of two die and keeps displaying the output of the die until they add up to 7 or 11. Also, I'm supposed to use a sub routine.

 

Here's what I got... please keep in mind I'm really new and have no idea what I'm doing  :P

public class DiceGame {	public static int Dice1, Dice2, rollTotal;		public static void main(String[] args) 	{				rollDice();				rollTotal = Dice1 + Dice2;				System.out.println("Dice 1: " + Dice1);		System.out.println("Dice 2: " + Dice2);		System.out.println("Total Roll: " + rollTotal);				while (rollTotal != 7 || rollTotal != 11)		{						rollDice();						System.out.println("Dice 1: " + Dice1);			System.out.println("Dice 2: " + Dice2);                        // Add up before printing the value                         rollTotal = Dice1 + Dice2;			System.out.println("Total Roll: " + rollTotal);											}	} 		public static void rollDice()	{		Dice1 = (int)(Math.random()*6)+1;		Dice2 = (int)(Math.random()*6)+1;	}}

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
https://linustechtips.com/topic/152551-java-programming-help/#findComment-2037517
Share on other sites

Link to post
Share on other sites

 

 

Hey I'm pretty new to programming and I'm supposed to make a program that simulates the rolling of two die and keeps displaying the output of the die until they add up to 7 or 11. Also, I'm supposed to use a sub routine.

 

Here's what I got... please keep in mind I'm really new and have no idea what I'm doing  :P

public class DiceGame {	public static int Dice1, Dice2, rollTotal;		public static void main(String[] args) 	{				rollDice();				rollTotal = Dice1 + Dice2;				System.out.println("Dice 1: " + Dice1);		System.out.println("Dice 2: " + Dice2);		System.out.println("Total Roll: " + rollTotal);				while (rollTotal != 7 || rollTotal != 11)		{						rollDice();						System.out.println("Dice 1: " + Dice1);			System.out.println("Dice 2: " + Dice2);                        // Add up before printing the value                         rollTotal = Dice1 + Dice2;			System.out.println("Total Roll: " + rollTotal);											}	} 		public static void rollDice()	{		Dice1 = (int)(Math.random()*6)+1;		Dice2 = (int)(Math.random()*6)+1;	}}

 

Still doesn't work :/ Its not stopping the program even if the roll adds up to a 7 or 11...

Link to comment
https://linustechtips.com/topic/152551-java-programming-help/#findComment-2037536
Share on other sites

Link to post
Share on other sites

Still doesn't work :/ Its not stopping the program even if the roll adds up to a 7 or 11...

public class DiceGame {	public static int Dice1, Dice2, rollTotal;		public static void main(String[] args) 	{				rollDice();				rollTotal = Dice1 + Dice2;				System.out.println("Dice 1: " + Dice1);		System.out.println("Dice 2: " + Dice2);		System.out.println("Total Roll: " + rollTotal);		               // AND not OR  		while (rollTotal != 7 && rollTotal != 11)		{						rollDice();						System.out.println("Dice 1: " + Dice1);			System.out.println("Dice 2: " + Dice2);                        // Add up before printing the value                         rollTotal = Dice1 + Dice2;			System.out.println("Total Roll: " + rollTotal);											}	} 		public static void rollDice()	{		Dice1 = (int)(Math.random()*6)+1;		Dice2 = (int)(Math.random()*6)+1;	}}

The total roll should NOT be 7 AND NOT be 11

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
https://linustechtips.com/topic/152551-java-programming-help/#findComment-2037539
Share on other sites

Link to post
Share on other sites

public class DiceGame {	public static int Dice1, Dice2, rollTotal;		public static void main(String[] args) 	{				rollDice();				rollTotal = Dice1 + Dice2;				System.out.println("Dice 1: " + Dice1);		System.out.println("Dice 2: " + Dice2);		System.out.println("Total Roll: " + rollTotal);		               // AND not OR  		while (rollTotal != 7 && rollTotal != 11)		{						rollDice();						System.out.println("Dice 1: " + Dice1);			System.out.println("Dice 2: " + Dice2);                        // Add up before printing the value                         rollTotal = Dice1 + Dice2;			System.out.println("Total Roll: " + rollTotal);											}	} 		public static void rollDice()	{		Dice1 = (int)(Math.random()*6)+1;		Dice2 = (int)(Math.random()*6)+1;	}}

The total roll should NOT be 7 AND NOT be 11

 

ooooooh thanks! Don't know why I thought it should be or....

Link to comment
https://linustechtips.com/topic/152551-java-programming-help/#findComment-2037546
Share on other sites

Link to post
Share on other sites

A slightly different solution:

public class DiceGame {		public static void main(String[] args){		int rollTotal = 0;				while (rollTotal != 7 && rollTotal != 11){			rollTotal = rollDice();				}	} 		public static int rollDice(){		int dice1 = (int)(Math.random()*6)+1;		int dice2 = (int)(Math.random()*6)+1;		int rollTotal = Dice1 + Dice2;		System.out.println("Dice 1: " + Dice1);		System.out.println("Dice 2: " + Dice2);		System.out.println("Total Roll: " + rollTotal);		return rollTotal;		}}

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
https://linustechtips.com/topic/152551-java-programming-help/#findComment-2037549
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×