黑马程序员技术交流社区

标题: 自己写的输入一个字母来判断是星期几的程序 [打印本页]

作者: 熊丽    时间: 2013-8-10 17:09
标题: 自己写的输入一个字母来判断是星期几的程序
输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母,第三个字母,直到可以判断出为止
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace exam3
  6. {
  7.     class Program
  8.     {
  9.         //请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母
  10.         static void Main(string[] args)
  11.         {
  12.             string[] week = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  13.             int index = 0;//索引字母
  14.             int  IsFirst=0 ; //输入字母在星期中出现的次数
  15.             List<string>tempStr=new List<string>();//储存星期
  16.             while (true)
  17.             {
  18.                 Console.Write("请输入:");
  19.                 string str = Console.ReadLine().Trim();//去掉空格
  20.                
  21.                 for (int i = 0; i < week.Length&&index==0; i++)
  22.                 {
  23.                     //string  a=week[i].Substring(index).ToUpper();
  24.                     if (str.ToUpper()==week[i].Substring(index,1).ToUpper().ToString())//输入的字符是否与某星期的字母匹配
  25.                     {
  26.                         IsFirst++;
  27.                         tempStr.Add(week[i]);//将相同位置上相同的字母的星期存放在tenpStr中
  28.                      
  29.                     }

  30.                 }
  31.                
  32.                 for(int i=0;i<tempStr.Count&&index>=1;i++)//从tempStr中比较
  33.                 {
  34.                     if (str.ToUpper().Equals(tempStr[i].Substring(index, 1).ToUpper()))
  35.                     {
  36.                         IsFirst++;
  37.                         tempStr[0] = tempStr[i];
  38.                     }
  39.                     else
  40.                         tempStr.RemoveAt(i);
  41.                        
  42.                 }
  43.                 if (IsFirst == 1)//此字母只出现了一次
  44.                     break;
  45.                 index++;
  46.                 IsFirst = 0;//重新输入一个字母,将IsFirst置0
  47.                
  48.             }
  49.             Console.WriteLine("Today is :"+tempStr[0]);
  50.             Console.ReadKey();

  51.         }
  52.     }
  53. }
复制代码

作者: 许庭洲    时间: 2013-8-10 19:19
值得学习ing!
作者: 彭家贰小姐    时间: 2013-8-10 22:57
{:soso_e179:}
作者: 熊丽    时间: 2013-8-11 21:34
{:soso_e112:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2