using System; using System.Collections.Generic; using System.Linq; using System.
ID: 3633881 • Letter: U
Question
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string output;
int diceRole, role, die;
int countOfTwos = 0,
countOfThrees = 0,
countOfFours = 0,
countOfFives = 0,
countOfSixes = 0,
countOfSevens = 0,
countOfEights = 0,
countOfNines = 0,
countOfTens = 0,
countOfElevens = 0,
countOfTwelves = 0;
GetRandomNumbers(diceRole, role,die, countOfTwos, countOfThrees, countOfFours, countOfFives,
countOfSixes, countOfSevens, countOfEights, countOfNines, countOfTens,
countOfElevens, countOfTwelves);
DisplayResult( output, diceRole, role, die, countOfTwos, countOfThrees, countOfFours, countOfFives,
countOfSixes, countOfSevens, countOfEights, countOfNines, countOfTens,
countOfElevens, countOfTwelves);
GetRandomNumbers (diceRole, role, die, countOfTwos, countOfThrees, countOfFours, countOfFives,
countOfSixes, countOfSevens, countOfEights, countOfNines, countOfTens,
countOfElevens, countOfTwelves)
Random rnd = new Random();
do
{
role = 0;
while (true)
{
role++;
diceRole = 0;
for (int i = 1; i <= 101; i++)
{
die = rnd.Next(1, 13);
diceRole += die;
switch (die)
{
case 1:
die = 2; countOfTwos++;
break;
case 2:
die = 3; countOfThrees++;
break;
case 3:
die = 4; countOfFours++;
break;
case 4:
die = 5; countOfFives++;
break;
case 5:
die = 6; countOfSixes++;
break;
case 6:
die = 7; countOfSevens++;
break;
case 7:
die = 8; countOfEights++;
break;
case 8:
die = 9; countOfNines++;
break;
case 9:
die = 10; countOfTens++;
break;
case 10:
die = 11; countOfElevens++;
break;
case 11:
die = 12; countOfTwelves++;
break;
}
DisplayResult(output, diceRole, role, die, countOfTwos, countOfThrees, countOfFours, countOfFives,
countOfSixes, countOfSevens, countOfEights, countOfNines, countOfTens,
countOfElevens, countOfTwelves);
output = "Number of twos: {0} ", countOfTwos;
}
}
}
Explanation / Answer
//Needed JOptionPane
import javax.swing.JOptionPane;
//Create class
class Program
{
//Main method
public static void main(String[] args)
{
//Assign variables
int countOfTwos = 0,
countOfThrees = 0,
countOfFours = 0,
countOfFives = 0,
countOfSixes = 0,
countOfSevens = 0,
countOfEights = 0,
countOfNines = 0,
countOfTens = 0,
countOfElevens = 0,
countOfTwelves = 0;
//Method to get random numbers and display the result
GetRandomNumbers(countOfTwos, countOfThrees, countOfFours, countOfFives,
countOfSixes, countOfSevens, countOfEights, countOfNines, countOfTens,
countOfElevens, countOfTwelves);
}//End main
//Definition of method
static public void GetRandomNumbers (int countOfTwos,int countOfThrees,int countOfFours, int countOfFives,
int countOfSixes,int countOfSevens,int countOfEights, int countOfNines,int countOfTens,int countOfElevens,int countOfTwelves)
{
int die=0;
//Loop repeats for 100 times
for (int i = 1; i <101; i++)
{
//Get a random die
die = 1+(int)(Math.random()*12);
//Switch case checks the die value and increase the count of value
switch (die)
{
case 2:
countOfTwos++;
break;
case 3:
countOfThrees++;
break;
case 4:
countOfFours++;
break;
case 5:
countOfFives++;
break;
case 6:
countOfSixes++;
break;
case 7:
countOfSevens++;
break;
case 8:
countOfEights++;
break;
case 9:
countOfNines++;
break;
case 10:
countOfTens++;
break;
case 11:
countOfElevens++;
break;
case 12:
countOfTwelves++;
break;
}//end Switch
}//end for
//Method displays the output
DisplayResult( countOfTwos, countOfThrees, countOfFours, countOfFives,
countOfSixes, countOfSevens, countOfEights, countOfNines, countOfTens,
countOfElevens, countOfTwelves);
}//end method
//Method DisplayResult definition
static public void DisplayResult( int countOfTwos,int countOfThrees,int countOfFours,int countOfFives,
int countOfSixes,int countOfSevens,int countOfEights, int countOfNines,int countOfTens,
int countOfElevens,int countOfTwelves)
{
//Display the output in a message box
JOptionPane.showMessageDialog(null,"Count Of Twos "+countOfTwos+" "+
"Count Of Threes "+countOfThrees+
" Count Of Fours "+countOfFours+
" Count of Fives "+countOfFives+
" Count of Sixes "+countOfSixes+
" Count of Sevens "+countOfSevens+
" Count of Eights "+countOfEights+
" Count of Nines "+countOfNines+
" Count of Tens "+countOfTens+
" Count of Elevens "+countOfElevens+
" Count of Twelves "+countOfTwelves);
System.exit(0);
}//end method
}//end class
-----------------------------------------------------------------------------------------------------
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.