請教JAVA程式,想修改一下@@

2014-12-15 12:44 am
你好,我打算用javascript設計一個等級計算器,可是不太會,已經有大大賜教了謝謝~不過想修改一下,可是不會,有大大能教我一下嗎?

https://tw.knowledge.yahoo.com/question/question?qid=1014121302862

==============

<script type="text/javascript">
function calculate() {
var total = 0;
total += get_score("page1", "result1");
total += get_score("page2", "result2");
total += get_score("page3", "result3");
var total_grade = get_grade(total / 3);
document.getElementById("overall").innerHTML = total_grade;
}
function get_score(score_from, result_to) {
var score = parseInt(document.getElementById(score_from).value);
document.getElementById(result_to).innerHTML = get_grade(score);
return score
}
function get_grade(score) {
var result = "D";
if (score >= 90) {
result = "A";
} else if (score >= 80) {
result = "B";
}
return result;
}
</script>

===================

想修改的部份:


例如說卷一、卷二、卷三同樣的分數給不同等級

卷一90分A級
卷二70分A級
卷三60分A級

只是很大概教一下就可以了,謝謝。初學者,想很久都沒頭緒。

回答 (2)

2014-12-16 4:30 am
✔ 最佳答案
<script type="text/javascript">
var GradeStrategy1 = function() {
this.getGrade = function(score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
return "F";
}
};
var GradeStrategy2 = function() {
this.getGrade = function(score) {
if (score >= 70) return "A";
if (score >= 60) return "B";
return "F";
}
};
var GradeStrategy3 = function() {
this.getGrade = function(score) {
if (score >= 60) return "A";
if (score >= 50) return "B";
return "F";
}
};
var OverallStrategy = function() {
this.getGrade = function(score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
if (score >= 60) return "D";
if (score >= 50) return "E";
return "F";
}
};
function calculate() {
var total = 0;
total += get_score("page1", "result1", new GradeStrategy1());
total += get_score("page2", "result2", new GradeStrategy2());
total += get_score("page3", "result3", new GradeStrategy3());
var overallStrategy = new OverallStrategy();
var total_grade = overallStrategy.getGrade(total / 3);
document.getElementById("overall").innerHTML = total_grade;
}
function get_score(score_from, result_to, strategy) {
var score = parseInt(document.getElementById(score_from).value);
document.getElementById(result_to).innerHTML = strategy.getGrade(score);
return score;
}
</script>
2014-12-15 5:14 pm
請留意,javascript和java是兩種不同的語言。


收錄日期: 2021-04-15 17:33:05
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20141214000051KK00059

檢視 Wayback Machine 備份