前情提要一下,上次在变数命名的善意那篇中我们把arr换成seats代表一堆位置,n1换成seat代表位置索引,n2换成number代表要放入seats内的值,这次我想带你探索while回圈内想表达什麽,如何才能让别人一眼就看出他想说的事。
int main()
{
int seats[10] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};
int i, j, seat, number;
printf("*seating*\n");
for(int i = 0; i < 10; ++i){
printf("%d ", seats[i]);
}
printf("\n");
printf("***************\n");
printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
scanf("%d %d", &seat, &number);
while(number != 0){
if(seats [seat - 1] == 0){
seats[seat - 1] = number;
printf("*seating*\n");
for(int i = 0; i < 10; ++i){
printf("%d ", seats[i]);
}
printf("\n");
printf("***************\n");
printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
scanf("%d %d", &seat, &number);
}
else{
printf("Sorry, seat is taken.\n");
scanf("%d %d", &seat, &number);
}
}
printf("*seating*\n");
bubble_sort(seats, 10);
for(int i = 0; i < 10; ++i){
printf("%d ", seats[i]);
}
printf("\n");
printf("***************\n");
return 0;
}
当number != 0时做以下的事,这是我第一眼看见程序码得到最直接的资讯,其实在上几行印出来的讯息中有提到
printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
所以我可以得知说继续执行whlie做的事是由使用者输入的数值决定的。
while(number != 0){
if(seats [seat - 1] == 0){
seats[seat - 1] = number;
printf("*seating*\n");
for(int i = 0; i < 10; ++i){
printf("%d ", seats[i]);
}
printf("\n");
printf("***************\n");
printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
scanf("%d %d", &seat, &number);
}
else{
printf("Sorry, seat is taken.\n");
scanf("%d %d", &seat, &number);
}
}
那往下看if(seats [seat - 1] == 0)里面又做了一堆事,但我们可以先看else的部分(因为感觉内容不多),else里面是告诉你位置被拿走了,然後又重新输入一次seat和number,回头再看if内的判断式,可以得知0这个数字代表的意思是位置是空的,重新再诠释一遍的话就是当位置是空的时候做下面的事否则重新再输入一次位置和数值。
if(seats [seat - 1] == 0){
seats[seat - 1] = number;
printf("*seating*\n");
for(int i = 0; i < 10; ++i){
printf("%d ", seats[i]);
}
printf("\n");
printf("***************\n");
printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
scanf("%d %d", &seat, &number);
}
else{
printf("Sorry, seat is taken.\n");
scanf("%d %d", &seat, &number);
}
那if内到底在做什麽呢? seats[seat - 1] = number这里很直觉是指把输入的值塞到seat-1这个空的座位里,-1的原因是阵列起始索引是从0开始的,接下来到for回圈里把所有的值印出来,最後再输入一次新的位置和数值。
seats[seat - 1] = number;
printf("*seating*\n");
for(int i = 0; i < 10; ++i){
printf("%d ", seats[i]);
}
printf("\n");
printf("***************\n");
printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
scanf("%d %d", &seat, &number);
可以请你仔细想一下这三件事情其实是各自独立的,但他们全部连在一起的时候会混淆他们之间的关系,增加阅读的困难,偷偷跟你分享个小技巧,加几个enter会更容易区分他们。
seats[seat - 1] = number;
printf("*seating*\n");
for(int i = 0; i < 10; ++i){
printf("%d ", seats[i]);
}
printf("\n");
printf("***************\n");
printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
scanf("%d %d", &seat, &number);
终於把整个while内在做的事搞懂了,再回头看完整的程序码後,不知道你有没有感觉到有些片段好像重复出现过了像是印出阵列内的所有值和请使用者输入seat和number。
printf("*seating*\n");
for(int i = 0; i < 10; ++i){
printf("%d ", seats[i]);
}
printf("\n");
printf("***************\n");
printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
scanf("%d %d", &seat, &number);
当出现这种情形,代表我们可以新增函式来取代重复的事情,新函式的名称我觉得用showSeats()跟getUserInput()这样最直接明白,当然你也可以有自己的想法,但切记要让别人一看就懂,不要用只有自己看得懂的意思,接下来我们来改改看。
void showSeats(int seats[], int length)
{
printf("*seating*\n");
for(int i = 0; i < length; ++i){
printf("%d ", seats[i]);
}
}
void getUserInput(int& seat, int& number){
printf("\n");
printf("***************\n");
printf("Please input the seat (0~9) and number(-1 to end game)\n");
scanf("%d %d", &seat, &number);
}
int main()
{
int seats[10] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};
int i, j, seat, number;
showSeats(seats, 10);
getUserInput(seat, number);
while(number != 0){
if(seats [seat - 1] == 0){
seats[seat - 1] = number;
showSeats(seats, 10);
getUserInput(seat, number);
}
else{
printf("Sorry, seat is taken.\n");
scanf("%d %d", &seat, &number);
}
}
bubble_sort(seats, 10);
showSeats(seats, 10);
printf("\n");
printf("***************\n");
return 0;
}
挖~~看看我们改完後的结果多麽令人舒畅,while回圈内是不是一看就知道他想做什麽了,虽然还有些地方可以表达得更清楚但已经比一开始看不出个毛来好太多了,而且这样做的好处有很多,除了增加阅读性之外,bug也方便查找,如果之後想要更换显示阵列的方式为从尾巴开始,只需要在showSeats里修改就可以了,最重要的是你帮助别人省去看程序码的时间,因为你已经写在函式上了,相信看你程序码的人会感受到你的心意。
下一篇我会带你把整个main里可以优化的地方重新组织一下,让整个输入数值到阵列的前置作业更清楚,感谢你陪我到这里,相信这段过程会对你有帮助,我们下次见^ ^
Q : wi10 看不到wifi 在cmd 输入 netsh wlan set hostednetw...
第三天记录一样让大家从游戏中探索,最基本好理解的游戏,是帮助上手的好方法,但会相较昨天困难点喔。 ...
看完这篇文章你会得到的成果图 前言 这篇我们要来学一个新的东西 QTimer! QTimer 是独立...
GitHub Repo https://github.com/b2etw/Spring-Kotlin...
千万不要在毫无准备的状态下奔赴战场! 面试已经是充满未知数的战场,如果你到了战场才发现自己把装备忘...