显示百分比和缺席

时间:2015-11-17 19:39:12

标签: javascript

我已经去了一个星期了,似乎无法弄明白......

该程序要求用户输入他们的缺勤数量和他们的分数,但如果学生有5个或更多缺席,他们会自动失败,无论他们的百分比如何。

  • A级 - 90至100
  • B级 - 80至89.9
  • C级 - 70至79.9
  • D 60至69.9
  • 60以下的任何东西都是F。

到目前为止,这是我的代码:

<html>
<head>
</head>
<body>
<script type="text/javascript">

//Program Name: gradeReporter.html
//Author: 
//Date: November 5th 2015
//Purpose: Calculate grades and absences of students 

//Variables
var percent = parseInt(percent * 1.03);
var absences = parseInt(absences);
var ES = ""; // Empty string
var PA = "<p />"; // HTML paragraph break
var BR = "<br />"	// HTML line break 
//Start the program 

if (absences >= 5) {
  document.write("You have failed due to having more than 5 absences");

  if (percent <= 100 || percent >= 90 && absences <=2) {
    document.write("Your Grade: A. Your percent: " * percent);
  } 
  else if (percent <= 89.9 || percent >= 80 && absences <=2) { 
    document.write("Your Grade: B. Your percent: " * percent);
  }
  else if (percent <= 79.9 || percent >= 70 && absences <=2) {
    document.write("Your Grade: C. Your percentage: " * percent);
  }
  else if (percent <= 69.9 || percent >= 60 && absences <=2) {
    document.write("Your Grade: D. Your percentage: " * percent);
  }
  else if (percent <= 59.9 || percent >= 50 && absences <=2) {
    document.write("Grade: F. You have failed this semester.");
  }
}
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要首先定义百分比和缺席数字。此外,当您说“您的百分比”时,您需要使用“+”而不是“*”。此外,您的一些括号错位。此代码现在应该有效:

//Variables
var percent = 80;
var absences = 6;
percent = percent * 1.03;
var ES = ""; // Empty string
var PA = "<p />"; // HTML paragraph break
var BR = "<br />"   // HTML line break 
//Start the program 

if (absences >= 5) {
document.write("You have failed due to having more than 5 absences");
}
if (percent <= 100 || percent >= 90 && absences <=2) {
document.write("Your Grade: A. Your percent: " + percent);
} 
else if (percent <= 89.9 || percent >= 80 && absences <=2) { 
document.write("Your Grade: B. Your percent: " + percent);
}
else if (percent <= 79.9 || percent >= 70 && absences <=2) {
document.write("Your Grade: C. Your percentage: " + percent);
}
else if (percent <= 69.9 || percent >= 60 && absences <=2) {
document.write("Your Grade: D. Your percentage: " + percent);
}
else if (percent <= 59.9 || percent >= 50 && absences <=2) {
document.write("Grade: F. You have failed this semester.");
}