In: Computer Science
C# programming
Write a number guessing game using System.Collections.Generic.Dictionary.
e.g. Suppose we generate the following random numbers and penalty money:
1 |
3 |
4 |
6 |
7 |
11 |
12 |
13 |
19 |
20 |
333 |
234 |
12 |
500 |
1569 |
9900 |
23 |
2 |
588 |
23 |
If a user guess 5 and 3, we output "You earned 234 in total!"
If a user guess 1 and 13, we output "You earned 335 in total!"
If a user guess 1 and 1, quit
* Please use seeded random generation to help you debug your code, i.e. "Random rand = new Random(0);"
**You may have the following properties/methods useful: Count, ContainsKey, Add
Please copy and paste the final working program here:
If you have any doubts, please give me comment...
using System;
using System.Collections.Generic;
class GuessingGame{
public static void Main(){
Dictionary<int, int> prizes = new Dictionary<int, int>();
Random rndGen = new Random(0);
int num, money;
int i=0;
while(i<10){
num = rndGen.Next(1, 20);
money = rndGen.Next(1, 10000);
if(!prizes.ContainsKey(num)){
prizes.Add(num, money);
i++;
}
}
Console.Write("Enter two numbers(space separated): ");
string[] line = Console.ReadLine().Split(' ');
int num1 = Int32.Parse(line[0]);
int num2 = Int32.Parse(line[1]);
if(num1==num2 || num1<1 || num1>20 || num2<0 || num2>20)
Console.WriteLine("quit");
else{
int prizeMoney = 0;
if(prizes.ContainsKey(num1))
prizeMoney += prizes[num1];
if(prizes.ContainsKey(num2))
prizeMoney += prizes[num2];
Console.WriteLine("You earned {0} in total!", prizeMoney);
}
}
}