用字符判断星期

#include <stdio.h>
int main()
{
	char ch;
	char ch1;
	
	ch = getchar();
	getchar();	//接收回车 
	
	switch(ch){
		case 'm':
			printf("monday\n");
			break;
		case 'w':
			printf("wednesday\n");
			break;
		case 'f':
			printf("friday\n");
			break;
		case 't':
			printf("please input another letter\n"); 
			ch1 = getchar();
			if(ch1=='u'){
				printf("tuesday\n"); 
			}
			if(ch1=='h'){
				printf("thursday\n");
			}
			break;
		case 's':
			printf("please input another letter\n");
			ch1 = getchar();
			if(ch1=='a'){
				printf("saturday\n"); 
			}
			if(ch1=='u'){
				printf("sunday\n");
			}
			break;	
		default:
			break;		
	}
	return 0;
 } 

学习到:

  1. 该题思想
  • 第一个字符用开关选择,若出现重复现象,则继续判断第二个字符,一次类推
  1. 当需要多次键入字符时,要考虑把回车字符吸收掉
    1getchar()
  2. switch使用
switch(){
  case 1: 语句1; break;
  case 2: 
         语句2;
         语句3;
         etc;
         braek;
         //可以不用加{}
   case 3:

   default: 语句1; break;
}
4. 写代码小技巧
不要想着一蹴而就,要慢慢来,最好边写边调试
5. 有时候必要的用户提示是必要的!
eg:printf("please input another letter\n");
6. if()与if()else()的区别
* if不论有多个都要执行一遍,它的逻辑:符合就是没有其他余地
* if()else()只会执行符合的其中一个,它的逻辑:不是它就是另一个
ps:该题显然只有是相应字符才是,没有其他余地,显示使用if

7. 遇到bug不要慌,也不要总是百度
1首先要经过自己的思考,看看能够自己找到原因,这样成就感比百度出来要好多了!
2去猜,去调试,去尝试,去查资料,
8. 
posted @ 2022-11-06 11:52  行至为成  阅读(48)  评论(0)    收藏  举报