【问题标题】:Javascript assignment has me stumpedJavascript作业让我难过
【发布时间】:2014-02-27 18:19:17
【问题描述】:

我需要做什么: 我需要建立一个程序,要求用户输入他们的成绩 1)assignment1 - 25分 2)assignment2 - 25分 3)assignment3 - 25分 4)assighment4 - 25分 5)exam1 - 100 分 6)期末考试 - 100分 7)最终项目 - 100分

因此所有作业的总分是 400 分

用户不得输入负分数,也不得输入超出作业总分限制的分数。输入所有分数后,我将分数显示为百分比和字母等级。

目前有效: 该计划将不接受负数,或任何超出作业可能设置的分数限制的成绩。我无法让“输入有效分数(0-max)”消息起作用,所以我放弃了该功能,而是在输入适当的值之前程序不会继续前进。我也可以显示百分比。但是一旦你查看代码,你会发现程序实际上并没有计算百分比,它只是计算 (sum/total)*100 稍后我做了一个 document.writeln,我只是添加了一个“%”符号。我发现这样更容易。

我不能去工作: 我不能得到该死的字母等级。我知道因为我将字母等级设置在一个特定范围内(例如 93.9%-90% 是 A-),所以我应该使用 if 语句,所以我这样做了,但我不确定我是否做得对,我还设置了一个名为“letter”的变量,我试图根据学生跌落的范围给出A、A-、B+、B、B-等的值。我无法让它工作。

这是我的代码,任何帮助将不胜感激。

我这里有点问题。

我可以让程序将所有成绩相加并除以总数,从而给我一个百分比并在我运行程序时显示它。我似乎无法做到的是显示字母等级。

到目前为止,我有 if 语句和一个名为 letter 的变量,我试图将它插入到我们在代码中看到的第一个 document.writeln 中,但是当我尝试使用这个变量并运行程序时屏幕变白。没有提示输入分数,什么都没有,它只是变白。

我认为这是我执行 if 语句的方式有问题,但我不知道。

这是我的代码,不胜感激

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8">
        <Title>Final Grade Calculation</Title>
        <script type = "text/javascript">
            var assignment1;
            var assignment2;
            var assignment3;
            var assignment4;
            var exam1;
            var finalexam;
            var finalproject;
            // the above variables represent the assignments, exams, and project.
            // the following lines of code will ask the user to enter their scores. We    will also try
            // to make sure that the user is not entering negative numbers, or numbers above the max set of points possible for each
            //assignment. Eash assignment is worth 25 points
            //exams are 100
            //and the final project is 100 as well, the total points possible should then be no more than 400
            //before we begin to prompt the user to enter their scores we will also declare a few more variables
            var a1; //assignment 1
            var a2; //assignment 2
            var a3; //assignment 3
            var a4; //assignment 4
            var e1; //exam1
            var ef; //final exam
            var fp; //final project
            var g; //is going to be ((a1+a2+...fp)/tp)
            var tp = 400; //total possible points
            var letter;
            //the following loops ensure that the user can only enter scores that fall withing the acceptable range for each respective assignment
            //assignments have  a min of 0, and a max of 25
            //exam1 has a min of 0, and a max of 100
            //finalexam has a min of 0 and a max of 100
            //final project has a min of 0 and a max of 100
            //if there is no score entered into the prompt then the variable for the respective assignment is 0 by default.


            do
            {
                assignment1=window.prompt("Enter your score for assignment 1");
                a1=parseInt(assignment1);
                //window.alert("Invalid Score entered");
            }
            while (a1 < 0 || a1 > 25 )
            if (assignment1=null)
            a1=0


            do
            {
                assignment2=window.prompt("Enter your score for assignment 2");
                a2=parseInt(assignment2);
                //window.alert("Invalid Score entered");
            }
            while (a2 < 0 || a2 > 25 )
            if (assignment2=null)
            a2=0

            do
            {
                assignment3=window.prompt("Enter your score for assignment 3");
                a3=parseInt(assignment3);
                //window.alert("Invalid Score entered");
            }
            while (a3 < 0 || a3 > 25 )
            if (assignment1=null)
            a3=0

            do
            {
                assignment4=window.prompt("Enter your score for assignment 4");
                a4=parseInt(assignment4);
                //window.alert("Invalid Score entered");
            }
            while (a4 < 0 || a4 > 25 )
            if (assignment4=null)
            a4=0

            do
            {
                exam1=window.prompt("Enter your score for exam 1");
                e1=parseInt(exam1);
                //window.alert("Invalid Score entered");
            }
            while (e1 < 0 || e1 > 100 )
            if (exam1=null)
            e1=0

            do
            {
                finalexam=window.prompt("Enter your score for the final exam");
                ef=parseInt(finalexam);
                //window.alert("Invalid Score entered");
            }
            while (ef < 0 || ef > 100 )
            if (finalexam=null)
            ef=0

            do
            {
                finalproject=window.prompt("Enter your score for the final project");
                fp=parseInt(finalproject);
                //window.alert("Invalid Score entered");
            }
            while (fp < 0 || fp > 100 )
            if (finalproject=null)
            fp=0

            // g is the variable which will be representing the final grade.
            //is will give us a decimal as an answer
            g = ((a1+a2+a3+a4+e1+ef+fp)/tp)*100
            document.writeln("<h1>Your final score is " + g.toFixed(2) +"%</h>"); //we convert the decimal into a percentage, not really though
            //the percentage displayed is only superficial, to be honest I was getting frustrated trying to find a way to make real percents
            //the following if's will assign letter grades to a range of decimals.



            if (g>=94)
            {letter="A"}
            else if (g>=93.90 || g<=90.00)
            {letter="A-"}
            else if (g>=89.90 || g<=87.00)
            {letter="B+"}
            else if (g>=86.90 || g<=84.00)
            {letter="B"}
            else if (g>=83.90 || g<=80.00)
            {letter="B-"}
            else if (g>=79.90 || g<=77.00)
            {letter="C+"}
            else if (g>=76.90 || g<=74.00)
            {letter="C"}
            else if (g>=73.90 || g<=70.00)
            {letter="C-"}
            else if (g>=69.90 || g<=67.00)
            {letter="D+"}
            else if (g>=63.90 || g<=60.00)
            {letter="D-"}
            else
            {letter="F"};

            document.writeln("<h1> Your grade is " + letter "</h1>");
        </script>    
    </head>
</html>

【问题讨论】:

  • document.writeln("

    你的成绩是"+字母"

    ");在字母和结束标记之间缺少 +。即 document.writeln("

    你的成绩是 " + letter + "

    ");

标签: javascript html


【解决方案1】:

你缺少一个 + 来连接字符串

document.writeln("<h1> Your grade is " + letter "</h1>");

应该是

document.writeln("<h1> Your grade is " + letter + "</h1>");

除此之外它工作正常:http://jsfiddle.net/fqv9t/(document.write 被替换为小提琴警报)

【讨论】:

  • 感谢您的反馈,我添加了 + 并且我成功了,现在我只需要它来显示适当的字母等级......不能得到任何低于 A- 我认为我的 If 语句都搞砸了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多