开发者

Trying to understand why my choice in an array doesn't work

开发者 https://www.devze.com 2023-03-17 18:05 出处:网络
Sorry for the ambiguous title. I\'m kind of new to programming and this issue is a bit weird so I didn\'t know how to word it.

Sorry for the ambiguous title. I'm kind of new to programming and this issue is a bit weird so I didn't know how to word it.

I'm simply practicing on creating an array populated with colors and the user just have to pick a color by picking 1-9. The issue is that I get an error message:

Use of unassigned local variable 'UserPickNum'

I already assigned it to an int and the only solution that will fix this if I created another construct, runs Console.WriteLine(colors[UserPickNum]); picks the color, then the program runs fine.

I guess my question is, why won't it work like this without having to create and call for another construct.

using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main()
    {
        string[] colors;
        string Pick;
        int PickNum;
        int UserPickNum;

        Console.WriteLine("Which color do you want? Pick 1-9");
        Pick = Console.ReadLine();
        if (int.TryParse(Pick, out PickNum))
        {
            UserPickNum = Convert.ToInt32(Pick);
        }
        else
        {
            Console.WriteLine("This isn't a valid number");
            Console.ReadKey();
            Main();
        }

        colors = new string[10] { "black", "white", "green", "purple", "red", "brown", "blue", "gray", "yellow", "indigo" };
        Console.WriteLine(colors[UserPickNum]);
       Main(); 开发者_如何转开发
    }


If Pick turns out not to be a number and thus int.TryParse returns false, then UserPickNum will not be assigned to a value. Probably the easiest way to resolve this is to assign a value to UserPickNum where you declare it, i.e.:

int UserPickNum = Int32.MinValue;


Your code only has one route through the if statement where you set UserPickNum - hence the warning. However, you don't need to call Convert.ToInt32 on Pick as you already have the number as an integer - in PickNum - assuming that the user entered a valid integer. This means you don't need UserPickNum at all. So you could change your code to be:

if (!int.TryParse(Pick, out PickNum))
{
    Console.WriteLine("This isn't a valid number");
    Console.ReadKey();
    Main();
}

colors = new string[10] { "black", "white", "green", "purple", "red", "brown", "blue", "gray", "yellow", "indigo" };
Console.WriteLine(colors[PickNum]);
Main(); 

You should also check that it's in range before trying to use it as an array index:

if (PickNum < 0 || PickNum > 9)
{
    Console.WriteLine("Input out of range");
    // Try again
}


That error means that you are trying to use a variable that hasn't had a value given to it yet. You say "I already assigned it to an int", but that's not really an assignment. The assignment is when you give it a value (e.g. int UserPickNum = 0).

In your code it looks you're attempting to make sure the value is assigned with your if/else statement, but the else clause doesn't actually stop the rest of the code after it from executing. You would need to return after you recursively call Main().


The error message you are getting is because your variable UserPickNum isn't initialized (It isn't set in your else block, and thus there is potentially a path through your code where it isn't set to a value).

You can set it to a default value when you declare it to fix this problem:

int UserPickNum = 0;

Other Feedback

Something else I noticed is that you don't even need the UserPickNum variable. In this part of your code:

Pick = Console.ReadLine();
if (int.TryParse(Pick, out PickNum))
{
    UserPickNum = Convert.ToInt32(Pick);
} 

the Variable PickNum contains the value that the user typed in as an integer. You can streamline this part of the code like this:

Pick = Console.ReadLine();
if (!int.TryParse(Pick, out PickNum))
{
    // code you are currently doing in your else block
    Console.WriteLine("This isn't a valid number");
    Console.ReadKey();
    Main();
} 

// code you are currently doing after your if else block
colors = new string[10] { "black", "white", "green", "purple", "red", "brown", "blue", "gray", "yellow", "indigo" };
Console.WriteLine(colors[PickNum]);
Main(); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号