Howdy, Stranger!

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

Any suggestions to make this look and run better?

SilhouetteSilhouette Member
edited July 2020 in Off-topic

So i'm pretty new to programming and this was my 1st official finished program. It calculates the amount of ways you can make a specified number with a specified amount of dice

Ex. How many ways can you make a 10 using 3 dice, (the program calculates it), you get 27.

I'm pretty proud of this, but i'm always open to learning how to improve my code :)

using System;


namespace Numbers

{

    class Program

    {

        static void Main(string[] args)

        {

            bool resetProgram = true;


            while (resetProgram == true)

            {

                Console.WriteLine("Input amount of dice (1-5).");

            int dice = Convert.ToInt32(Console.ReadLine());

            while (dice > 5 || dice < 1)

            {

                Console.WriteLine("please input a valid number (1-5)");

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

            }


            Console.WriteLine("Input desired number.");

            int desiredNum = Convert.ToInt32(Console.ReadLine());


            Console.WriteLine("");

           

            int A = 1;

            int B = 1;

            int C = 1;

            int D = 1;

            int E = 1;


            int total = 0;

            int ways = 0;


            bool cancel = false;


            while (cancel == false)

            {

                switch (dice)

                {

                    case 1:


                    total = A;


                    if (total == desiredNum)

                    {

                        ways = ways + 1;

                    }

                    Console.WriteLine("A = " + A);

                    Console.WriteLine("total = " + total);

                    Console.WriteLine("ways = " + ways);

                    Console.WriteLine("");

                    

                    A = A + 1;


                    if (A > 6)

                    {

                        cancel = true;

                    }


                    break;


                    case 2:


                    total = A + B;


                    if (total == desiredNum)

                    {

                        ways = ways + 1;

                    }

                    Console.WriteLine("A = " + A);

                    Console.WriteLine("B = " + B);

                    Console.WriteLine("total = " + total);

                    Console.WriteLine("ways = " + ways);

                    Console.WriteLine("");

                    

                    A = A + 1;


                    if (A > 6)

                    {

                        A = 1;

                        B = B + 1;

                    }

                    if (B > 6)

                    {

                        cancel = true;

                    }


                    break;


                    case 3:


                    total = A + B + C;


                    if (total == desiredNum)

                    {

                        ways = ways + 1;

                    }

                    Console.WriteLine("A = " + A);

                    Console.WriteLine("B = " + B);

                    Console.WriteLine("C = " + C);

                    Console.WriteLine("total = " + total);

                    Console.WriteLine("ways = " + ways);

                    Console.WriteLine("");


                    A = A + 1;


                    if (A > 6)

                    {

                        A = 1;

                        B = B + 1;

                    }

                    if (B > 6)

                    {

                        B = 1;

                        C = C + 1;

                    }

                    if (C > 6)

                    {

                        cancel = true; 

                    }


                    break;


                    case 4:


                    total = A + B + C + D;


                    if (total == desiredNum)

                    {

                        ways = ways + 1;

                    }

                    Console.WriteLine("A = " + A);

                    Console.WriteLine("B = " + B);

                    Console.WriteLine("C = " + C);

                    Console.WriteLine("D = " + D);

                    Console.WriteLine("total = " + total);

                    Console.WriteLine("ways = " + ways);

                    Console.WriteLine("");


                    A = A + 1;


                    if (A > 6)

                    {

                        A = 1;

                        B = B + 1;

                    }

                    if (B > 6)

                    {

                        B = 1;

                        C = C + 1;

                    }

                    if (C > 6)

                    {

                        C = 1;

                        D = D + 1; 

                    }

                    if (D > 6)

                    {

                        cancel = true;

                    }


                    break;

                    

                    case 5:


                    total = A + B + C + D + E;


                    if (total == desiredNum)

                    {

                        ways = ways + 1;

                    }

                    Console.WriteLine("A = " + A);

                    Console.WriteLine("B = " + B);

                    Console.WriteLine("C = " + C);

                    Console.WriteLine("D = " + D);

                    Console.WriteLine("E = " + E);

                    Console.WriteLine("total = " + total);

                    Console.WriteLine("ways = " + ways);

                    Console.WriteLine("");


                    A = A + 1;


                    if (A > 6)

                    {

                        A = 1;

                        B = B + 1;

                    }

                    if (B > 6)

                    {

                        B = 1;

                        C = C + 1;

                    }

                    if (C > 6)

                    {

                        C = 1;

                        D = D + 1; 

                    }

                    if (D > 6)

                    {

                        D = 1;

                        E = E + 1;

                    }

                    if (E > 6)

                    {

                        cancel = true;

                    }


                    break;

                }

            }

            bool loop = true;


            Console.ReadKey();

            Console.Clear();

            Console.WriteLine("the amount of ways you can make a " + desiredNum +" using " + dice + " dice, is " + ways);

            Console.WriteLine("");

            Console.WriteLine("Reset? Type [y] for yes, and [n] for no.");


            string reset = Console.ReadLine();

            while (loop == true)

            {

                switch (reset)

                {

                    case "y":

                    Console.Clear();

                    resetProgram = true;

                    loop = false;


                    break;


                    case "n":

                    resetProgram = false;

                    loop = false;

                    break;


                    default:

                    Console.WriteLine("unknown command. To reset type [y] for yes, and [n] for no.");

                    reset = Console.ReadLine();

                    break;

                }

            }

            }

        }

    }

}

Best Answer

  • JIMMY_VASHI04JIMMY_VASHI04 Member
    Accepted Answer

    Asking us about making this code smaller an more efficient is not a good thing


    Save it for later.


    So when you have nothing to then try this on your own

Sign In or Register to comment.