Switch语句-评分系统

huangapple go评论58阅读模式
英文:

Switch statements- grading system

问题

#include <stdio.h>;

void main() {
  int a, b, c, d, e, f, avg;

  printf("Enter marks of six subjects: ");
  scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
  avg = (a + b + c + d + e + f) / 6;
  printf("Average is %d", avg);

  switch (avg) {
    case 10:
      printf("Your grade is 10.");
      break;
    case 9:
      printf("Your grade is 9.");
      break;
    case 8:
      printf("Your grade is 8.");
      break;
    case 7:
      printf("Your grade is 7.");
      break;
    case 6:
      printf("Your grade is 6.");
      break;
    case 5:
      printf("Your grade is 5.");
      break;  // You missed the 'break' statement here.
    default:
      printf("You are fail.");
  }
}
英文:
#include &lt;stdio.h&gt;

void main() {
  int a, b, c, d, e, f, avg;

  printf(&quot;Enter marks of six subjects: &quot;);
  scanf(&quot;%d %d %d %d %d %d %d&quot;, &amp;a, &amp;b, &amp;c, &amp;d, &amp;e, &amp;f);
  avg = (a + b + c + d + e + f) / 6;
  printf(&quot;Average is %d&quot;, avg);

  switch (avg) {
    case 10:
      printf(&quot;Your grade is 10.&quot;);
      break;
    case 9:
      printf(&quot;Your grade is 9.&quot;);
      break;
    case 8:
      printf(&quot;Your grade is 8.&quot;);
      break;
    case 7:
      printf(&quot;Your grade is 7.&quot;);
      break;
    case 6:
      printf(&quot;Your grade is 6.&quot;);
      break;
    case 5:
      printf(&quot;Your grade is 5.&quot;);
    default:
      printf(&quot;You are fail.&quot;);
  }
}

After I enter the marks of six subjects, the code is not running, it's neither showing any error message as well.

答案1

得分: 1

以下是您要翻译的内容:

Problems include

Not enabling all compiler warnings
未启用所有编译器警告

Mis-match
不匹配

&quot;%d %d %d %d %d %d %d&quot;, &amp;a, &amp;b, &amp;c, &amp;d, &amp;e, &amp;f: 7 specifiers and 6 following arguments.
&quot;%d %d %d %d %d %d %d&quot;, &amp;a, &amp;b, &amp;c, &amp;d, &amp;e, &amp;f:7个格式说明符和6个后续参数。

Not checking return value of scanf()
未检查scanf()的返回值
// scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
if (scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f) != 6) {
; // TBD: Handle error, maybe print some error message and exit
}

No break
没有break语句
case 5:
printf("Your grade is 5.");
break; // add
default:
printf("You are fail.");

英文:

Problems include

Not enabling all compiler warnings

Mis-match

&quot;%d %d %d %d %d %d %d&quot;, &amp;a, &amp;b, &amp;c, &amp;d, &amp;e, &amp;f: 7 specifiers and 6 following arguments.

Not checking return value of scanf()

// scanf(&quot;%d %d %d %d %d %d %d&quot;, &amp;a, &amp;b, &amp;c, &amp;d, &amp;e, &amp;f);
if (scanf(&quot;%d %d %d %d %d %d&quot;, &amp;a, &amp;b, &amp;c, &amp;d, &amp;e, &amp;f) != 6) {
  ; // TBD: Handle error, maybe print some error message and exit
}

No break

case 5:
  printf(&quot;Your grade is 5.&quot;);
  break; // add 
default:
  printf(&quot;You are fail.&quot;);

huangapple
  • 本文由 发表于 2023年2月6日 03:13:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定