#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# BRACKEYS - Solution to C# Tutorial 04 Challenge

13»

• My Solution To Challeng4:

using System;

namespace yeet_le_program

{

class Challenge4

{

static void Main(string[] args)

{

Random numberGen = new Random();

int roll1 = 0;

int roll2 = 1;

int attempts = 0;

Console.WriteLine("Press Enter to roll the dice");

while(roll1 != roll2)

{

roll1 = numberGen.Next(1,7);

roll2 = numberGen.Next(1,7);

Console.WriteLine("You Rolled: "+roll1+","+roll2);

attempts++;

}

Console.WriteLine("It took you "+attempts+" attempts to roll 2 of the same kind");

}

}

}

• edited November 2021

I'm very proud of my solution :)

```using System;
using System.Collections.Generic;
using System.Linq;

namespace MyApp // Note: actual namespace depends on the project name.
{
public class Program
{
public static void Main(string[] args)
{
// Title
Console.Title = "2 Dice rolls";

// Random Number
Random numGen = new Random();

// Variables
int roll1 = 0;
int roll2 = 0;
int attempts = 0;

Console.WriteLine("Press any key to roll the dice.");

// Main
roll1 = numGen.Next(1, 7);
roll2 = numGen.Next(1, 7);
Console.WriteLine("You rolled: " + roll1 + " and " +roll2);
attempts++;
while (roll1 != roll2) {
roll1 = numGen.Next(1, 7);
roll2 = numGen.Next(1, 7);
Console.WriteLine("You rolled: " + roll1 + " and " + roll2);
attempts++;
}

// Output number of attempts
Console.WriteLine("It took you " + attempts + " attemps to roll one of a kind.");

// Wait for input before closing
}
}
}
```
• Quick question, how come it doesn't work unless int roll02 =1 ??

• using System;

namespace Code_Relearn_Camp

{

class Program

{

static void Main(string[] args)

{

Random numberGen = new Random();

int roll1 = 0;

int roll2 = 1;

int attempts = 0;

Console.WriteLine("Press space to roll the die.");

while (roll1 != roll2)

{

roll1 = numberGen.Next(1, 7);

roll2 = numberGen.Next(1, 7);

Console.WriteLine("You rolled: " + roll1 + " and " + roll2);

attempts++;

}

Console.WriteLine("It took you " + attempts + " attempts to roll two of a kind");

//To make sure that this doesn't close immediately.

}

}

}

• KurRuppted VS Code

using System;

using System.Collections.Generic;

using System.Linq;

namespace MyApp // Note: actual namespace depends on the project name.

{

public class Program

{

public static void Main(string[] args)

{

Random numberGen01 = new Random();

Random numberGen02 = new Random();

int roll01 = 2;

int roll02 = 1;

int attempts = 0;

Console.WriteLine("Press enter to roll the two dices!");

while(roll01 != roll02)

{

roll01 = numberGen01.Next(1,7);

roll02 = numberGen02.Next(1,7);

Console.WriteLine("Die 1: " + roll01);

Console.WriteLine("Die 2: " + roll02);

attempts ++;

}

Console.WriteLine("It took you " + attempts + " attempts to match both dies!");

}

}

}

• // Heres a solution to check for only two 6's

using System;

namespace MyApp // Note: actual namespace depends on the project name.

{

internal class Program

{

static void Main(string[] args)

{

Random numGen = new Random();

int roll01 = 0;

int roll02 = 0;

int attempts = 0;

Console.WriteLine("Press enter to roll");

while (roll01 != 6 && roll02 != 6)

{

roll01 = numGen.Next(1, 7);

roll02 = numGen.Next(1, 7);

Console.WriteLine("Dice 1 : " + roll01);

Console.WriteLine("Dice 2 : " + roll02);

attempts++;

}

Console.WriteLine("It took you "+ attempts + " to roll two 6's");

}

}

}

• @svper Its because the condition while (roll01 != roll02)

• using System;

namespace MyApp // Note: actual namespace depends on the project name.

{

internal class Program

{

static void Main(string[] args)

{

Random numGen = new Random();

int roll01 = 0;

int roll02 = 1;

int attempts = 0;

Console.WriteLine("Press enter to roll");

while (roll01 != roll02)

{

roll01 = numGen.Next(1, 7);

roll02 = numGen.Next(1, 7);

Console.WriteLine("Dice 1 : " + roll01);

Console.WriteLine("Dice 2 : " + roll02);

attempts++;

}

Console.WriteLine("It took you "+ attempts + " attempts to roll two " + roll01);

}

}

}

• this was mine:

```using System;

class Program
{
static void Main(string[] args)
{
Random numberGen = new Random();

int roll1 = 0;        // dice 1
int attempts = 0;    // attempts taken
int roll2 = 1;        // dice 2

Console.WriteLine("Press enter to roll the dice.");

while(roll1 != roll2) {    // loop

roll1 = numberGen.Next(1, 7);    // rolling dice
Console.WriteLine("Dice 1 rolled: " + roll1);
roll2 = numberGen.Next(1, 7);
Console.WriteLine("Dice 2 rolled: " + roll2 + "\n");
attempts++;
}

Console.WriteLine("\nIt took you " + attempts + " attempts to get both dice equivalent.");    //winning text

}
}

```
• My attempt :|

Random numberGen = new Random();

int roll1 = 1;

int roll2 = 0;

int attempts = 0;

Console.WriteLine("Press ENTER to roll");

while(roll1 != roll2) {

roll1 = numberGen.Next(1, 7);

roll2 = numberGen.Next(1, 7);

Console.WriteLine("Dice 1: " + roll1);

Console.WriteLine("Dice 2: " + roll2);

Console.WriteLine();

attempts++;

}

Console.WriteLine("It took you " + attempts + " attempts to roll two dice of the same number");

// Wait before closing

• I decided that in order to memorize everything up until now in this course i had to give myself a mini-challange. So i made a simple gameish-calculator.

int result, number1, number2;

Console.WriteLine("How many calculations do you want to make? It cant be larger than 10");

while(calcNumber > 10) {

}

for (int i = 0; i < calcNumber; i++)

{

Console.WriteLine("\nPress 1 to add\nPress 2 to subtract\nPress 3 to multiply");

while(decision > 3) {

Console.WriteLine("\nPress 1 to add\nPress 2 to subtract\nPress 3 to multiply");

}

Console.WriteLine("Here starts the calculation.");

Console.Write("First number: ");

Console.Write("Second number: ");

switch(decision) {

case 1:

result = number1 + number2;

Console.WriteLine(number1 + " + " + number2 + " = " + result);

break;

case 2:

result = number1 - number2;

Console.WriteLine(number1 + " - " + number2 + " = " + result);

break;

case 3:

result = number1 * number2;

Console.WriteLine(number1 + " x " + number2 + " = " + result);

break;

}

}

// Wait before closing

• here is the code:

Here is the outcome:

Have a great day!

• Random numberGen = new Random();

int roll1 = 1;

int roll2 = 0;

int attempts = 0;

Console.WriteLine("Press enter to roll the dice.");

while(roll1 != roll2){

roll1 = numberGen.Next(1, 7);

roll2 = numberGen.Next(1, 7);

Console.WriteLine("Roll 1: "+ roll1);

Console.WriteLine("Roll 2: "+ roll2 +"\n");

attempts++;

}

Console.WriteLine("It took you "+ attempts + " attempts to roll the same number on both dice!");

• using System;

namespace C_

{

class Program

{

static void Main(string[] args)

{

Random numGen = new Random();

int roll01 = 0;

int roll02 = 1;

int attemps = 0;

Console.WriteLine("Press enter to roll the dice.");

while(roll01 != roll02){

roll01 = numGen.Next(1, 7);

roll02 = numGen.Next(1, 7);

Console.WriteLine("First dice: " + roll01);

Console.WriteLine("Second dice: " + roll02 + "\n");

attemps++;

}

Console.Write("It tooks you " + attemps + " attemps to roll a the same number.");

}

}

}

• edited October 2022

using System;

namespace My_awesone_program

{

class Program

{

static void Main(string[] args)

{

Random numberGen1 = new Random();

Random numberGen2 = new Random();

int roll1 = 0;

int roll2 = 1;

int attempts1 = 0;

//int totalroll ;

Console.WriteLine("Press any key to roll the dice");

while (roll1 != roll2)

{

roll1 = numberGen1.Next(1, 7);

roll2 = numberGen2.Next(1, 7);

Console.WriteLine(\$"You rolled a {roll1} and a {roll2}");

attempts1++;

}

Console.WriteLine(\$"It took you {attempts1} attempts to roll the same dice");

//Wait before closing

}

}

}

Took me a while to realize that roll1 and roll2 first stared as 0, and it wouldnt loop. Until i saw the comments here and got how stupid I am haha!

• okay I did it well im trying my best and this is what i got but it was obviously wrong

But thank you all i understood and learnt from my mistake<3

int roll1 = 0;

int roll2 = 1;

int attempts = 0;

Console.WriteLine("Press Enter to roll the die");

while(roll1 == roll2 &&  roll1 + roll2 !=6);

{

roll1 = numberGen.Next(1, 7);

roll2 = numberGen.Next(1, 7);

Console.WriteLine("You rolled: " + roll1 + roll2);

attempts++;

}

Console.WriteLine("IT TOOK YOU "  +  attempts + " attempts to roll the same dice.");

• Took a few tries but i managed it

```using System;

namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program
{
static void Main(string[] args)
{
Console.Title = "Dice";

Random numberGen = new Random();

double roll02 = 0;
double roll01 = 0;
int attempts = 0;
double i = numberGen.Next(1, 7);

Console.WriteLine("Press enter to roll the die.");

while (roll01 != i)
{

roll01 = numberGen.Next(1, 7);
roll02 = i;

Console.WriteLine("You rolled a " + roll01);
Console.WriteLine("You rolled a " + roll02);
attempts++;
}

Console.WriteLine("It took you " + attempts + " times to roll two of a kind.");