Question

Intro to Java. Can someone please help me answer this question. Create a java program playing...

Intro to Java.

Can someone please help me answer this question.

  1. Create a java program playing the game of craps with a random number generator. Write a method craps that plays the game of craps and it should return a 1 representing a win, a 2 representing a loss, and a 0 representing the need to toss the pair of dice again. The main method should ask how many games you wish to play then call the method craps. Main method also should accumulate the number of wins and lose.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random; import java.util.Scanner; public class Main { public static int craps(){ Random rand = new Random(); int dice1 = rand.nextInt(6) + 1; int dice2 = rand.nextInt(6) + 1; int sum = dice1 + dice2; if (sum == 7 || sum == 11) { return 1; } if (sum == 2 || sum == 3 || sum == 12) { return 2; } return 0; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; System.out.println("How many games you wish to play? "); n = sc.nextInt(); int wins = 0, loss = 0; while (n > 0) { int result = craps(); if (result == 1) { wins ++; } else if (result == 2) { loss ++; } } System.out.println("Number of wins = " + wins); System.out.println("Number of loss = " + loss); } }
Add a comment
Know the answer?
Add Answer to:
Intro to Java. Can someone please help me answer this question. Create a java program playing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • I'm having trouble with this question, could someone please help? A person pays $2 to play...

    I'm having trouble with this question, could someone please help? A person pays $2 to play a game rolling a single dice. If 1 or 2 comes up, the player wins nothing. If 3, 4, 5, or 6 comes up, then the player wins the difference between $2 and the number rolled. Fill in the following table, and calculate the expected value of the game. X 1 2 3 4 5 6 Total P(X) $ win P(X) x $ win...

  • c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...

    c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below. Your program should have two sections that correspond to Problems 12 and 13: 1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your...

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste...

    Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste version): from tkinter import Tk, Label, Entry, Button from random import * class Craps(Tk): #set up the main window def __init__(self, parent=None): Tk.__init__(self, parent) self.title('Play Craps') self.new_game() self.make_widgets() #when a new game is started, the firstRoll will start at 0 def new_game(self): self.firstRoll = 0    #create and place the widgets in the window def make_widgets(self): Label(self, text="Die 1").grid(row=0, column=0, columnspan=1) Label(self, text="Die 2").grid(row=0,...

  • Can anyone help me with this java program? We are still very early with lessons so...

    Can anyone help me with this java program? We are still very early with lessons so no advanced code please. And if you comment throughout the code that would be incredibly helpful. Thank you Create 2 Java files for this assignment: Die.java and TestDie.java Part 1 - The Die Class             The program Design a Die class that contains the following attributes: sides: represents how many sides a dice has. A dice usually has 6 sides but sometimes could have...

  • Could someone please help me with this? Thank you Game Description: The popular rock-paper-scissors game is...

    Could someone please help me with this? Thank you Game Description: The popular rock-paper-scissors game is usually played between two people in which each player simultaneously chooses either a rock or a paper or scissors (usually with an outstretched hand). The rule of the game is simple: rock crushes scissors, scissors cut paper, and paper wraps rock. If both the players choose the same object, then it ends in a tie. (See this link for more details.) Problem Description: You...

  • Please do this by JAVA!!!!!!! This program can assume all inputs are integers representing whole dollar...

    Please do this by JAVA!!!!!!! This program can assume all inputs are integers representing whole dollar amounts. 1) Ask the user for a stake amount (initial amount of money available to bet). Check to make the sure user enters a correct number (over 0). State a descriptive error to re-prompt the user if the stake amount is not a correct amount like a negative number). 2) Ask the thrower for their bet. The amount of money the thrower has to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT