我想通過刪除所有那些if statements
來簡化此代碼,如果可能的話,單個函數和/或可用於刪除這些元素的所有內容(如果有的話)。當檢查多個整數範圍時,替換多個if語句
我需要檢查多個整數範圍,並根據用戶評分我需要設置兩個變量。
這是我的代碼:
if (userScore <= 40)
{
object.setMinValue(0);
object.setMaxValue(40);
}
if (userScore > 40 && userScore <= 65)
{
object.setMinValue(41);
object.setMaxValue(65);
}
if (userScore > 65 && userScore <= 85)
{
object.setMinValue(66);
object.setMaxValue(85);
}
if (userScore > 85 && userScore <= 95)
{
object.setMinValue(86);
object.setMaxValue(95);
}
if (userScore > 95 && userScore <= 100)
{
object.setMinValue(96);
object.setMaxValue(100);
}
我認爲一個switch
聲明可以使用,因爲有很多if statements
來檢查同一個變量,但有一個更簡單,更有效的[性能expecially]解?
感謝
http://stackoverflow.com/questions/2696436/switch-case-in-jquery – sasi 2014-10-29 10:16:49
switch語句不能在這裏使用,因爲你有範圍,而不是固定值。 – 2014-10-29 10:23:24