I need help changing this code from Java to C#. Here are the instructions for th
ID: 3606232 • Letter: I
Question
I need help changing this code from Java to C#.
Here are the instructions for the assignment:
-change the constant final to a C# code final
-replace all print line statements with C# code equilivent
-remove "playAgain = keyboard.next();" and replace this with C# code to get the user input and pass it to the playAgain variable
-declare a random variable below your "playAgain" variable like this: RANDOM rand = new Random ();
-replace both Math.Random() calls in your code with the call to the next Method on the rand variable like this: rand.Next()
-Run the program. What is the bug? Write the answer in a readme text file.
-fix the bug and rerun the program.
/**
* Workshop 5
*
* This program simulates 10,000 games of craps.
* It counts the number of wins and losses and outputs the probability
* of winning.
*
* Created: NA
*
* @author NA
* @version 1
*/
public class Craps
{
private static final int NUM_GAMES = 100;
public static void Main(String[] args)
{
String playAgain = "";
do{
// Variable declarations
int numSevens = 0;
int numElevens = 0;
int roll = 0;
// Play 100 games
for (int i=0; i<NUM_GAMES; i++)
{
// Simulate rolling the two dice, with values from 1-6
roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) + 2;
// Check for initial win or loss
if (roll == 7)
{
numSevens++;
}
else
if(roll == 11)
{
numElevens++;
}
}
// Output probability of winning
System.out.println("The player rolled " + numSevens + " sevens and " + numElevens + " elevens for a total of " + (numSevens + numElevens)
+ " wins out of 100.");
System.out.println(" Try again, Enter y for yes, n for no");
playAgain = keyboard.next();
}while(play.Again.Equals("y", StringComparisson.Invariant.CultureIgnoreCase));"
}
} // Question2
Explanation / Answer
Below is the working program: -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiceRoller
{
/**
* Workshop 5
*
* This program simulates 10,000 games of craps.
* It counts the number of wins and losses and outputs the probability
* of winning.
*
* Created: NA
*
* @author NA
* @version 1
*/
public class Craps
{
private static readonly int NUM_GAMES = 100;
public static void Main(String[] args)
{
String playAgain = "";
do{
// Variable declarations
int numSevens = 0;
int numElevens = 0;
int roll = 0;
// Play 100 games
Random rand = new Random();
for (int i=0; i<NUM_GAMES; i++)
{
// Simulate rolling the two dice, with values from 1-6
roll = rand.Next(6) + rand.Next(6) + 2;
// Check for initial win or loss
if (roll == 7)
{
numSevens++;
}
else
if(roll == 11)
{
numElevens++;
}
}
// Output probability of winning
Console.WriteLine("The player rolled {0} sevens and {1} elevens for a total of {2} wins out of 100.",numSevens,numElevens,(numSevens + numElevens));
Console.WriteLine(" Try again, Enter y for yes, n for no");
playAgain = Console.ReadLine();
}while(playAgain.Equals("y", StringComparison.InvariantCultureIgnoreCase));
}
} // Question2
}
Output: -
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.