Javascript任务让我难过

时间:2014-02-27 10:13:39

标签: javascript html

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

所有作业的总分数为400分

不允许用户输入负分数,并且不允许他们输入超出分配可能总分限制的分数。输入所有分数后,我将分数显示为百分比和字母等级。

到目前为止有效: 该计划不接受否定,或任何高于设定的可能点数限制的等级。我无法获得“输入有效分数(0-max)”消息,因此我废弃了该功能,相反,在输入适当的值之前,程序不会向前移动。我也能够显示百分比。但是一旦你查看代码,你会看到程序实际上没有计算百分比,它只是计算(总和/总)* 100以后我做一个document.writeln我只需添加一个“%”符号。我发现这样更容易。

我无法工作: 我不能得到该死的字母等级。我知道,因为我将字母等级设置在特定范围内(例如93.9%-90%是A-),如果我这样做,我应该使用,但我不确定我是否做得对,我也设置了一个名为“字母”的变量,我试图给出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>

1 个答案:

答案 0 :(得分:0)

你错过了一个+来连接字符串

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

应该是

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

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