I am making a simple learning game where a number is generated and the user will have to enter the word. The problem is Im trying to get a hint button working basically when the user clicks the hint button a message box will display the first character of the string which is in the array.
Here is a example of what my array looks like.
static string[] numberList = { "one","two", "Three","four","five","Six","Seven","Eight","Nine","Ten"};
So if the number is 2 and the strings are always in that order, then you can do
var firstletter = numberList[2-1][0];
That will get you a Char. If you want that also as string then do
firstletter.ToString();
just like this
var first = numberList[2-1][0];
first.ToString();
I don't know if this is optimized, but I like using substring() because it's very obvious what I want.
numberlist[i-1].Substring(0, 1);
where 'i' is the number you are looking for (subtract 1 because arrays use a 0-based index)
Related
I am creating a student module form with multiple functions. I am having issues reading the minimum and maximum values within the list box. I have been struggling for days and would greatly appreciate any form of help. Thanks in advance!
I have tried using different arrays, storing different values ect. I thought the issues within the code came from no 'mark' being stored. But i am certain that it is working and believe the issues lies within the line of code in 15.
public int MinMark()
{
int lowest = int.Parse(ModuleData.studentMark[0]);
for (int index = 1; index < ModuleData.studentMark.Count;index++)
{
if (int.Parse(ModuleData.studentMark[index]) < lowest)
{
lowest = ModuleData.studentMark.ToString()[index];
}
}
return lowest;
So far my code is just outputting the first index on from the list. i have explored all my lecture notes and have tried anything i can think of to get it working.
The line
lowest = ModuleData.studentMark.ToString()[index];
is incorrect and would give you the char value of a character in the string. What the above line is doing is taking the mark which is a string, converting it to a string again, and then selecting the character at index index from that string.
Instead you want the the string as an integer which can be achieved as below
lowest = int.Parse(ModuleData.studentMark[index]);
lowest = ModuleData.studentMark.ToString()[index];
This line is almost certainly incorrect, as you're returning the string representation of your collection (likely something like "System.String[]" or "System.Collections.Generic.List'1[System.String]") and getting a character from the string by index and implictly converting that character to an integer. The line should likely be
lowest = int.Parse(ModuleData.studentMark[index]);
However, you can replace this method with a single LINQ query, like so:
public int MinMark() => ModuleDate.studentMark.Select(int.Parse).Min();
This will parse all the student marks to integers then select the smallest from the collection. If this is still only returning the first index, then the first index is likely the lowest value in your case, or the ModuleDate.studentMark field isn't being populated as you expect.
I'm new to programming I have this exercise where I have to Write a function filterLongWords() that finds and displays all words that are longer than a given integer i in a list that has been passed in as an argument and I cant display the words more than once and I don't know how to write it if any one here could tell me how or tell me a website I can go to for help that would be ok too.Thanks
A hint for u....
Pass a string array to that List<string> filterLongWords(string[] words) function.
Instantiate a list that you will return at the end.
Implement a foreach loop that iterates that array.
Now you look at the length of that current iterated word and if it doesn't fit your length put it in your instantiated list.
Return your instantiated list or convert it to an array and return that.
I won't code it for you - it's just a hint.
This is just to settle a curiosity - Suppose, in my C# project, I have a list containing millions of strings, each along the following lines:
"123Hi1234Howdy"
"Hi1Howdy23"
....
And all I need to know is, for each character in the string, if it is a digit or is it a letter.
So, I was thinking the easiest way to store this would be as 0's and 1's or True / False. So, in the example above, assuming I could assign IsLetter = 1 and IsDigit = 0, I could transform each line to:
"123Hi1234Howdy" >> 00011000011111
"Hi1Howdy23" >> 1101111100
....
That seems to me to be the most efficient way to store the data I'm looking for (but please do already correct me if I'm wrong on this - I'm still pretty much a newbie with programming).
So, writing the code that loops through a line and checks for whether each character is a digit or a letter and converting it to true/false or 1/0 is easy enough. My question is what would be the best way to store each line's output?
Should I store each line's output as a bit array? Could it be stored as some other type (maybe, say, integer) that could then be converted back to a series of bits? Should it be stored as a boolean array?
Any other thoughts on the best way to store this? When it's all said and done, I need to have a list where I can know, for example:
myList[0] = 00011000011111
myList[1] = 1101111100
And, then, therefore myList[0] <> myList[1]
You could use a BitArray for each word and set the bits to true or false if they are a digit or not. See this possible solution:
void Main()
{
string[] words =
{
"123Hi1234Howdy",
"Hi1Howdy23"
};
//Create an array of BitArray
var bArrays = words.Select(w => new BitArray(w.Select(c => char.IsDigit(c)).ToArray()));
//You can also create string too
var strings = words.Select(w => new string(w.Select(c => char.IsDigit(c) ? '1' : '0').ToArray())).ToArray();
}
This is not necessarily the fastest or most efficient. I guess it depends on what you intend to do with the strings, but at least it's simple!
I have created a string array named 'coin' and I am trying to output the content of the string array to a item box along with another integer variable, and I keep getting the list box saying String[] Array.
To create the array, I have used the following:
string[] coins = new string[4];
Then on the click of the enter button, a new item will be added to the array, along with the count variable which increments on the click of the button, then it outputs the contents of the coin array to the list box:
coins[valuesEntered] = "coin" + valuesEntered.ToString();
valuesEntered++;
listBox1.Items.Add(coins);
However, this seems not to work and instead just outputs String[] Array to the group box each time the enter button is clicked. I cant seem to get past this brick wall at the moment.
You are attempting to add an object to your ListBox, specifically a string array represented in syntax as String[] Array. You want the contents of the array, not the array itself.
You first need to join the individual contents of the array to a separate string variable before you attempt to add it to the ListBox.
string listOfCoins = string.Join(" ",coins);
listBox1.Items.Add(listOfCoins.ToString());
this might work too
listBox1.Items.Add(coins.Text);
I don't use Visual C# much, so take this with a grain of salt.
I want to provide the user with a selection of questions but I want them to be random, having a quiz game with the same questions isn't exactly fun.
My idea was to store a large collection of questions and there appropiate answers in a text file:
What colour is an Strawberry|Red
How many corners are there on a Triangle|Three
This means that I could simply select a line at random, read the question and answer from the line and store them in a collection to be used in the game.
I've come up with some pseudo code with an approach I think would be useful and am looking for some input as to how it could be improved:
Random rand = new Random();
int line;
string question,answer;
for(int i = 0; i < 20; i++)
{
line = rand.Next();
//Read question at given line number to string
//Read answer at given line number to string
//Copy question and answer to collection
}
In terms of implementing the idea I'm unsure as to how I can specify a line number to read from as well as how to split the entire line and read both parts separately. Unless there's a better way my thoughts are manually entering a line number in the text file followed by a "|" so each line looks like this:
1|What colour is an Strawberry|Red
2|How many corners are there on a Triangle|Three
Thanks for any help!
Why not read the entire file into an array or a list using ReadLine and then refer to a random index within the bounds of the array to pull the question/answer string from, rather than reading from the text file when you want a question.
As for parsing it, just use Split to split it at the | delineator (and make sure that no questions have a | in the question for some reason). This would also let you store some wrong answers with the question (just say that the first one is always right, then when you output it you can randomize the order).
You don't want to display any questions twice, right?
Random random = new Random();
var q = File.ReadAllLines("questions.txt")
.OrderBy(x=>random.Next())
.Take(20)
.Select(x=>x.Split('|'))
.Select(x=>new QuestionAndAnswer(){Question=x[0],Answer=x[1]});