简体   繁体   中英

Namespace problems (missing }?) and passing params to method for displaying arrays

Two problems with my code:

1- I'm getting weird syntax errors with Console.Writeline in Main(), and I think I have a missing right curly brace '}'

2- I can't seem to figure out my first method after Main(). It's just supposed to be a simple void method to write the elements of an array, but Visual Studio seems to think it's either a class or namespace from the errors.

Can anyone spot where I screwed up?

public static void Main(string[] args)
{
    //static array for winning[6], empty for player[6], empty for matching[6]
    int [] winning = new int [6] {2, 4, 6, 9, 1, 3};
    int [] player = new int [6];
    int [] matching = new int [6];
    int inValue;

    //Input loop
    Console.WriteLine("Please enter six lotto numbers, between 1 and 9");

    for (int i = 0; i < player.Length; i++)
    {
        inValue = Console.Read();
        if (inValue < 1 || inValue > 9) //Validate for int 1-9
        {
            Console.WriteLine("Please enter a whole number between 1 and 9");
        }
        winning[i] = inValue;
    }

    //Output
    Console.WriteLine("The winning numbers were:");
    DisplayArray(int[] winning);
    Console.WriteLine("Your numbers were:");
    DisplayArrayContents(int[] player);
    Console.WriteLine("You had " + MatchCount() + " matches.");
    Console.WriteLine("Your matching numbers are:")
    DisplayArrayContents(int[] matching);
    Console.Read();
}

//Empty method to display arrays
static void DisplayArray(params int[] args)
{
    for (int i = 0; i < args.Length; i++)
    {
        Console.Write({0} + "\t", array[i]);
    }
    Console.Write("\n");
} 

Edit: Thanks everyone! I forgot to rename some variables and methods in there, but the main problem was a missing ; and unnecessary data types as arguments in Main().

A couple of things to clean up your syntax errors:

1-To display values in your array "args" (this is passed in as a parameter to DisplayArray in your method signature), change "array[i]" to "args[i]".

static void DisplayArray(params int[] args)
{
    for (int i = 0; i < args.Length; i++)
    {
        Console.Write("{0}\t", args[i]);
    }
    Console.Write("\n");
} 

2-When calling DisplayArray, you just need to pass in the instance of the array you want to operate on in the method, so change the calls from:

DisplayArray(int[] winning);
DisplayArrayContents(int[] player);

to:

DisplayArray(winning);
DisplayArrayContents(player);

Good luck!

UPDATE

Here is a working prototype:

class Program
{
    static void DisplayArray(params int[] args)
    {
        for (int i = 0; i < args.Length; i++)
        {
            Console.Write("{0}\t", args[i]);
        }
        Console.Write("\n");
    }

    static void Main(string[] args)
    {
        //static array for winning[6], empty for player[6], empty for matching[6]
        int [] winning = new int [6] {2, 4, 6, 9, 1, 3};
        string[] tmp = new string[6];
        int [] player = new int [6];
        int [] matching = new int [6];
        string line;
        int inValue;
        bool valid;

        //Input loop
        do
        {
            Console.WriteLine("Please enter six lotto numbers between 1 and 9, separated by spaces: ");
            valid = true;
            line = Console.ReadLine();
            tmp = line.Split(' '); //split on space
            for (int i = 0; i < tmp.Length; i++)
            {
                int.TryParse(tmp[i], out inValue);
                if (inValue < 1 || inValue > 9) //Validate for int 1-9
                {
                    Console.WriteLine("{0} must be a whole number between 1 and 9", tmp[i]);
                    valid = false;
                }
                player[i] = inValue;
            }
        }
        while (!valid);

        //Output
        Console.WriteLine("The winning numbers were:");
        DisplayArray(winning);
        Console.WriteLine("Your numbers were:");
        DisplayArray(player);
        //Console.WriteLine("You had " + MatchCount() + " matches.");
        //Console.WriteLine("Your matching numbers are:")
        //DisplayArrayContents(matching);
        //Console.Read();


        string retVal = "";
        while(retVal != "exit")
        {
            Console.WriteLine("Type 'exit' to end the program: ");
            retVal = Console.ReadLine();
            if (retVal == "exit")
                Environment.Exit(0);
        }
    }
}
//Empty method to display arrays
static void DisplayArray(params int[] args)

This should be static void DisplayArray(int[] array) , as you're passing in arrays anyhow and there's no need for the performance hit of varargs (that's what you do when you declare the parameter as params int[] args .


Console.Write({0} + "\t", array[i]);

You don't concatenate format strings together like this. You can just use:

Console.Write("{0}\t", array[i]); 

Also, array is not defined inside DisplayArray() of your implementation. You've named your parameter args


DisplayArrayContents(int[] matching);

You don't have a DisplayArrayContents method, and you don't need to specify the type of what you're passing in again. The compiler can guarantee type safety at the point of calling, so you would just pass it in by name,

DisplayArray(matching);

Missing a semi-colon here:

Console.WriteLine("Your matching numbers are:")

In your input loop, you are writing the player input to the winning array, which means that they would end up always picking the winning numbers, by virtue of that they just put them in.

You're likely wanting player[i] = inValue;


inValue = Console.Read()

This does not do what you're likely expecting. Try a program with just:

var inValue = Console.Read();
Console.WriteLine(inValue);

and at the console, input 1. You'll get an output different that 1 , because Console.Read() return char values.

A combination of Console.ReadLine() , Int32.Parse() and perhaps String.Split() will get you where you need to go.

You should not pass a type along with the parameter when you call a method. So for example DisplayArray(int[] winning); should be just DisplayArray(winning); Fix all those errors and you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM