Topic: case 语句 求救

  Print this page

1.case 语句 求救 Copy to clipboard
Posted by: wuyoubei1
Posted on: 2005-09-08 13:16

1.if(score >=90 && score <=100)
finalScore="甲";
else if (score >=80 && score <=90)
finalScore="乙 ";
else if (score >=70 && score <=80)
finalScore="丙 ";
else if (score >=60 && score <=70)
finalScore="丁 ";
else
finalScore="戊 ";
用更简单的方法写出来(不能用逻辑表达式)

2.Re:case 语句 求救 [Re: wuyoubei1] Copy to clipboard
Posted by: why
Posted on: 2005-09-08 13:39

I don't think it is a good idea to use select-case.
Moreover, think about when score is greater than 100 or is exactly 90, 80, or 70...

Anyway,
简单的方法 :
if (score >=100)
...
else if (score >=90)
finalScore = "甲";
else if (score >=80)
...
else if (score >=70)
...
else if (score >=60)
...
else
...

简单(?)的方法 :
finalScore = (score >=100) ? "xx" : ( (score >=100) ? "甲" : ( (score >=90) ? ......

3.Re:case 语句 求救 [Re: wuyoubei1] Copy to clipboard
Posted by: wuyoubei1
Posted on: 2005-09-08 16:35

我只是个新手,我想知道case 语句的一些知识,麻烦你帮忙啊。把这道题用case语句的形式表示出来,好吗?谢谢!

4.Re:case 语句 求救 [Re: wuyoubei1] Copy to clipboard
Posted by: why
Posted on: 2005-09-08 21:19

You should get a introductory book to read...

switch (score) {
case 100:
case 99:
... // fill in case 98 to case 91 here
case 90:
finalScore="甲 ";
break;
case 89:
... // fill in case 88 to case 81 here
case 80:
finalScore="乙 ";
break;
case 79:
... // fill in case 78 to case 71 here
case 70:
finalScore="丙 ";
break;
case 69:
... // fill in case 68 to case 61 here
case 60:
finalScore="丁 ";
break;
default:
finalScore="戊 ";
}

5.Re:case 语句 求救 [Re: wuyoubei1] Copy to clipboard
Posted by: ge_zwj83
Posted on: 2005-09-09 11:24

case看起来很麻烦

6.Re:case 语句 求救 [Re: ge_zwj83] Copy to clipboard
Posted by: why
Posted on: 2005-09-09 11:35

ge_zwj83 wrote:
case看起来很麻烦

Only because it is not appropiate to use switch-case here, sometimes it could be quite elegant.
Anyway, I always try not to use such syntactic sugar.

7.Re:case 语句 求救 [Re: wuyoubei1] Copy to clipboard
Posted by: wuyoubei1
Posted on: 2005-09-09 12:59

谢谢各位

8.Re:case 语句 求救 [Re: wuyoubei1] Copy to clipboard
Posted by: nik
Posted on: 2005-09-12 17:17


int tmp = score/10;
if(tmp > 10)
tmp = 10;
switch(tmp)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: // code of score < 60 && score >= 0
break;
case 6: // code of score <70 && score >=60
break;
case 7: // code of score < 80 && score >= 70
break;
case 8: // code of score < 90 && score >= 80
break;
case 9: // code of score < 100 && score >= 90
break;
case 10: // code of score >= 100
break;
default: // code of exception case
}

9.Re:case 语句 求救 [Re: wuyoubei1] Copy to clipboard
Posted by: nik
Posted on: 2005-09-12 17:18

靠,怎么空格输不出来??

10.Re:case 语句 求救 [Re: nik] Copy to clipboard
Posted by: why
Posted on: 2005-09-12 19:49

nik wrote:
靠,怎么空格输不出来??

use our [ code ] tag to format code.
HTML would "eat up" white spaces.Smile


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923