I've created a "Choose your Case" game using a cash prize array of all the prize amounts and the case numbers a user can pick. When they pick a number, it tells the user what is in their case. However, at the end it spits out all the cases and what was in each one, and the data is misaligned by one index.
static void Main(string[] args)
{
int[] cashPrizeArray = new int[26] { 0, 1, 2, 5, 10, 20, 50, 100, 150, 200, 250, 500, 750, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 50000, 75000, 100000, 200000 };
int[] caseArray = new int[26] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };
Array.Sort(cashPrizeArray);
Random rnd = new Random();
int[] cashPrizeArrayR = cashPrizeArray.OrderBy(x => rnd.Next()).ToArray();
foreach (int i in cashPrizeArrayR)
{
Console.Write("{0} ", i);
}
Console.WriteLine("\n(please ignore the numbers above)\n\n\nDeal or Not!");
Console.Write("Choose a case: 1-26: ");
int userCase = int.Parse(Console.ReadLine());
if (!caseArray.Contains(userCase))
{
Console.WriteLine("\nUnexpected input text.\nThis application will now be terminated.\nPress ENTER to continue...");
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("You chose case " + userCase);
Console.ReadLine();
}
Console.WriteLine("This case contains...\n$" + cashPrizeArrayR[userCase]);
Console.ReadLine();
Console.WriteLine("\nFor reference, below are the case numbers and their values: ");
Console.ReadLine();
var caseAndPrize = cashPrizeArrayR.Zip(caseArray, (p, c) => new { Prize = p, Case = c });
foreach (var pc in caseAndPrize)
{
Console.WriteLine("Case " + pc.Case + " $" + (pc.Prize));
}
Console.ReadLine();
}
It runs fine, but it outputs the value incorrectly, as if the data column is shifted downwards. Can anyone provide a solution where I have gone wrong? Many thanks.

Aucun commentaire:
Enregistrer un commentaire