我在if和switch语句中遇到C#代码问题 [英] I have a C# code problem with an if and switch statement

查看:82
本文介绍了我在if和switch语句中遇到C#代码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要编写一个程序,该程序将告诉用户每个月的天数.该程序应允许用户输入代表月份的整数(Jan = 1,Feb = 2,依此类推).确认这是有效的月份数(即1到12之间)后,程序应两次确定该月的天数:一次使用一系列if语句,一次使用switch语句.
将得到的天数放在两个不同的输出标签中.
注意:假设2月始终有28天.



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.



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;
                   }


这就是我得到的,但是我需要在stch标签中使用相同的信息,如果label


this is what i got but i need the same info in stch label and if label

推荐答案

您正确地开始了

You started right

if (monthNo <= 0 || monthNo > 12)
{
  MessageBox.Show("Invalid Input!!", "Must be 1 to 12!!");
} else if (monthNo == 1 || monthNo == 3 ....



看看是否可以从那里去.如果这是我的学校作业,我真的不想帮您太大忙,那么您可以学到的最好的方法就是自己思考和尝试.



See if you can go from there. If this is a school assignment I really don''t want to help you to much, the best thing you can learn is to think and try for yourself.


此代码无法修复,在我看来,由于它基于硬编码数据.此样式不受支持.好的代码永远不会包含长的switchif语句,例如数据驱动的内容.

您可以将月份集设为枚举类型并对其进行迭代,创建按枚举索引的数据数组.有关朝着这个方向的综合方法,请参阅我的文章:枚举类型不枚举!解决.NET和语言限制 [ ^ ].

但是,有不同的方法.

—SA
This code is beyond repair, in my view, due to it is based on hard-coded data. This style is not supportable. Good code never contains long switch or if statements such things are data-driven.

You could make the set of months the enumeration type and iterate through it, create array of data indexed by enumeration. For comprehensive approaches in this direction, please see my article: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

There are different approaches, though.

—SA


这里没有人要做家庭作业...
No one here is going to do your homework...


这篇关于我在if和switch语句中遇到C#代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆