Textbox value to array or list [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I enter the value in textbox:
1,2,3,4,5,6,7...
How to into to int array or list and show value display ???
And stop when a user enters the wrong consecutive 3 times or total error of 5

//textbox is your textbox controls' name
string[] content = textbox.Text.Split(',');
//use linq convert stringp[] to int[]
int[] nums = (from num in content
select int.Parse(num)).ToArray();

I don't understand the last part of your question (the bit about stopping when a user enters something wrong... I guess you'd have to define "wrong" in this context). Regardless, the following code should take values entered into a text box (let's call it txtNumbers) and put it in an array of ints.
string[] splitContent = txtNumbers.Text.Split(new char[] { ',' });
int[] nums = new int[splitContent.Length];
for (int i = 0; i < splitContent.Length; i++)
{
nums[i] = Int32.Parse(splitContent[i]);
}

Related

Console.ReadLine().Contains Console.ReadLine()? why do i get false when the number is present in both? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Could someone help me why do i get true only if the numbers are the same? I need to find out if the second input is in the first one. True/false
int[] firstNumber;
firstNumber = new int[10];
firstNumber[0] =
Convert.ToInt32(Console.ReadLine());
int secondNumber =
Convert.ToInt32(Console.ReadLine()) ;
Console.WriteLine(firstNumber.Contains(secondNumber));
Edit. I need to get true for ex.:firstNumber 5214 and secondNumber 4
how can i get true for 5432(firstNum) and 3(secondNum)
Use strings instead of integers, which will use string.Contains instead of Array.Contains:
string firstNumber = Console.ReadLine();
string secondNumber = Console.ReadLine();
Console.WriteLine(firstNumber.Contains(secondNumber));

Splitting numbers into list/array (e.g. 1998 to 1,9,9,8) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
this is my first post so excuse me if I am not being clear enough.
As the title says I want to split a number into smaller parts, for example 1998 to 1,9,9,8. I have looked around but I am stuck trying to figure out the right term to search for.
Reason why I want this done is to later multiply every other number with either 1 or 2 to be able to examine whether it's correct or not.
Sorry I can't provide any code because I am not sure how I should tackle this problem.
As #mjwills said, mod is what you want.
static IEnumerable<int> Digitize(int num)
{
while (num > 0)
{
int digit = num % 10;
yield return digit;
num = num / 10;
}
}
And then call it like so:
static void Main(string[] _)
{
int num = 1998;
int[] digits = Digitize(num).ToArray();
Console.WriteLine($"Original: {num}");
foreach (var digit in digits)
Console.WriteLine(digit);
}
make a string of that number and make it like this
```
int a = 1990;
int[] intArray = new int[a.ToString().Length];
int index = 0;
foreach( char ch in a.ToString())
{
intArray[index] = int.Parse(ch.ToString());
index++;
}

calculating multi squares in one textbox in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm working on windows form application and I have homework to calculate the square of 5 numbers in one textbox and I have to put all 5 numbers in different lines
I expect the results to be like this,
you enter 5 numbers in each line
4
9
16
25
and get them in one single message box saying the results like
2, 3, 4, 5
Pass the Textbox Text into the function and It will Print all the SqureRoot of the Numbers
private void squreRootFinder(string textBox)
{
string[] numbers = textBox.Split('\n');
string data = "";
for (int i = 0; i < numbers.Length; i++)
{
data += Math.Sqrt(Convert.ToInt32(numbers[i])) + " ";
}
MessageBox.Show(data);
}

Sum of the elements per square range of int's in List<int> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My list is:
var list = new List<int>()
{
1, 2, 3, 4
};
Without doing
int sum = list[0]*list[0] + list[1]*list[1] + list[2]*list[2] + list[3]*list[3]
var result = list.Sum(o => o * o);
While the other answers work as well, you should learn how to use a loop. It is a fundamental building block of programming. For example, we can use a foreach loop to iterate over each element in the list. see:
int sum = 0;
foreach (int val in list)
sum += val * val;

Grid of numbers that randomizes on click [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a game that involves finding and clicking on a number (0-9) in a grid that randomizes each time you click on the correct one.
I want to get it so that when you click on the correct number, the grid randomizes again.
How would you do this?
Here's what it would kind of look like in the end:
I assume you're rendering an array of integers in order:
for (int i = 0; i < arrayOfNumbers.Length; i++ ) {
// rendering here
render(arrayOfNumbers[i]);
}
If thats the case.. just randomize the array after a successful click.. somewhat like this:
var rnd = new System.Random();
var arrayOfNumbers = Enumerable.Range(1, 9).OrderBy(r => rnd.Next()).ToArray();
Then you can just re-render (or let your game loop continue to render the array). Since the array has changed, your rendering will too.
Every time you detect a click on the correct number (I hope you know how to do this) you simply randomize the array of numbers you're displaying in your grid:
//Fisher-Yates algorithm
Random generator = new System.Random();
int len = array.Length;
while (len > 1)
{
len--;
int k = generator.Next(len + 1);
int temp = array[k];
array[k] = array[len];
array[len] = temp;
}

Categories