Keep asking user to type lowercase letter in c#. Give an error when letter is uppercase
Program ends when user types "!"
I don't know how to continuously asking users for inputs in my loop.
Console.WriteLine("Type a lowercase letter.");
char letter = char.Parse(Console.ReadLine());
while(letter !='!')
{
if(char.IsLower(letter))
{
Console.WriteLine("OK. Type another lowercase letter");
}
else
{
Console.WriteLine("Error");
}
letter = char.Parse(Console.ReadLine());
break;
}
Answers (2)
It looks like you are trying to learn some basic C# techniques. So another example that does what you say - with a different paradigm:
static void Main(string[] args)
{
var printout = new string[] { "Type a lowercase letter.", "OK. Type another lowercase letter", "Error" };
Console.WriteLine(printout[0]);
var sequance = SequenceOf(() => (Console.ReadLine() + " ")[0])
.TakeWhile(x => x != '!');
foreach (var item in sequance)
{
Console.WriteLine(printout[char.IsLower(item) ? 1 : 2]);
}
}
public static IEnumerable<T> SequenceOf<T>(Func<T> generator)
{
while (true)
{
yield return generator();
}
}
Once you get comfortable with this paradigm - it makes for more readable code in my opinion, and the way to read it is:
- spit out canned response
- generate a sequence (of line reads)(first char)
- take from that sequence while the char is not !.
- for each item in sequence - spit out canned response (conditional)
Explanation for some of my choices
- I use the printout var to store canned responses. In a bigger app you would rather use resources to do something similar. sometimes the canned responses will be formatted responses this paradigm is still useful in that case. read more about resource management in the msdn.
- the SequenceOf util func I wrote. Note that in C# a functions that return IEnumerable can be implemented in one of two fashions - the simple return new IEnumerable - returning an IEnumerable object - is the WYSIWYG way of implementing that func. The other more interesting way will be the iterator pattern this lets you write pretty looking code that just executes when the created IEnumerable's MoveNext function is called - and it 'yields' back to your code once it was either able to create your next value or it reached the end of the sequence - very neat and very useful code pattern. (the compiler recognizes the yield keyword and re-formats your code in a class implementing the IEnumerable interface. this is done automagically your own method is then rewritten by the compiler as "return new AutoGeneratedIEnumerableClass()" and the debugging line info is kept so you can have a nice debug session later)
- you will further notice that the SequenceOf util functions is generic - that is it can be used to create a sequence of char - or of anything else. you can learn about generics in c# here
- further more SequenceOf accepts a Func delegate parameter. delegates are just method objects. they are special objects that can hold a reference to a single method, and then be called just as a method can be with the () notation. In earlier versions of C# and the .net FW it made a lot of sense to create your own delegate objects with the delegate keyword. but microsoft aims to please - and in the latest versions of the FW you have generic reusable delegate objects that are suitable for most of your real world needs. learn more about delegates here
- TakeWhile is a part of LINQ there is not enough praise to say about LINQ. I use it when I can. It makes for very readable code.
- notice in the Main function when I use the SequenceOf I don't pass simple functions (though i can) - instead I use c#'s shorthand to write functions - lambda expressions - these, though they are just syntactic sugar, enable you to write clearer code when passing simple functions (simple behavior) to other functions - by the way - when a function gets another function as a parameter - it is called a higher order function
- last but not least there is the ternary if - many people frown upon the question mark notation - I like to use in where the term is small and visually pleasing.
Hope this gets you places you wanted to go.
Have fun.
As @Steve suggested, remove the break. When you're in a loop, break
statement will instantly bring you outside of that loop.
In your case, your while was looping only once.
Read more here : https://msdn.microsoft.com/en-us/library/adbctzc4.aspx
Console.WriteLine("Type a lowercase letter.");
char letter;
Char.TryParse(Console.ReadLine(), out letter);
while (letter != '!')
{
if (char.IsLower(letter))
{
Console.WriteLine("OK. Type another lowercase letter");
}
else
{
Console.WriteLine("Error");
}
Char.TryParse(Console.ReadLine(), out letter);
}
Changed the Parse
to TryParse
since you can handle the error yourself. Well, you can surround Parse
With a Try...Catch but exception are slow.
Parse v. TryParse
Why is throwing exceptions so slow?
all credit should go to @Steve