I am having problem in this program that i have. i want to be able to print the random words from the void Spoken() method and also to be able to return the number of words that are counted using the Main() method.
static void Main ()
{
Spoken ();
string sentence = Spoken ();
string[] words = sentence.Split (' ');
Console.WriteLine("Words counted: " words.Length);
}
static void Spoken ()
{
var wordss = new string[]{
"\nfine day\n",
"\nnight time\n",
"\nexclusive place to unpack\n",
"\ndoing better at clicking\n",
"\nkilling time?\n",
};
var ran = new Random ();
var pc = ran.Next (words.Length);
Console.WriteLine (words[pc]);
}
sample output:
exclusive place to unpack
Words counted: 4
what should i do? and is there a way? any help would be much appreciated
CodePudding user response:
Change the method declaration to return string
and then return words[pc]
from the method.
Or if you want to return only the count if words then keep the Console.WriteLine
in the Spoken method and just return the array length fr the method.
If only printing to the console is the only motive then you can keep the method as returning void
and print inside the method only.
And if the intention is to just print the words and count then you can avoid Split
. You can just run a for loop and check for the IsWhiteSpace
and keep on incrementing counter value.