我仍然无法让我的编码运行。当我尝试运行编码时,会出现一个空白页面。具体来说,为了使这个简单的编码能够运行,我需要做什么?我需要的只是有人看一遍或测试它并告诉我需要什么来让我的编码工作。
<html>
<body>
<script language="JavaScript">
<!--
var classCtr;
var nmAnswer;
var clsGrade;
var totalvalue;
var gpatotalvalue;
gpatotalvalue = 0;
totalvalue = 0;
// set up one dimensional array
var myClass = new Array();
classnm = 0;
do
{
// start columns in second dimension of the array
myClass[classCtr] = new Array();
// get values from user and put in array
myClass[classCtr][0] = prompt ("Enter Class Name");
myClass[classCtr][1] = prompt ("Enter grade recieved");
myClass[classCtr][2] = {"A":4,"B":3,"C":2,"D":1,"F":0}[class[classCtr][1]];
myClass[classCtr][3] = prompt ("Enter credit hours")
// accumulate the total value
totalvalue = totalvalue + parseFloat(myClass[classCtr][3]);
// add one to the total number
classCtr++;
gpatotalvalue = gpatotalvalue + parseFloat(myClass[classCtr][1]);
nmAnswer = prompt ("Do you have more classes");
} while (nmAnswer == "yes");
// set variable that is used as counter
clsGrade = 0;
// print out header for content
document.write("<H2 align='center'>Grade Point Average</H2><br>");
document.write("<table bgcolor='Grey' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<tr>");
document.write("<td>Class Name</td><td align='center'>Class Grade</td><td align='center'>Grade Credit</td>");
document.write("</tr>");
// Loop through array displaying html and javascript values in the array
while (clsGrade == 0 && classnm == 0)
{
document.write("<tr>");
document.write("<td>");
document.write (class[clsGrade][0]);
document.write("</td>");
document.write("<td align='center'>");
document.write (class[clsGrade][1]);
document.write("</td>");
document.write("<td align='center'>");
document.write (class[clsGrade][3]);
document.write("</td>");
document.write("</tr>");
document.write("</td>");
// increment the counter
clsGrade++;
}
// finish the table of data and display the total value
document.write("</Table>");
document.write("<br>");
document.write("<table bgcolor='grey' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<tr>");
document.write("<td>Total value</td><td align='center'>" + totalvalue + "</td>");
document.write("</tr>");
document.write("<tr>");
document.write("<td>GPA</td><td align='center'>" + gpatotalvalue + "</td>");
document.write("</tr>");
document.write("</Table>");
//-->
</script>
</font></body>
</html>
答案 0 :(得分:1)
这是一个工作版本,虽然我不确定GPA计算是否正确,但新西兰没有GPA,因此不确定其计算方法。我在代码中添加了一些注释,指出你出错的地方:)
<html>
<body>
<script language="JavaScript">
<!--
var classCtr = 0; // should be initialised to 0 so it can be incremented
var nmAnswer;
var clsGrade;
var totalvalue;
var gpatotalvalue = 0;
var totalvalue = 0;
// set up one dimensional array
var myClass = new Array();
//removed some unused variables
do
{
// start columns in second dimension of the array
myClass[classCtr] = new Array();
// get values from user and put in array
myClass[classCtr][0] = prompt ("Enter Class Name");
myClass[classCtr][1] = prompt ("Enter grade recieved");
myClass[classCtr][2] = {"A":4,"B":3,"C":2,"D":1,"F":0}; //removed the myClass[classCtr][1] part
myClass[classCtr][3] = prompt ("Enter credit hours")
// accumulate the total value
totalvalue = totalvalue + parseFloat(myClass[classCtr][3]); //had class instead of myClass
gpatotalvalue = gpatotalvalue + parseFloat(myClass[classCtr][2][ myClass[classCtr][1] ] ); //moved this line above the classCtr++; line so you weren't accessing an undefined variable, there some changes here to access the myClass[classCtr][2] object value correctly..
// add one to the total number
classCtr++;
nmAnswer = prompt ("Do you have more classes");
} while (nmAnswer == "yes");
// set variable that is used as counter
clsGrade = 0;
// print out header for content
document.write("<H2 align='center'>Grade Point Average</H2><br>");
document.write("<table bgcolor='Grey' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<tr>");
document.write("<td>Class Name</td><td align='center'>Class Grade</td><td align='center'>Grade Credit</td>");
document.write("</tr>");
// Loop through array displaying html and javascript values in the array
while (myClass[clsGrade]) //the previous conditional would only out put the first "persons" grades so changed this to loop through until all peoples grades
{
document.write("<tr>");
document.write("<td>");
document.write (myClass[clsGrade][0]); //had class instead of myClass
document.write("</td>");
document.write("<td align='center'>");
document.write (myClass[clsGrade][1]);//had class instead of myClass
document.write("</td>");
document.write("<td align='center'>");
document.write (myClass[clsGrade][3]);//had class instead of myClass
document.write("</td>");
document.write("</tr>");
document.write("</td>");
// increment the counter
clsGrade++;
}
// finish the table of data and display the total value
document.write("</Table>");
document.write("<br>");
document.write("<table bgcolor='grey' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<tr>");
document.write("<td>Total value</td><td align='center'>" + totalvalue + "</td>");
document.write("</tr>");
document.write("<tr>");
document.write("<td>GPA</td><td align='center'>" + gpatotalvalue + "</td>");
document.write("</tr>");
document.write("</Table>");
//-->
</script>
</font></body>
</html>
答案 1 :(得分:0)
在这一行中,您使用class
代替myClass
:
myClass[classCtr][2] = {"A":4,"B":3,"C":2,"D":1,"F":0}[myClass[classCtr][1]];
在这些行中,您首先增加classCtr
,这意味着当您访问数组末尾之外的元素时,您将获得未定义的值:
gpatotalvalue = gpatotalvalue + parseFloat(myClass[classCtr][1]);
classCtr++;