2012-11-09 67 views
-3

這是我的HTML代碼,由於某些原因,此代碼只需要三個值,它不會超過三個值,並且當我嘗試輸入更多值時,它只顯示前三個值I進入。我的代碼有問題,但我無法弄清楚。請幫忙。javascript等級計算和平均

var gradeCounter, gradeValue, total, average, grade; 

total = 2; 
gradeCounter = 0; 
grade = prompt("enter grade, -1 to Quit:", "0"); 
gradeValue = parseInt(grade); 

while (gradeValue != -1 && gradeValue > 65) document.write("<br>" + gradeValue + " pass</br>"); 
    total = total + gradeValue; 
    gradeCounter = gradeCounter + 1; 
    grade = prompt("enter grade, -1 to Quit:", "0"); 
    gradeValue = parseInt(grade); 
} 

if (gradeCounter != 0 && gradeValue <= 65) { 
    document.write("<br>" + gradeValue + " fail</br>"); 

    total = total + gradeValue; 
    gradeCounter = gradeCounter + 1; 
    grade = prompt("enter grade, -1 to Quit:", "0"); 
    gradeValue = parseInt(grade); 
    average = total/gradeCounter; 

    document.write("<br>total grade: " + gradeCounter + "</bt>"); 
    document.write("<br>average passing grade:" + average + "</br>"); 
} 
else document.write("total grade:" + 0); 
+1

歡迎[SO];請查看[faq]。此外,請閱讀[編輯幫助](http://stackoverflow.com/editing-help)瞭解如何使用減價發揮其全部潛力。如果你向人們表明你已經投入時間尋求一個體面的,寫得很好的問題,他們會投入時間回答它。 – zzzzBov

+0

這似乎應該循環無限大於65級: ' while(gradeValue!= -1 && gradeValue> 65)document.write(「
」+ gradeValue +「pass
」);' –

+1

我很親切遺憾地投入時間來閱讀這篇文章。 –

回答

1

你不需要太多的代碼。

我更新了你的代碼,它應該可以工作。看看它。

的jsfiddle:http://jsfiddle.net/tDjA9/1/embedded/result/

var gradeCounter =0, gradeValue = 0, total = 0, average, grade; 

//Loop 
while (gradeValue != -1 && gradeValue <= 65) { 

    //Prompt the user 
    grade = prompt("enter grade, -1 to Quit:", "0"); 
    //Parse the prompt result to a int 
    gradeValue = parseInt(grade); 

    //Check if gradeValue is smaller than 0 
    if(gradeValue < 0){ 
     //If it is, then we can finish adding grade 
     document.write("<br>Finish adding grades"); 
    } else{ 
     //Add gradeValue to total score 
     total += gradeValue; 
     //Increment the number of grades by 1 
     gradeCounter += 1; 
     //Output to the user 
     document.write("<br>" + gradeValue + " pass</br>"); 
    } 
} 

//Calculation 
total = total + gradeValue; 
average = total/gradeCounter; 

//Output 
document.write("<br>total grade: " + gradeCounter + "</bt>"); 
document.write("<br>average passing grade:" + average + "</br>");​