我一直無法理解Java方法的概念,而且我需要爲Grade Calculator程序編寫一個代碼,這個程序不會比我得到的那麼混亂。如何在Java中爲不同的任務創建不同的方法,然後調用它們?
這是我有什麼(這一切都在一個方法):
public class GradeCalculator2{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int students;
String studentName;
System.out.println("Welcome to GradeCalculator program.");
System.out.println("Author: Hayden Wires");
System.out.println("Date: 10/25/2016");
System.out.print("Enter the number of students:");
students = sc.nextInt();
System.out.println("Enter next student's information:");
System.out.print("Name:");
studentName = sc.next();
for(int i = students; i > 1; i++){
double quizScore1;
double quizScore2;
double quizScore3;
double quizScore4;
double quizScore5;
System.out.print("Quiz 1:");
quizScore1 = sc.nextDouble();
quizScore1 /= 100;
quizScore1 *= .02;
System.out.print("Quiz 2:");
quizScore2 = sc.nextDouble();
quizScore2 /= 100;
quizScore2 *= .02;
System.out.print("Quiz 3:");
quizScore3 = sc.nextDouble();
quizScore3 /= 100;
quizScore3 *= .02;
System.out.print("Quiz 4:");
quizScore4 = sc.nextDouble();
quizScore4 /= 100;
quizScore4 *= .02;
System.out.print("Quiz 5:");
quizScore5 = sc.nextDouble();
quizScore5 /= 100;
quizScore5 *= .02;
double totalQuizScores = quizScore1 + quizScore2 + quizScore3 + quizScore4 + quizScore5;
totalQuizScores /= 5;
double a1_Score;
double a2_Score;
double a3_Score;
double a4_Score;
double a5_Score;
double a6_Score;
System.out.print("Assignment 1:");
a1_Score = sc.nextDouble();
a1_Score /= 100;
a1_Score *= .025;
System.out.print("Assignment 2:");
a2_Score = sc.nextDouble();
a2_Score /= 100;
a2_Score *= .025;
System.out.print("Assignment 3:");
a3_Score = sc.nextDouble();
a3_Score /= 100;
a3_Score *= .05;
System.out.print("Assignment 4:");
a4_Score = sc.nextDouble();
a4_Score /= 100;
a4_Score *= .05;
System.out.print("Assignment 5:");
a5_Score = sc.nextDouble();
a5_Score /= 100;
a5_Score *= .05;
System.out.print("Assignment 6:");
a6_Score = sc.nextDouble();
a6_Score /= 100;
a6_Score *= .1;
double a1_a2_Scores = a1_Score + a2_Score;
double a3_a4_a5_Scores = a3_Score + a4_Score + a5_Score;
double assignmentScoresTotal = a1_a2_Scores + a3_a4_a5_Scores + a6_Score;
assignmentScoresTotal /= 3;
double mt1Score;
System.out.print("Midterm 1:");
mt1Score = sc.nextDouble();
mt1Score /= 100;
mt1Score *= .1;
double mt2Score;
System.out.print("Midterm 2:");
mt2Score = sc.nextDouble();
mt2Score /= 100;
mt2Score *= .2;
double mtScoresTotal = mt1Score + mt2Score;
mtScoresTotal /= 2;
double finalScore;
System.out.print("Final:");
finalScore = sc.nextDouble();
finalScore /= 100;
finalScore *= .3;
double totalGrade = assignmentScoresTotal + totalQuizScores + mtScoresTotal + finalScore;
totalGrade *= 100;
totalGrade *= 2;
System.out.println(studentName + "'S SCORE:" + totalGrade);
String letterGrade;
if (totalGrade >= 93){
letterGrade = "A";
} else if ((totalGrade >= 90)&&(totalGrade < 92)){
letterGrade = "A-";
} else if ((totalGrade >= 87)&&(totalGrade < 90)){
letterGrade = "B+";
} else if ((totalGrade >= 83)&&(totalGrade < 87)){
letterGrade = "B";
} else if ((totalGrade >= 80)&&(totalGrade < 83)){
letterGrade = "B-";
} else if ((totalGrade >= 77)&&(totalGrade < 80)){
letterGrade = "C+";
} else if ((totalGrade >= 74)&&(totalGrade < 77)){
letterGrade = "C";
} else if ((totalGrade >= 70)&&(totalGrade < 74)){
letterGrade = "C-";
} else if ((totalGrade >= 68)&&(totalGrade < 70)){
letterGrade = "D+";
} else if ((totalGrade >= 60)&&(totalGrade < 68)){
letterGrade = "D";
} else {
letterGrade = "F";
}
System.out.println(studentName + "'S LETTER GRADE:" + letterGrade);
}
}
}
我理解這一點非常凌亂,可能很難看,但是這就是我需要幫助。我知道有一種方法可以減少使用方法重複使用該程序的難度,但我無法在這個程序中理解這個概念。
這一切都是在Oracle的在線Java教程很好解釋。我建議你[從這裏開始](https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html) –
在http://codereview.stackexchange上,好的人會更好地服務這個問題。 .COM /。 –