Click here to Skip to main content
15,892,643 members

I have a C# code problem with an if and switch statement

jdsad2008 asked:

Open original thread
I am going to write a program that will tell the user the number of days in a individual month. The program should allow the user to enter a whole number representing the month (Jan = 1, Feb = 2, etc.). After checking that this is a valid month number (i.e., between 1 and 12), program should then determine the number of days in that month two different times: once using a series of if statements and once using a switch statement.
Place the resulting number of days in two different output Labels.
NOTE: Assume that February always has 28 days.

[EDIT]

C#
private void button1_Click(object sender, EventArgs e)
       {
           int monthNo = 0;
           string[] month = new string[13];
           month[0] = "\0";
           month[1] = "January"; month[2] = "February"; month[3] = "March";
           month[4] = "April"; month[5] = "May"; month[6] = "June";
           month[7] = "July"; month[8] = "August"; month[9] = "September";
           month[10] = "October"; month[11] = "November"; month[12] = "December";

           {


               monthNo = int.Parse(monthNoText.Text);
               if (monthNo <= 0 || monthNo > 12)
               {
                   MessageBox.Show("Invalid Input!!",
                       "Must be 1 to 12!!");

               }

               {
                   switch (monthNo)
                   {
                       case 1:
                       case 3:
                       case 5:
                       case 7:
                       case 8:
                       case 10:
                       case 12:
                           switchText.Text = "31 days";
                           break;
                       case 4:
                       case 6:
                       case 9:
                       case 11:
                           switchText.Text = "30 days";
                           break;
                       case 2:
                           switchText.Text = "28 days";
                           break;
                   }

this is what i got but i need the same info in stch label and if label
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900