简体   繁体   中英

need help for a simple console program c#

needing a simple console program for c# where i ask the user to input random arguments consists of mixed integer/string. and then i have to locate integers and use it as the number of times to print the strings in the input args so far i have these

Console.Write("please type in random letters, words, numbers: ");

string uInput = Console.ReadLine();

args.Count(uInput);

was told output should be somthing like this

some ran 3 dom st uffs lke this 9 <<<< user input

dom st uffs lke this 9 <<< pront output after user input

dom st uffs lke this 9

dom st uffs lke this 9

i'm hitting a wall here as i am not really sure how to use the "args" method. thank you for any response.

In Main, we access a string array called args. This array is populated with command-line arguments from the operating system. This makes it possible to configure programs with minimal complexity.

if (args == null)
{
    Console.WriteLine("args is null"); // Check for null array
}
else
{
    Console.Write("args length is ");
    Console.WriteLine(args.Length); // Write array length
    for (int i = 0; i < args.Length; i++) // Loop through array
    {
        string argument = args[i];
        Console.Write("args index ");
        Console.Write(i); // Write index
        Console.Write(" is [");
        Console.Write(argument); // Write string
        Console.WriteLine("]");
    }
}

This will show you how the command-line parameters are received from a Windows command line.

Your console app will have arguments in the form of an array. You need to iterate thru each item and perform some validation to determine if the value is an integer value or a string. Once you have determined the type you can store is in another array and that can used to store only integers. Then you can iterate thru that and do what you must.

Another way is to look into linq and iterate thru a filtered result set.

Not quite sure what you need exactly, args is a array or strings. Usually it allows us to read output CommandLine parameters.

public static void Main(string[] args)
{
    if(args!= null && args.Length >0)
    {
        // Read parameters as args[0]. args[1] etc...
    }
    else
    {
        // if there are no commandline parameters/arguments.
        var input = Console.ReadLine(); // Read's user input.

        for(int i=0;i<3;i++) // Since you want to print user input 3 times,you can do this way. 
        {
            Console.WriteLine(input);
        }           
    }               
}

Take a look at some sample Fiddler Demonstration

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