这个switch 有什么问题么。
程序代码:
#include<stdio.h> void add(int a1, int a2){ int resa = a1 + a2; printf("%d\n", resa); printf("done!\n"); } void sub(int s1, int s2){ int ress = s1 - s2; printf("%d\n", ress); printf("done!\n"); } void mul(int m1, int m2){ int resm = m1*m2; printf("%d\n", resm); printf("done!\n"); } void div(int d1, int d2){ int resd = d1 / d2; printf("%d\n", resd); printf("done!\n"); } int main(){ printf("Enter the operation of your choice!\n"); printf("a.add s.substract m.multiply d.diveide q.quit\n"); char choice; while ((choice = getchar()) != 'q'){ switch (choice){ case'a': int a1, a2; printf("please enter a1:\n"); scanf_s("%d", &a1); printf("please enter a2:\n"); scanf_s("%d", &a2); add(a1,a2); break; case 's': int s1, s2; printf("please enter s1:\n"); scanf_s("%d", &s1); printf("please enter s2:\n"); scanf_s("%d", &s2); sub(s1, s2); break; case 'm': int m1, m2; printf("please enter m1:\n"); scanf_s("%d", &m1); printf("please enter m2:\n"); scanf_s("%d", &m2); mul(m1, m2); break; case 'd': int d1, d2; printf("please enter d1:\n"); scanf_s("%d", &d1); printf("please enter d2:\n"); gtd1: scanf_s("%d", &d2); if (d2 != 0) div(d1, d2); else { printf("Plesae enter your d2 again!\n"); goto gtd1; } break; default: printf("please input effective keyword\n"); break; } } }
这个程序就是选择加减乘除,如果选了一个没有的选项,比如输入b。会出来两次please input effective keyword,是为什么呢。