SlideShare a Scribd company logo
1 of 3
Download to read offline
/************************************************************************************
**********
* Amy Baxter
* Program 07
*
* This program is designed to simulate a dice rolling/odds program.
*************************************************************************************
********/
import java.util.*;
public class Lab07 {
public static void main(String[] args) {
// Declare Variables
int loses = 0;// keeps track of number of loses
int wins = 0;// keeps track of wins
int dice1;
int dice2;
boolean playAgain = true;
welcome();
while (playAgain == true) {
dice1 = rollDie();
dice2 = rollDie();
printDice(dice1, dice2);
if (isPair(dice1, dice2)) {
wins++;
System.out.println("Congrats! You won!");
} else {
loses++;
System.out
.println("You lost, you're really bad at this
game...");
}
playAgain = playAgain();
}
report(wins, loses);
}
// displays a brief accounting of the rules and format of the game
public static void welcome() {
System.out.println(" Welcome to Computer Dice 2013n"
+ "-------------------------------------n"
+ "You will first roll your dicen"
+ "Next the outcome of your roll willn" + "be
determined:n"
+ "Any pair and you Winn" + "Anything else and you
Losen"
+ "--------------------------------------n");
}
// returns an int in the range [1, 6]
public static int rollDie() {
Random generator = new Random();
int roll = generator.nextInt(6) + 1;
return roll;
}
// formats d1 and d2 as a roll of two die and prints to the screen
public static void printDice(int d1, int d2) {
System.out.println(" Player n--------n " + d1 + " " + d2);
}
// returns true only when d1 == d2
public static boolean isPair(int d1, int d2) {
if (d1 == d2) {
return true;
} else {
return false;
}
}
// displays a report of the game's outcomes to the screen
public static void report(int wins, int loses)
{
System.out.println("Computer Dice Resultsn"
+ "---------------------n" + "You played " + (wins +
loses)
+ " roundsn" + "Rounds won : " + wins + "n"
+ "Rounds lost : " + loses + "n" + "---------------------
");
}
// ask the user if they want to play again.
public static boolean playAgain() {
Scanner scanner = new Scanner(System.in);
boolean valid = false;
String input = "f";
while (valid == false) {
System.out.println("Do you want to play again [y,n]:");
input = scanner.next();
//input = input.toLowerCase();
if (input.equals("y") || input.equals("n")) {
valid = true;
}
}//end while
//scanner.close();
// Return true if y else return false
if (input.equals("y")) {
return true;
} else {
return false;
}
}
}

More Related Content

What's hot

Pattern matching in Elixir by example - Alexander Khokhlov
Pattern matching in Elixir by example - Alexander KhokhlovPattern matching in Elixir by example - Alexander Khokhlov
Pattern matching in Elixir by example - Alexander KhokhlovElixir Club
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuerysergioafp
 
The Ring programming language version 1.5.1 book - Part 54 of 180
The Ring programming language version 1.5.1 book - Part 54 of 180The Ring programming language version 1.5.1 book - Part 54 of 180
The Ring programming language version 1.5.1 book - Part 54 of 180Mahmoud Samir Fayed
 
TicketBEKA? Ticket booking
TicketBEKA? Ticket bookingTicketBEKA? Ticket booking
TicketBEKA? Ticket bookingLikhith Pujari
 
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014Supriya Radhakrishna
 
Keyboard Access APIs
Keyboard Access APIsKeyboard Access APIs
Keyboard Access APIstoddkloots
 

What's hot (12)

Pattern matching in Elixir by example - Alexander Khokhlov
Pattern matching in Elixir by example - Alexander KhokhlovPattern matching in Elixir by example - Alexander Khokhlov
Pattern matching in Elixir by example - Alexander Khokhlov
 
J2 me 07_5
J2 me 07_5J2 me 07_5
J2 me 07_5
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuery
 
The Ring programming language version 1.5.1 book - Part 54 of 180
The Ring programming language version 1.5.1 book - Part 54 of 180The Ring programming language version 1.5.1 book - Part 54 of 180
The Ring programming language version 1.5.1 book - Part 54 of 180
 
TicketBEKA? Ticket booking
TicketBEKA? Ticket bookingTicketBEKA? Ticket booking
TicketBEKA? Ticket booking
 
Computer practicals(part) Class 12
Computer practicals(part) Class 12Computer practicals(part) Class 12
Computer practicals(part) Class 12
 
efy-articles
efy-articlesefy-articles
efy-articles
 
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
 
Phonegap for Android
Phonegap for AndroidPhonegap for Android
Phonegap for Android
 
Keyboard Access APIs
Keyboard Access APIsKeyboard Access APIs
Keyboard Access APIs
 
Fact, Fiction, and FP
Fact, Fiction, and FPFact, Fiction, and FP
Fact, Fiction, and FP
 
CreateJS
CreateJSCreateJS
CreateJS
 

Viewers also liked (11)

Ost 1 10916 73
Ost 1 10916 73Ost 1 10916 73
Ost 1 10916 73
 
Stb 14969
Stb 14969Stb 14969
Stb 14969
 
Word seminar rs. muhammadiyah palembang (repaired)
Word seminar rs. muhammadiyah palembang (repaired)Word seminar rs. muhammadiyah palembang (repaired)
Word seminar rs. muhammadiyah palembang (repaired)
 
Portad amichi^^
Portad amichi^^Portad amichi^^
Portad amichi^^
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
 
Prp_Portfolio
Prp_PortfolioPrp_Portfolio
Prp_Portfolio
 
cepra 3
cepra 3cepra 3
cepra 3
 
Hola
HolaHola
Hola
 
инђија
инђијаинђија
инђија
 
Album
AlbumAlbum
Album
 
Cubismo
CubismoCubismo
Cubismo
 

Similar to Simulate dice rolling and odds

public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdfkavithaarp
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfarchanaemporium
 
import java.util.Scanner;public class Fraction {   instan.pdf
import java.util.Scanner;public class Fraction {    instan.pdfimport java.util.Scanner;public class Fraction {    instan.pdf
import java.util.Scanner;public class Fraction {   instan.pdfapleathers
 
Nested For Loops and Class Constants in Java
Nested For Loops and Class Constants in JavaNested For Loops and Class Constants in Java
Nested For Loops and Class Constants in JavaPokequesthero
 
java-introduction.pdf
java-introduction.pdfjava-introduction.pdf
java-introduction.pdfDngTin307322
 
Program(Output)
Program(Output)Program(Output)
Program(Output)princy75
 
VideoGamepublic class VideoGam.pdf
VideoGamepublic class VideoGam.pdfVideoGamepublic class VideoGam.pdf
VideoGamepublic class VideoGam.pdfannaistrvlr
 
Here is the game description- Here is the sample game- Here is the req.pdf
Here is the game description- Here is the sample game- Here is the req.pdfHere is the game description- Here is the sample game- Here is the req.pdf
Here is the game description- Here is the sample game- Here is the req.pdftrishulinoverseas1
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfanjandavid
 
Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdfHere is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdftrishulinoverseas1
 
Goal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdfGoal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdfaaicommunication34
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfanithareadymade
 
Treatment, Architecture and Threads
Treatment, Architecture and ThreadsTreatment, Architecture and Threads
Treatment, Architecture and ThreadsMathias Seguy
 
Java programI made this Account.java below. Using the attached cod.pdf
Java programI made this Account.java below. Using the attached cod.pdfJava programI made this Account.java below. Using the attached cod.pdf
Java programI made this Account.java below. Using the attached cod.pdffathimafancy
 

Similar to Simulate dice rolling and odds (20)

RandomGuessingGame
RandomGuessingGameRandomGuessingGame
RandomGuessingGame
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
 
import java.util.Scanner;public class Fraction {   instan.pdf
import java.util.Scanner;public class Fraction {    instan.pdfimport java.util.Scanner;public class Fraction {    instan.pdf
import java.util.Scanner;public class Fraction {   instan.pdf
 
COnnect4Game
COnnect4GameCOnnect4Game
COnnect4Game
 
Nested For Loops and Class Constants in Java
Nested For Loops and Class Constants in JavaNested For Loops and Class Constants in Java
Nested For Loops and Class Constants in Java
 
Java
JavaJava
Java
 
java-introduction.pdf
java-introduction.pdfjava-introduction.pdf
java-introduction.pdf
 
Pro.docx
Pro.docxPro.docx
Pro.docx
 
Program(Output)
Program(Output)Program(Output)
Program(Output)
 
VideoGamepublic class VideoGam.pdf
VideoGamepublic class VideoGam.pdfVideoGamepublic class VideoGam.pdf
VideoGamepublic class VideoGam.pdf
 
Here is the game description- Here is the sample game- Here is the req.pdf
Here is the game description- Here is the sample game- Here is the req.pdfHere is the game description- Here is the sample game- Here is the req.pdf
Here is the game description- Here is the sample game- Here is the req.pdf
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
Mamdani
MamdaniMamdani
Mamdani
 
Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdfHere is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
 
Goal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdfGoal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdf
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdf
 
Treatment, Architecture and Threads
Treatment, Architecture and ThreadsTreatment, Architecture and Threads
Treatment, Architecture and Threads
 
Java programI made this Account.java below. Using the attached cod.pdf
Java programI made this Account.java below. Using the attached cod.pdfJava programI made this Account.java below. Using the attached cod.pdf
Java programI made this Account.java below. Using the attached cod.pdf
 

Simulate dice rolling and odds

  • 1. /************************************************************************************ ********** * Amy Baxter * Program 07 * * This program is designed to simulate a dice rolling/odds program. ************************************************************************************* ********/ import java.util.*; public class Lab07 { public static void main(String[] args) { // Declare Variables int loses = 0;// keeps track of number of loses int wins = 0;// keeps track of wins int dice1; int dice2; boolean playAgain = true; welcome(); while (playAgain == true) { dice1 = rollDie(); dice2 = rollDie(); printDice(dice1, dice2); if (isPair(dice1, dice2)) { wins++; System.out.println("Congrats! You won!"); } else { loses++; System.out .println("You lost, you're really bad at this game..."); } playAgain = playAgain(); } report(wins, loses); } // displays a brief accounting of the rules and format of the game public static void welcome() { System.out.println(" Welcome to Computer Dice 2013n"
  • 2. + "-------------------------------------n" + "You will first roll your dicen" + "Next the outcome of your roll willn" + "be determined:n" + "Any pair and you Winn" + "Anything else and you Losen" + "--------------------------------------n"); } // returns an int in the range [1, 6] public static int rollDie() { Random generator = new Random(); int roll = generator.nextInt(6) + 1; return roll; } // formats d1 and d2 as a roll of two die and prints to the screen public static void printDice(int d1, int d2) { System.out.println(" Player n--------n " + d1 + " " + d2); } // returns true only when d1 == d2 public static boolean isPair(int d1, int d2) { if (d1 == d2) { return true; } else { return false; } } // displays a report of the game's outcomes to the screen public static void report(int wins, int loses) { System.out.println("Computer Dice Resultsn" + "---------------------n" + "You played " + (wins + loses) + " roundsn" + "Rounds won : " + wins + "n" + "Rounds lost : " + loses + "n" + "--------------------- "); } // ask the user if they want to play again. public static boolean playAgain() { Scanner scanner = new Scanner(System.in); boolean valid = false; String input = "f";
  • 3. while (valid == false) { System.out.println("Do you want to play again [y,n]:"); input = scanner.next(); //input = input.toLowerCase(); if (input.equals("y") || input.equals("n")) { valid = true; } }//end while //scanner.close(); // Return true if y else return false if (input.equals("y")) { return true; } else { return false; } } }