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»

Comments

  • 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)

                {

                    Console.ReadKey();

                    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");

                Console.ReadKey();

            }

        }

    }

  • ArxyncArxync Member
    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
                Console.ReadKey();
                roll1 = numGen.Next(1, 7);
                roll2 = numGen.Next(1, 7);
                Console.WriteLine("You rolled: " + roll1 + " and " +roll2);
                attempts++;
                    while (roll1 != roll2) {
                    Console.ReadKey();
                    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
                Console.ReadKey();
            }
        }
    }
    
  • 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)

                {

                    Console.ReadKey();


                    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.

                Console.ReadKey();    


            }

        }

    }

  • 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)

    {

    Console.ReadKey();


    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!");



    Console.ReadKey();

    }

    }

    }



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




    // See https://aka.ms/new-console-template for more information

    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)

                {

                    Console.ReadKey();

                    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");


                Console.ReadLine();

                             

            }

        }

    }

  • @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)

                {

                    Console.ReadKey();

                    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);


                Console.ReadKey();

                             

            }

        }

    }

  • 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
            Console.ReadKey();
    
    
            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 :|

    // See https://aka.ms/new-console-template for more information


    // Zmiana wygladu konsoli


    Random numberGen = new Random();


    int roll1 = 1;

    int roll2 = 0;

    int attempts = 0;


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


    while(roll1 != roll2) {

        Console.ReadKey();


        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

    Console.ReadKey();

  • 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.

    // See https://aka.ms/new-console-template for more information


    int result, number1, number2;


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

    Console.Write("Your number: ");


    int calcNumber = Convert.ToInt16(Console.ReadLine());


    while(calcNumber > 10) {

        Console.Write("Your number cant be larger than 10\nYour number: ");

        calcNumber = Convert.ToInt16(Console.ReadLine());

    }


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

    {

       

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

            int decision = Convert.ToInt16(Console.ReadLine());


        while(decision > 3) {

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

            decision = Convert.ToInt16(Console.ReadLine());

        }


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


        Console.Write("First number: ");

        number1 = Convert.ToInt32(Console.ReadLine());


        Console.Write("Second number: ");

        number2 = Convert.ToInt32(Console.ReadLine());


        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

    Console.ReadKey();

  • here is the code:


    Here is the outcome:


    Have a great day!

  • MekseeMeksee Member

    Random numberGen = new Random();


    int roll1 = 1;

    int roll2 = 0;

    int attempts = 0;


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


    while(roll1 != roll2){

        Console.ReadKey();

        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!");


    Console.ReadKey();

  • 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){

                    Console.ReadKey();


                    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.");


                Console.ReadKey();

            }

        }

    }

  • vonkingenvonkingen Member
    edited October 2

    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)

          {

            Console.ReadKey();

            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

          Console.ReadKey();

        }


           

      }

    }


    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);

    {

        Console.ReadKey();

    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.");




     Console.ReadKey();

  • KaalKaal Member

    here's my solution!

Sign In or Register to comment.