Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am trying to check user input which supposes to be a four-digit account number only. if the user enters more or less input. it would say "invalid entry". I am not sure how to check var length in c#.
Console.WriteLine("Enter a four-digit account number");
int acc = Convert.ToInt32(Console.ReadLine());
int d = acc % 10;
acc = acc / 10;
if (acc % 7 == d)
Console.WriteLine("Valid Account Number");
else
Console.WriteLine("Invalid Account Number");
Updated
var inputText = Console.ReadLine();
var inputNumber = 0;
// try to parse - if success, then parsingFlag = True
// and parsed number is stored to inputNumber
var parsingFlag = Int32.TryParse(inputText, out inputNumber);
if ((inputNumber >= 1000) && (inputNumber <= 9999))
{
// do your algorithm
}
else
Console.WriteLine("Invalid entry");
Related
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 1 year ago.
Improve this question
I want a code to generate sequence of Alphanumeric character like LLNNLLNNLL where L is the Letter and N is the number. For example if the length is 5 the sequence will be LLNNL and if the length is 6 the sequence will be LLNNLL and if 7 it would be LLNNLLN.
string alphabets = "ABCDEFGHIJKLMNPQRSTUVWX";
int length = model.VALUE_INT;
string result = "";
for (int i = 0; i < length; i++)
{
int next = _random.Next(23);
result += alphabets.ElementAt(next);
result += _randomNum.Next(1, 9);
}
This is what I have tried but condition is not fulfilling
For each position (value of i) of your output string you need to check if you need a character or an integer.
Since the pattern repeats every 4 positions, you can achieve this using the modulo operator:
for (int i = 0; i < length; i++)
{
switch (i % 4)
{
case 0:
case 1:
int next = _random.Next(23);
result += alphabets.ElementAt(next);
break;
case 2:
case 3:
result += _randomNum.Next(1, 9);
break;
}
}
Or another possibility: Add blocks of LLNN and then truncate the result to the needed length...
for (int i = 0; i <= (length/4); i++)
{
result += alphabets.ElementAt(_random.Next(23));
result += alphabets.ElementAt(_random.Next(23));
result += _randomNum.Next(1, 9);
result += _randomNum.Next(1, 9);
}
result = result.Substring(0,length);
If you want to improve these lines, you can use a StringBuilder
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
string txte = save.ToString();
for (int i = 7; i < save.Length; i= i+8)
{
outs.Append(txte.Substring(i-7,i)+ " ");
}
Given
public static class Extension
{
public static IEnumerable<string> Chunks(this string input, int size)
// select with index
=> input?.Select((x, i) => i)
// filter by chunk
.Where(i => i % size == 0)
// substring out the chunk, or part thereof
.Select(i => input.Substring(i, Math.Min(size,input.Length - i)));
}
Usage
var s = "11111111222222223333333344444444";
foreach (var result in s.Chunks(8))
Console.WriteLine(result);
Console.WriteLine("---");
// or add space
Console.WriteLine(string.Join(" ", s.Chunks(8)));
Results
11111111
22222222
33333333
44444444
---
11111111 22222222 33333333 44444444
As long as you are sure your input has a length which is the exact multiple of 8 characters then this works easily:
var output =
String.Join(
" ",
Enumerable.Range(0, txt.Length / 8).Select(x => txt.Substring(x * 8, 8)));
If you need to ensure that the input has a length which is the multiple of 8 then do this first:
txt = txt.PadRight(txt.Length + txt.Length % 8 == 0 ? 0 : 8 - txt.Length % 8);
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 years ago.
Improve this question
I have to make console program that asks the user to enter number, then the program has to make square of stars (*) equal to the number that the user input.
Console.WriteLine("enter number and press ENTER");
int userNumber = int.Parse(Console.ReadLine());
int rowNumber = 0;
int lineNumber = 0;
int counter = 0;
while(counter < userNumber) {
Console.Write("*");
while(lineNumber < userNumber) {
Console.WriteLine("*");
lineNumber++;
}
counter++;
}
Console.WriteLine();
example:
user input :5
*****
* *
* *
* *
*****
you can do the following:
explanation for the code:
Square is formed of 2 equal sides, so in order to draw the square using *, you will think of it like a matrix
you need a loop to draw the rows and another loop to draw the columns, and both loop has upper limit which is the number entered by the user.
the rule of drawing is like this i will put star if i am in the first row or the last one or in the first column or last, so by addressing this i used the if statement ( i==0 || i== number-1 || j==0 || j== number -1) where i is the row and j is the column, and if this condition is not satisfied, print space
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Please enter a number:");
var number=Convert.ToInt32(Console.ReadLine());
for(int i=0; i < number; i++)
{
for(int j=0; j < number; j++)
{
if(i==0 || i == number-1 || j==0 || j == number-1)
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
}
}
here a working DEMO
hope this will help you
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
if (i%2==0)
{
Console.WriteLine(i);
}
}
}
}
It's start from 0 but you can't see.Try this:
for (int i = 0; i < 1000; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
if (i % 100 == 0) Console.ReadKey();
}
Press enter to see next numbers.That will show 100 numbers at once,you can change it if you want to display less number..
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 9 years ago.
Improve this question
I am having trouble with this. How can I count a length I want input to.
Lets say I ask a user an input. He enters 10. It counts 1,2,3,4,etc... 10.
Or if users enter 5, 1,2,3,4,5 is output
Thanks.
EDIT:
I am sorry. This isn't homework. School doesn't start till next week and I am practicing.
Sorry, I should have given code.
This is what I had that does work
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i < length; i++)
{
Console.WriteLine(i);
}
I am just assuming since I am new that I did some sloppy code and am looking for maybe something cleaner. Or another point of view for it.
update your code with <=
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
You just need to change the '<' operator to '<=':
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
string length;
Console.Write("Enter Length: ");
length= Console.ReadLine();
for (int i = 1; i <= Int32.Parse(length); i++)
{
Console.WriteLine(i);
}
Console.ReadKey();