-2

So i wanted to make a simple dice roll game in c# with switch and case but when I start the case statements don't print anything. I hope one of you guys can help me there because as you can see I am a complete beginner.

using System;
using System.Threading;
namespace Übung
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Random r = new Random();
            int rnd = Random.Range(1, 7);

            Console.WriteLine("Möchten sie WÜrfeln? \n ja? \n nein?");
            string s = Console.ReadLine();

            if (s == "ja")
            {
                switch (rnd)
                {
                    case 1:
                        Console.WriteLine("Ihr WÜrfel hat die Zahl 1 ergeben");
                        break;
                    case 2:
                        Console.WriteLine("Ihr WÜrfel hat die Zahl 1 ergeben");
                        break;
                    case 3:
                        Console.WriteLine("Ihr WÜrfel hat die Zahl 1 ergeben");
                        break;
                    case 4:
                        Console.WriteLine("Ihr WÜrfel hat die Zahl 1 ergeben");
                        break;
                    case 5:
                        Console.WriteLine("Ihr WÜrfel hat die Zahl 1 ergeben");
                        break;
                    case 6:
                        Console.WriteLine("Ihr WÜrfel hat die Zahl 1 ergeben");
                        break;
                    }
            }
            else
            {
                return;
            }
        }
    }
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Peppy
  • 5
  • 3
  • 2
    Did you step through the code? What's the value of `rnd` when it reaches the `switch` statement? – madreflection Aug 02 '22 at 16:26
  • 3
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 02 '22 at 16:28
  • 3
    All your outcomes are the same: `Ihr WÜrfel hat die Zahl 1 ergeben` – LarsTech Aug 02 '22 at 16:58
  • 1
    And also... replace "if (s == "ja")" by "if (s.ToLower() == "ja")"...just in case. – Ricardo Rodrigues Aug 02 '22 at 16:59

1 Answers1

2

This seems problematic:

Random r = new Random();
int rnd = Random.Range(1, 7);

Random.Range is part of Unity3D and requires using the UnityEngine namespace - is that what you meant to use? If not, and you're writing a plain .NET console app, you probably meant to write:

Random r = new Random();
int rnd = r.Next(1, 7);

By the way, a better way to print out the result would be to include it as part of the string you are printing, rather than using a switch case enumerating all possible values. That is, you can replace the entire switch statement with this:

Console.WriteLine($"Ihr WÜrfel hat die Zahl {rnd} ergeben");
Asik
  • 21,506
  • 6
  • 72
  • 131
  • Good old `Next(inclusive, exclusive)` followed by *string interpolation*, I guess, that OP wants some kind of *loop* as well to throw dice until `nein` input; +1 – Dmitry Bychenko Aug 02 '22 at 17:21