C
C#2y ago
Ryan-T1412

❔ ✅ error CS0150 A constant value is expected

using MoreLinq;

string[] cards = {"rock", "paper", "scissors"};

Dictionary<string, string> winningSituations = new Dictionary<string, string>
{
{"rock", "scissors"},
{"paper", "rock"},
{"scissors", "paper"}
};

Random random = new Random();

string botChoice = cards.Shuffle(random).First();
Console.WriteLine(botChoice);

Console.WriteLine("Enter your choice: (rock, paper, scissors)");
string userChoice = Console.ReadLine().ToLower();

if (!cards.Contains(userChoice))
{
Console.WriteLine("Invalid input!!!");
}
if (userChoice == botChoice)
{
Console.WriteLine("Draw!");
}else
{
switch (winningSituations[userChoice])
{
case botChoice:
Console.WriteLine("You Won the Round!");
break;
default:
Console.WriteLine("You Lost the Round!");
break;
}
}
using MoreLinq;

string[] cards = {"rock", "paper", "scissors"};

Dictionary<string, string> winningSituations = new Dictionary<string, string>
{
{"rock", "scissors"},
{"paper", "rock"},
{"scissors", "paper"}
};

Random random = new Random();

string botChoice = cards.Shuffle(random).First();
Console.WriteLine(botChoice);

Console.WriteLine("Enter your choice: (rock, paper, scissors)");
string userChoice = Console.ReadLine().ToLower();

if (!cards.Contains(userChoice))
{
Console.WriteLine("Invalid input!!!");
}
if (userChoice == botChoice)
{
Console.WriteLine("Draw!");
}else
{
switch (winningSituations[userChoice])
{
case botChoice:
Console.WriteLine("You Won the Round!");
break;
default:
Console.WriteLine("You Lost the Round!");
break;
}
}
11 Replies
Ryan-T1412
Ryan-T1412OP2y ago
idk why i'm getting this error but its first time that i tried to use a module i don't think switch case causing the error
ero
ero2y ago
it is the case causing te error
Ryan-T1412
Ryan-T1412OP2y ago
i have no idea string botChoice = cards.Shuffle(random).First(); maybe this
Susko3
Susko32y ago
hint: don't use a switch-case in this scenario as the string isn't a compile-time constant
Ryan-T1412
Ryan-T1412OP2y ago
so i can not use switch-case with strings
ero
ero2y ago
yes you can
Ryan-T1412
Ryan-T1412OP2y ago
or dictionary
ero
ero2y ago
but only when they're constant
Ryan-T1412
Ryan-T1412OP2y ago
ok i replaced switch-case with if else statement
if (winningSituations[userChoice] == botChoice)
{
Console.WriteLine("You Won the Round!");
}else
{
Console.WriteLine("You Lost the Round!");
}
if (winningSituations[userChoice] == botChoice)
{
Console.WriteLine("You Won the Round!");
}else
{
Console.WriteLine("You Lost the Round!");
}
now it works !close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server