I'm working a little with switch statements and want to know how to ignore the case sensitivity when it comes to input values.
Here is my code:
using System;
namespace SwitchStatements
{
class MainClass
{
public static void Main(string[] args)
{
Start:
Console.WriteLine("Please Input the Grade");
char grade = Convert.ToChar(Console.ReadLine());
switch (grade)
{
case 'A':
Console.WriteLine("Excellent Work!");
break;
case 'B':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
break;
}
Console.ReadKey();
goto Start;
}
}
}
If I put 'a' in instead of 'A' it returns the default response.
Can I use perhaps a .Comparison of some sort? If so where would I put it?
You can use ConsoleKey as condition for switch, the code will be like the following.
var grade =Console.ReadKey().Key;
switch (grade)
{
case ConsoleKey.A:
Console.WriteLine("Excellent Work!");
break;
case ConsoleKey.B:
// Something here
break;
case ConsoleKey.C:
// Something here
break;
case ConsoleKey.D:
// Something here
break;
case ConsoleKey.E:
// Something here
break;
default:
// Something here
break;
}
So that you can avoid converting the input to uppercase/Lower case, and then it goes for another conversion To Char. Simply use ConsoleKey Enumeration inside the switch.
You can use ToUpper(); Like
Convert.ToChar(Console.ReadLine().ToUpper());
and to get saved from the error of getting more charaters with Console.ReadLine() you can use
char grd = Convert.ToChar(Console.ReadKey().KeyChar.ToString().ToUpper());
you can use like following also
char grade = Convert.ToChar(Console.ReadLine().ToUpperInvariant());
https://msdn.microsoft.com/en-us/library/system.string.toupperinvariant.aspx
Convert to uppercase before switch like below,
grade = Char.ToUpper(grade);
Change
char grade = Convert.ToChar(Console.ReadLine());
To
char grade = Convert.ToChar(Console.ReadLine().ToUpper());
https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx
Write Switch on grade.ToUpper() like this and don't change change it's value, may be you will need it after
char grade = Convert.ToChar(Console.ReadLine());
switch (grade.ToUpper())
{
case 'A':
Console.WriteLine("Excellent Work!");
break;
case 'B':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
break;
}
You may fall from one case to another like this
public static void Main(string[] args)
{
Boolean validInputRead = false;
Char grade;
while(!validInputRead)
{
validInputRead = true;
Console.WriteLine("Please Input the Grade");
grade = Convert.ToChar(Console.Read());
switch (grade)
{
case 'A':
case 'a':
Console.WriteLine("Excellent Work!");
break;
case 'B':
case 'b':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
case 'c':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
case 'd':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
validInputRead = false;
break;
}
Console.ReadKey();
}
}
EDIT
Changed from Console.ReadLine() to Console.Read() as suggested
Added while(!validInputRead) as requested
string letterGrade;
int grade = 0;
// This will hold the final letter grade
Console.Write("Input the grade :");
switch (grade)
{
case 1:
// 90-100 is an A
letterGrade = "A";
Console.WriteLine("grade b/n 90-100");
break;
case 2:
// 80-89 is a B
letterGrade = "B";
Console.WriteLine("grade b/n 80-89");
break;
case 3:
// 70-79 is a C
letterGrade = "C";
Console.WriteLine("grade b/n 70-79");
break;
case 4:
// 60-69 is a D
letterGrade = "D";
Console.WriteLine(" grade b/n 60-69 ");
break;
default:
// point whic is less than 59
Console.WriteLine("Invalid grade");
break;
}
Related
I want to write a code in C# that asks a user to input 2 numbers to compare which is higher but cant seem to get an output from this.
{
class Program
{
public static int Max { get; private set; }
static void Main(string[] args)
{
Console.WriteLine("Please enter 2 (two) integer numbers on a separate line: ");
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
switch (Max)
{
case 1:
if (num1 < num2)
{
Console.WriteLine(num2 + "is Maximum");
}
break;
case 2:
if (num1 > num2)
{
Console.WriteLine(num1 + "is Maximum");
}
break;
}
}
}
}
You are using the "Max" variable on the condition of the switch, but you never actually declare "Max", and the switch is only expectating the numbers 1 or 2, because you put case 1: and case 2:
So the correct way to show output is this:
if(num1 > num2)
{
Max = 1;
}
else
{
Max = 2;
}
switch(Max)
{
case 1:
Console.WriteLine(num1 + "is Maximum");
break;
case 2:
Console.WriteLine(num2 + "is Maximum");
break;
}
This is just in case you need to use a switch case, because you can do it only with a simple if
case cannot be used to compare values of two numbers you might just want to use if-else statement or if you just want a application of case statement use this :
switch (input)
{
case 1:
Console.WriteLine("a");
break;
case 2:
Console.WriteLine("b");
break;
case 3:
Console.WriteLine("c");
break;
case 4:
Console.WriteLine("d");
break;
case 5:
Console.WriteLine("e");
break;
case 6:
Console.WriteLine("f");
break;
case 7:
Console.WriteLine("g");
break;
default:
Console.WriteLine("Invalid input");
break;
}
Problem: How to write this if-codeblock into a switch case? I have only a problem with the boolean.
int Note = 0;
Console.WriteLine("What is your note?: ");
Note = Convert.ToInt32(Console.ReadLine());
if ((Note < 1) || (Note > 6))
{
Console.WriteLine("Your input is wrong!");
}
else
{
if (Note <= 4)
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
}
}
I tried to write this codeblock into a switch-method.
int Note = 0;
Console.WriteLine("What ist your note?: ");
Note = Convert.ToInt32(Console.ReadLine());
switch (Note)
{
case Note < 1 || Note > 6:
Console.WriteLine("Your input is wrong!");
break;
case Note <= 4:
Console.WriteLine("Passed");
break;
}
Error Message: I can't convert the string into int.
C#7+ supports range-based switching:
switch (Note)
{
case int n when n< 1 || Note > 6:
Console.WriteLine("Your input is wrong!");
break;
case int n when n <= 4:
Console.WriteLine("Passed");
break;
}
You can do it like that if you really need a switch
switch (Note)
{
case 1:
case 2:
case 3:
case 4:
Console.WriteLine("Passed");
break;
case 5:
case 6:
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Your input is wrong!");
break;
}
This is not where switch statements are for. You should keep using if.
I will proof that very easily, using a switch, as you asked:
switch (Note)
{
case 1:
case 2:
case 3:
case 4:
Console.WriteLine("Passed");
break;
case 5:
case 6:
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Your input is wrong!");
break;
}
This is just way too verbatim. You don't want to write down every value. if is more appropriate.
I'm creating program in which the user inputs a number that will display the month associated with that month (1 = january, 2 = feburary, 3 = march,....etc)
however, when inputting the number and pressing enter, the program breaks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication27
{
class Program
{
public static void Main()
{
int month = 0;
//Range 1-12 refers to Month
Console.WriteLine("Please enter Month in numerical increments (1-12)");
month = int.Parse(Console.ReadLine());
switch (month)
{
case 1:
Console.WriteLine("This is the First Month...January");
break;
case 2:
Console.WriteLine("This is the Second Month...Februrary");
break;
case 3:
Console.WriteLine("This is the Third Month...March");
break;
case 4:
Console.WriteLine("This is the Fourth Month...April");
break;
case 5:
Console.WriteLine("This is the Fifth Month...May");
break;
case 6:
Console.WriteLine("This is the Sixth Month...June");
break;
case 7:
Console.WriteLine("This is the Seventh Month...July");
break;
case 8:
Console.WriteLine("This is the Eighth Month...August");
break;
case 9:
Console.WriteLine("This is the Ninth Month...September");
break;
case 10:
Console.WriteLine("This is the Tenth Month...October");
break;
case 11:
Console.WriteLine("This is the Eleventh Month...November");
break;
case 12:
Console.WriteLine("This is the Twelfth Month...December");
break;
default:
Console.WriteLine("You have inputed an invalid Character");
break;
}
//Attempt to looP code
}
}
}
Unsure what is causing the termination
Add a while loop to your code and set a code like "0" to exit the loop or terminate the program.
public static void Main()
{
int month = 0;
bool exit = false;
while (!exit)
{
//Range 1-12 refers to Month
Console.WriteLine("Please enter Month in numerical increments (1-12)");
month = int.Parse(Console.ReadLine());
switch (month)
{
case 0:
exit = true;
break;
case 1:
Console.WriteLine("This is the First Month...January");
break;
case 2:
Console.WriteLine("This is the Second Month...Februrary");
break;
case 3:
Console.WriteLine("This is the Third Month...March");
break;
case 4:
Console.WriteLine("This is the Fourth Month...April");
break;
case 5:
Console.WriteLine("This is the Fifth Month...May");
break;
case 6:
Console.WriteLine("This is the Sixth Month...June");
break;
case 7:
Console.WriteLine("This is the Seventh Month...July");
break;
case 8:
Console.WriteLine("This is the Eighth Month...August");
break;
case 9:
Console.WriteLine("This is the Ninth Month...September");
break;
case 10:
Console.WriteLine("This is the Tenth Month...October");
break;
case 11:
Console.WriteLine("This is the Eleventh Month...November");
break;
case 12:
Console.WriteLine("This is the Twelfth Month...December");
break;
default:
Console.WriteLine("You have inputed an invalid Character");
break;
}
}
//Attempt to looP code
}
The problem states "Write a program that accepts a 10-digit telephone number that may contain one or more alphabetic characters. Display the corresponding number using numerals...etc"
ABC:2 through WXYZ:9
This chapter teaches about loops but I found myself really lost at this problem. I completed the code but I think it sucks...
My question: Is there a better way to shorten this code up? And I only figured to use the c# keyword case, is there another way?
EDIT: Arabic as in you could type in 1800WALLTO and it will give you 1800925586
ALSO I am not asking for a code that doesn't work, this does EXACTLY what I want and asked it to do. I am just asking for any advise or input on how to make it better. I really wanted to know a way to do it without switch and case break etc...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 0;
char userInput = ' ';
string inputString = "",
outputString = "";
Console.WriteLine("Enter the digits from the phone number");
do
{
userInput = Console.ReadKey(false).KeyChar;
inputString += userInput;
if (Char.IsLetter(userInput))
userInput = userInput.ToString().ToUpper().ToCharArray()[0];
switch (userInput)
{
case '1':
outputString += '1';
x++;
break;
case '2':
case 'A':
case 'B':
case 'C':
outputString += '2';
x++;
break;
case '3':
case 'D':
case 'E':
case 'F':
outputString += '3';
x++;
break;
case '4':
case 'G':
case 'H':
case 'I':
outputString += '4';
x++;
break;
case '5':
case 'J':
case 'K':
case 'L':
outputString += '5';
x++;
break;
case '6':
case 'M':
case 'N':
case 'O':
outputString += '6';
x++;
break;
case '7':
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += '7';
x++;
break;
case '8':
case 'T':
case 'U':
case 'V':
outputString += '8';
x++;
break;
case '9':
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += '9';
x++;
break;
case '0':
outputString += '0';
x++;
break;
default:
Console.WriteLine("You entered an incorrect value-Try again");
x--;
break;
}
}
while (x < 10);
Console.WriteLine("\nYou entered {0}", inputString);
Console.WriteLine("Your number is {0}", outputString);
}
}
}
Use a System.Collections.Generic.Dictionary where the dictionary keys are the characters A-Z and 0-9 and the values are the corresponding numbers:
var lookup = Dictionary<char, int> {
{'A',2},
{'B',2},
// etc...
{'Z', 9},
{'1':1},
{'2':2}
// etc... include the numerals so you don't have to converts some things to char not the rest...
};
// to lookup a character:
char item = 'A';
int number = lookup['A'];
To decode an phone number just split it into an array of char's and look them up one after the other
List<int> digits = new List<int>();
foreach (char c in inputString)
{
digits.Add(lookup[c]);
}
Im sure somebody will post a 1-liner using LINQ as well, but this is the vanilla version.
Using a string look up would shorten the code:--
String decode "--------------------------------01234567890------2223334445556667778889999---------------------------------"; //256 char string
numout = decode.substring((int) Char.GetNumericValue(userinput),1);
But it would be a lot less efficient than using a "case" statement. Less code does not mean less cpu.
I am developing a console application, that should listen for digits from a numpad keyboard in both num lock states - on and off. The application is running on Raspberry Pi with Arch Linux and Mono. Since I did not found a way, that is compiling under Mono, to permanently turn numlock on, I am using the following method to convert num pad commands to digits:
private string ReadNumPadSymbol(ConsoleKeyInfo keyInfo)
{
char editedSymbol;
switch (keyInfo.Key)
{
case ConsoleKey.Insert:
editedSymbol = '0';
break;
case ConsoleKey.End:
editedSymbol = '1';
break;
case ConsoleKey.DownArrow:
editedSymbol = '2';
break;
case ConsoleKey.PageDown:
editedSymbol = '3';
break;
case ConsoleKey.LeftArrow:
editedSymbol = '4';
break;
case ConsoleKey.Clear:
editedSymbol = '5';
break;
case ConsoleKey.RightArrow:
editedSymbol = '6';
break;
case ConsoleKey.Home:
editedSymbol = '7';
break;
case ConsoleKey.UpArrow:
editedSymbol = '8';
break;
case ConsoleKey.PageUp:
editedSymbol = '9';
break;
default:
return String.Empty;
}
return editedSymbol.ToString();
}
It works as expected under Windows, but under Linux, the method returns empty string, when the "5" button is pressed. For some reason it does not enters the ConsoleKey.Clear case. Is there a fix for this?
Thanks!