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
}
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 am building a form that takes an input number from a text box and then will take the number that was input, and display the roman numeral equivalent in another text box.
My Form:
private void convertButton_Click(object sender, EventArgs e)
{
int numberInput;
switch (numberInput)
This is where I keep getting an error code. The "switch (numberInput)" is seen as and unassigned local variable. How do I assign it so that it will be able to access all of the case integers?
{
case 1:
outputTextBox.Text = "I";
break;
case 2:
outputTextBox.Text = "II";
break;
case 3:
outputTextBox.Text = "III";
break;
case 4:
outputTextBox.Text = "IV";
break;
case 5:
outputTextBox.Text = "V";
break;
case 6:
outputTextBox.Text = "VI";
break;
case 7:
outputTextBox.Text = "VII";
break;
case 8:
outputTextBox.Text = "VIII";
break;
case 9:
outputTextBox.Text = "IX";
break;
case 10:
outputTextBox.Text = "X";
break;
default:
MessageBox.Show("Please enter and number between 1 and 10. Thank you!");
break;
}
Cause your variable is not assigned yet int numberInput; and so the error. You said input is coming from a TextBox, in that case do like below assuming textbox1 is your TextBox control instance name
int numberInput = Convert.ToInt32(this.textbox1.Text.Trim());
Convert.ToInt32 may throw an exception if the parsing is unsuccessful. Another method is to Int.Parse:
int numberInput = int.Parse(textbox1.Text.Trim());
or better yet
int numberInput;
if(int.TryParse(textbox1.Text.Trim(), out numberInput))
{
switch (numberInput)
...
}
I'm creating n 8th ball and I'm trying to randomly generate one of the eight phrases into a textbox once a button is tapped and can't get my head around how to get my phrases in a textbox when a button is tapped.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
Console.;
break;
case 1:
Console.WriteLine.TextBox("Yes");
break;
case 2:
Console.WriteLine.TextBox("No");
break;
case 3:
Console.WriteLine.TextBox("Maybe");
break;
case 4:
Console.WriteLine.TextBox("You could say that");
break;
case 5:
Console.WriteLine.TextBox("Most certain");
break;
case 6:
Console.WriteLine.TextBox("Dont even try");
break;
case 7:
Console.WriteLine.TextBox("Full steam ahead");
break;
}
}
Console.WriteLine() normally writes to the Console output in a Console App. So it seems you are mixing something up...
Your textbox in the wpf app needs to have a variable name e.g. theTextBox and then you assign the string to the .Text property.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
theTextBox.Text = "";
break;
case 1:
theTextBox.Text = "Yes";
break;
case 2:
theTextBox.Text = "No";
break;
case 3:
theTextBox.Text = "Maybe";
break;
case 4:
theTextBox.Text = "You could say that";
break;
case 5:
theTextBox.Text = "Most certain";
break;
case 6:
theTextBox.Text = "Dont even try";
break;
case 7:
theTextBox.Text = "Full steam ahead";
break;
}
}
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;
}