我想创建一个程序,向用户请求10个等级,然后过滤它们以通过和失败,然后打印通过次数并失败。我做了程序,但输出错误。
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=10;
while (studentcounter!=0)
{
printf("enter the next grade\n");
scanf("%d",grade);
student--;
}
switch (grade)
{
case 1:
if (grade >= 50)
pass++;
break;
case 2:
if (grade <= 49)
fail++;
break;
}
}
printf("the number of fail is %d",fail);
printf("the number of pass is %d",pass);
}
问题是程序要求十个等级,但最后它会打印失败次数和通过次数为零。为什么呢?
答案 0 :(得分:4)
您正在输入成绩编号。一个switch语句测试该案例的等级,我很确定等级不是1%或2%。在这种情况下,if语句将是更合理的选择。
其次,你有一个从未使用过的代码块。首先你将studentcounter设置为零,然后你说“当studentcounter不为零时只执行这个块”......
studentcounter=0;
while (studentcounter!=0) {
printf("enter the next grade\n");
scanf("%d",grade);
student--;
}
第三个问题是,你拼错了成绩。
您可以按如下方式重写代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=0;
while (studentcounter < 10) {
printf("enter the next grade:\n");
scanf("%d",&grade);
if (grade >= 50) {
pass++;
} else {
fail++;
}
studentcounter++;
}
printf("the number of fail is: %d \n",fail);
printf("the number of pass is: %d \n",pass);
return 0;
}
抱歉,如果我忽视了一些事情;我没时间把它扔进我的编辑器:P
欢呼声
答案 1 :(得分:0)
你需要在while循环中保持通过/失败的数量,等级变量将在每次输入时被覆盖。
编辑: 另外,不要使用switch语句。
答案 2 :(得分:0)
好的,呈现的代码是一堆垃圾。我会把它作为练习让你理清我为你修好的东西:
#include <stdio.h>
int main(int argc, char *argv[])
{
int pass = 0;
int fail = 0;
int grade= 0;
int studentcounter;
for (studentcounter=1; studentcounter<=10; studentcounter++ )
{
printf("Enter grade for student #%-2d :", studentcounter);
scanf("%d",&grade);
if(grade >=50)
pass++;
if(grade<=49)
fail++;
}
printf("the number of fail is %d\n",fail);
printf("the number of pass is %d\n",pass);
}
PS。如果这是作业,请在转入时给予肯定。通过查看其他人的代码,您可以学到很多东西,但从不声称要创建您没有的代码。
答案 3 :(得分:0)
C'case'语句不能按照您期望的方式工作。
他们对“常量表达式”进行操作,这意味着为了完成您给出的任务,“确定成绩是通过还是失败”,必须编写一个形式错综复杂的开关语句
switch ( grade ) { case 100: case 99: case 98: case 97: case 96: case 95: case 94: case 93: case 92: case 91: case 90: case 89: case 88: case 87: case 86: case 85: case 84: case 83: case 82: case 81: case 80: case 79: case 78: case 77: case 76: case 75: case 74: case 73: case 72: case 71: case 70: case 69: case 68: case 67: case 66: case 65: case 64: case 63: case 62: case 61: case 60: case 59: case 58: case 57: case 56: case 55: case 54: case 53: case 52: case 51: case 50: pass++; break; default: fail++; break; }
这可能会给你正确的答案,但是,你写作这样的怪物可能会在作业上收到“失败”。
我会想到另一个问题,即'while,switch和if'语句或者你可以修改你当前的程序,以便不是接受10个等级,而是提示用户是否希望进入另一个等级,只接受答案'Y','y'或'N','n'因此'切换'该答案继续。
答案 4 :(得分:0)
#include <stdio.h> int main(void) { int pass, fail, grade, answer, studentcounter; pass = 0; fail = 0; grade = 0; studentcounter = 10; while ( studentcounter-- ) { printf("enter the next grade\n"); scanf("%d", &grade); answer = ( grade > 49 ); switch ( answer ) { case 1: pass++; break; default: fail++; break; } } printf("the number of fail is %d \n", fail); printf("the number of pass is %d \n", pass); }
答案 5 :(得分:0)
char pass_fail = (grade >= 50)? y : n;
switch( pass_fail )
{
case 'y':
// print pass
break;
case 'n':
// print fail
break;
}
希望这可以帮助你
答案 6 :(得分:0)
2个错误:
1:while循环应该在switch语句之后结束 2:你的变量名是&#34; studentcounter&#34;但是你正在减少学生&#34; 请检查以下代码。这应该有用。
void main()
{
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=10;
while (studentcounter!=0)
{
printf("enter the next grade\n");
scanf("%d",grade);
studentcounter--;
switch (grade)
{
case 1:
if (grade >= 50)
pass++;
break;
case 2:
if (grade <= 49)
fail++;
break;
}
}
printf("the number of fail is %d",fail);
printf("the number of pass is %d",pass);
}