我創建一個程序來評估輸入比如我輸入的5號函數應該能返回,這意味着數:返回一個數的雙重價值
5 => 500
4 => 400
6 => 600
10 => 1000 and so on.
這是我到目前爲止已完成。
public int NumberEval(int theNumber)
{
int theResult = 0;
switch (theNumber)
{
case 0:
theResult = 1;
break;
case 1:
theResult = 1;
break;
case 2:
theResult = 200;
break;
case 3:
theResult = 300;
break;
case 4:
theResult = 400;
break;
case 5:
theResult = 500;
break;
case 6:
theResult = 600;
break;
case 7:
theResult = 700;
break;
case 8:
theResult = 800;
break;
case 9:
theResult = 900;
break;
case 10:
theResult = 1000;
break;
case 11:
theResult = 1100;
break;
case 12:
theResult = 1200;
break;
case 13:
theResult = 1300;
break;
case 14:
theResult = 1400;
break;
case 15:
theResult = 1500;
break;
case 16:
theResult = 1600;
break;
case 17:
theResult = 1700;
break;
case 18:
theResult = 1800;
break;
case 19:
theResult = 1900;
break;
case 20:
theResult = 2000;
break;
default:
break;
}
return theResult;
}
我的問題是,如果我輸入50,那麼它應該是5000。有沒有辦法來縮短這一方法,因爲我不希望每個項目在switch語句手動編碼?或者甚至是這個數學方程?
乘以100? – Steve
...除非'theNumber == 1'或'== 0',在這種情況下只返回1(除非這是你的問題中的錯字?)。爲了將來的參考,你應該問自己的第一個問題是「我怎麼知道我想要返回?」也就是說,是什麼讓你認爲如果'Number == 9'你應該返回900?如果你這樣做了,並提出「因爲9 * 100 = 900」,這將給你一個暗示,如何解決這個問題,而不用硬編碼每個答案。 – yshavit
你必須使用開關嗎? –