2014-11-25 39 views
1

我是初學者,我需要一些幫助來完成我的任務。我無法弄清楚我做錯了什麼。前哨程序不起作用

我必須製作一個程序來讀取一系列值爲0到100和-1的考試分數以停止處理。輸入並驗證分數。程序應計算並打印通過次數(> = 50)和失敗次數(0-50)。當輸入-1分時,顯示通過次數和失敗次數。

<script> 
var score = 0; 
var passCount = 0; 
var failCount = 0; 

score = parseInt(prompt("Input score between 1-100, -1 to quit","0")); 

while (score !< 0){ 
    if (score >= 50 || score <= 100){ 
    passCount = passCount + 1; 
    alert ("You passed! Pass count = "+passCount+"Fail count = "+failCount); 
    } 
    else 
    if (score<50){ 
    failCount = failCount + 1; 
    alert ("You failed! Pass count = "+passCount+"Fail count = "+failCount); 
    } 
    else 
    if (score > 100){ 
    alert ("Invalid number"); 
    } 
    score = parseInt(prompt("Input score between 1-100, -1 to quit","0")); 
} 
document.write ("Total: Passes - "+passCount+"Fails "+failCount); 
</script> 

回答

1

變更,必須做到:

  1. while (score >= 0)

    沒有運營商!<。你要麼必須使用while (! (score<0))while (score >=0)while (score != -1)

  2. 如果(得分> = 50 &&得分< = 100)

    既然你是在原來代碼中使用||OR操作,控制永不熄滅故障計數分支。需要使用AND運算符才能使條件生效。

最終代碼:

<script> 
var score = 0; 
var passCount = 0; 
var failCount = 0; 

score = parseInt(prompt("Input score between 1-100, -1 to quit","0")); 

while (score >= 0){ 
    if (score >= 50 && score <= 100){ 
    passCount = passCount + 1; 
    alert ("You passed! Pass count = "+passCount+"Fail count = "+failCount); 
    } 
    else 
    if (score<50){ 
    failCount = failCount + 1; 
    alert ("You failed! Pass count = "+passCount+"Fail count = "+failCount); 
    } 
    else 
    if (score > 100){ 
    alert ("Invalid number"); 
    } 
    score = parseInt(prompt("Input score between 1-100, -1 to quit","0")); 
} 
document.write ("Total: Passes - "+passCount+"Fails "+failCount); 
</script> 
+0

謝謝!這解決了它。 :-) – 2014-11-25 07:47:31

0

嘗試改變你的病情while(score != -1)

+0

謝謝! :-) :-) – 2014-11-25 07:48:04