一旦用户输入了正确的字母,如何阻止循环重复? #包括 #包括 #包括 int main() { 字符代码,名称[50]; 国际收支平衡,平衡,平衡,网球=0,游泳=0,羽毛球=0; printf(“T代表网球”); printf(“羽毛球B”); printf(“用于游泳的S\n”); printf(“请输入您的姓名:”); scanf(“%s”,名称); 做{ printf(“\n请输入您为费用支付的金额”\n”); scanf(“%d”,已支付余额); printf(“请选择一个代码:\n”); scanf(“%c”、&code); scanf(“%c”、&code); 如果(代码=='T'| |代码=='T') { 余额=18000; 余额=余额-已支付余额; 网球=网球+1; printf(“您好%s您的运动项目是网球。您的余额为$%d”,姓名,bal_rem); } else if(代码=='B'| |代码=='B') { 余额=14000; 余额=余额-已支付余额; 羽毛球=羽毛球+1; printf(“您好%s您的运动项目是羽毛球。您的余额为$%d”,姓名,bal_rem); } else if(code='S'| code='S') { 余额=16000; 余额=余额-已支付余额; 游泳=游泳+1; printf(“您好%sc您的运动项目是游泳。您剩余的余额是$%d”,姓名,bal_rem); } 否则如果(代码!=“T”|代码!=“T”|代码!=“B”|代码!=“B”|代码!=“S”|代码!=“S”) { printf(“无效代码”); } }而(代码!=“T”|代码!=“T”|代码!=“B”|代码!=“B”|代码!=“S”|代码!=“S”); printf(“打网球的人数是%d\n”,网球); printf(“打网球的人数是%d\n”,羽毛球); printf(“打网球的人数是%d\n”,游泳); 返回0; }
更改此测试:一旦用户输入了正确的字母,如何阻止循环重复? #包括 #包括 #包括 int main() { 字符代码,名称[50]; 国际收支平衡,平衡,平衡,网球=0,游泳=0,羽毛球=0; printf(“T代表网球”); printf(“羽毛球B”); printf(“用于游泳的S\n”); printf(“请输入您的姓名:”); scanf(“%s”,名称); 做{ printf(“\n请输入您为费用支付的金额”\n”); scanf(“%d”,已支付余额); printf(“请选择一个代码:\n”); scanf(“%c”、&code); scanf(“%c”、&code); 如果(代码=='T'| |代码=='T') { 余额=18000; 余额=余额-已支付余额; 网球=网球+1; printf(“您好%s您的运动项目是网球。您的余额为$%d”,姓名,bal_rem); } else if(代码=='B'| |代码=='B') { 余额=14000; 余额=余额-已支付余额; 羽毛球=羽毛球+1; printf(“您好%s您的运动项目是羽毛球。您的余额为$%d”,姓名,bal_rem); } else if(code='S'| code='S') { 余额=16000; 余额=余额-已支付余额; 游泳=游泳+1; printf(“您好%sc您的运动项目是游泳。您剩余的余额是$%d”,姓名,bal_rem); } 否则如果(代码!=“T”|代码!=“T”|代码!=“B”|代码!=“B”|代码!=“S”|代码!=“S”) { printf(“无效代码”); } }而(代码!=“T”|代码!=“T”|代码!=“B”|代码!=“B”|代码!=“S”|代码!=“S”); printf(“打网球的人数是%d\n”,网球); printf(“打网球的人数是%d\n”,羽毛球); printf(“打网球的人数是%d\n”,游泳); 返回0; },c,C,更改此测试: #include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { char code, name[50]; int bal_paid, balance, bal_rem, tennis = 0, swimming = 0, Badminton = 0; printf("T for Tennis\n"); printf("
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
char code, name[50];
int bal_paid, balance, bal_rem, tennis = 0, swimming = 0, Badminton = 0;
printf("T for Tennis\n");
printf(" B for Badminton\n");
printf(" S for Swimming\n");
printf("please enter your name:");
scanf("%s", name);
do {
printf("\nPlease enter the amount you paid for the fees\n");
scanf("%d", &bal_paid);
printf("Please select a code : \n");
scanf("%c", &code);
scanf("%c", &code);
if (code == 'T' || code == 't')
{
balance = 18000;
bal_rem = balance - bal_paid;
tennis = tennis + 1;
printf("Hello %s your sport event is tennis. Your balance remaining is $%d", name, bal_rem);
}
else if (code == 'B' || code == 'b')
{
balance = 14000;
bal_rem = balance - bal_paid;
Badminton = Badminton + 1;
printf("Hello %s your sport event is Badminton. Your balance remaining is $%d", name, bal_rem);
}
else if (code == 'S' || code == 's')
{
balance = 16000;
bal_rem = balance - bal_paid;
swimming = swimming + 1;
printf("Hello %sc your sport event is swimming. Your balance remaining is $%d", name, bal_rem);
}
else if (code != 'T' || code != 't' || code != 'B' || code != 'b' || code != 'S' || code != 's')
{
printf("invalid code");
}
} while (code != 'T' || code != 't' || code != 'B' || code != 'b' || code != 'S' || code != 's');
printf("the amount of people doing tennis is %d\n", tennis);
printf("the amount of people doing tennis is %d\n", Badminton);
printf("the amount of people doing tennis is %d\n", swimming);
return 0;
}
为此:
while (code != 'T' || code != 't' || code != 'B' || code != 'b' ...)
无论
code
的值如何,前者都将始终生成true。编辑器包括编辑和格式化工具。正确格式化的源代码是{…}
符号。(也就是说,它不会正确格式化您的代码,但会使正确缩进和格式化的代码看起来像正确缩进和格式化的代码。)您的代码测试!='T'| |代码!='t’…
是错误的。例如,代码不是T
,也不是T
,因此这将始终是真的,因为无限循环。它不能同时是T
和T
。将逻辑或更改为逻辑和。我已更新帖子,使代码至少标记为代码。但由于不一致的缩进和过度使用不必要的空行,它仍然难以阅读。请进一步编辑以使代码可读。@kaylum通过联机C格式化程序添加了一些美化。
while (code != 'T' && code != 't' && code != 'B' && code != 'b' ...)