none
Programmering 1 (C#) RRS feed

  • Question

  • Hi!
    I want to create a method that corespond the Main cod but I'm not success, can you help me. How I can do it. User can chose how many Dice can random. 

    static int RullaTärning(int tärningar)
            {

            }
            static void Main()
            {
                Random slumpTal = new Random();
                List<int> tärningar = new List<int>();
                bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\tVälkommen till tärningsgeneratorn: ");
                    Console.Write("\n\t[1] Rulla tärning: ");
                    Console.Write("\n\t[2] Kolla vad du rullade: ");
                    Console.Write("\n\t[3] Avsluta: ");
                    Console.Write("\n\tVälja: ");
                    int val;
                    int.TryParse(Console.ReadLine(), out val);
                    switch (val)
                    {
                        case 1:
                            Console.Write("\n\tHur många tärningar vill du rulla: ");
                            bool inmatning = int.TryParse(Console.ReadLine(), out int antal);
                            if (inmatning)
                            {
                                for (int i = 0; i < antal; i++)
                                {
                                    tärningar.Add(RullaTärning(slumpTal));
                                }
                            }
                            break;
                        case 2:
                            int sum = 0;
                            if (tärningar.Count <= 0)
                            {
                                Console.WriteLine("\n\tDet finns inga sparade tärningsrull! ");
                            }
                            else
                            {
                                Console.WriteLine("\n\tRulla tärningar: ");
                                foreach (int tärning in tärningar)
                                {
                                    Console.WriteLine("\t" + tärning);
                                }
                                Console.WriteLine("\n\tMedelvärdet på alla tärningasrull: " + sum);
                            }
                            break;
                        case 3:
                            Console.WriteLine("\n\tTack för att du rullade tärning! ");
                            Thread.Sleep(1000);
                            kör = false;
                            break;
                        default:
                            Console.WriteLine("\n\tVälja 1-3 från meny. ");
                            break;
    Friday, December 7, 2018 12:43 PM

All replies

  • Hello,

    When posting code indicate what is not working, post the code in a code block and post all of the code. I took your code and made it whole but will not run as is because of the throw new NotImplementedException as it's unclear of the intent for these two methods.

    using System;
    using System.Collections.Generic;
    using System.Threading;
    
    namespace ConsoleApp1
    {
        class Program
        {
            /// <summary>
            /// unused
            /// </summary>
            /// <param name="tärningar"></param>
            /// <returns></returns>
            static int RullaTärning(int tärningar)
            {
                throw new NotImplementedException(); // you left logic out
            }
    
            static void Main(string[] args)
            {
                Random slumpTal = new Random();
                List<int> tärningar = new List<int>();
                bool kör = true;
                while (kör)
                {
                    Console.WriteLine("\n\tVälkommen till tärningsgeneratorn: ");
                    Console.Write("\n\t[1] Rulla tärning: ");
                    Console.Write("\n\t[2] Kolla vad du rullade: ");
                    Console.Write("\n\t[3] Avsluta: ");
                    Console.Write("\n\tVälja: ");
                    int val;
                    int.TryParse(Console.ReadLine(), out val);
                    switch (val)
                    {
                        case 1:
                            Console.Write("\n\tHur många tärningar vill du rulla: ");
                            bool inmatning = int.TryParse(Console.ReadLine(), out int antal);
                            if (inmatning)
                            {
                                for (int i = 0; i < antal; i++)
                                {
                                    tärningar.Add(RullaTärning(slumpTal));
                                }
                            }
    
                            break;
                        case 2:
                            int sum = 0;
                            if (tärningar.Count <= 0)
                            {
                                Console.WriteLine("\n\tDet finns inga sparade tärningsrull! ");
                            }
                            else
                            {
                                Console.WriteLine("\n\tRulla tärningar: ");
                                foreach (int tärning in tärningar)
                                {
                                    Console.WriteLine("\t" + tärning);
                                }
    
                                Console.WriteLine("\n\tMedelvärdet på alla tärningasrull: " + sum);
                            }
    
                            break;
                        case 3:
                            Console.WriteLine("\n\tTack för att du rullade tärning! ");
                            Thread.Sleep(1000);
                            kör = false;
                            break;
                        default:
                            Console.WriteLine("\n\tVälja 1-3 från meny. ");
                            break;
                    }
                }
            }
            /// <summary>
            /// You didn't show this
            /// </summary>
            /// <param name="slumpTal"></param>
            /// <returns></returns>
            private static int RullaTärning(Random slumpTal)
            {
                throw new NotImplementedException();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, December 7, 2018 12:55 PM
    Moderator
  • Hi Amir Rezai,

    Is there any update, do you resolve the issue?  It seems that Karen's reply resolve your issue, please take a look.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 10, 2018 6:08 AM
    Moderator
  • Besides the text in a foreign language, can you gives a brief resume of what you want to do?

    Something like: "I wanted to make a random number and display a text"

    If your variable's name are very meaningful can you please translate?

    e.g if one of your variable name is: numberTwo, if you translate we (I) will get that it needs to receive number two ( 2 ).

    But If their name doesn't matter, you can let how it is and just give a explanation of what you want to do.

    I can understand part of what is C#, but my present skills prevent me of figuring out what it needs to be done.

    BP-LP 2005/2016 @ll rights reserved

    Tuesday, December 11, 2018 9:54 AM