如何在Java中为不同的任务创建不同的方法,然后调用它们?

时间:2016-10-26 18:03:07

标签: java methods call

我一直无法理解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);
        }
    }
}

我知道这非常混乱,可能很难阅读,但这就是我需要帮助的地方。我知道有一种方法可以使用方法减少冗余,我只是无法在这个程序的上下文中掌握这个概念。

3 个答案:

答案 0 :(得分:2)

Java是精心设计的面向对象编程语言。面向对象的哲学与继承高度相关。 Java是在OOP的继承属性上构建的。由于继承,你不能拥有全局方法。

以下示例不是良好的面向对象实践,只是为了显示过程:

public class myClass{
public static void main(String[] args){
    f1();

}

private void f1(arg1, arg2){
//your code here
} //end of method f1
} //end of class

这不是一个好的面向对象实践,因为你通常将你的主(驱动)类和其他类分开,而在主类中你只有主要的方法。

答案 1 :(得分:1)

要缩短代码,您需要寻找模式!

了解如何以类似的方式处理每个测验和每个作业的得分?

您总是按照以下方式处理测验:

System.out.print("Quiz <insert a number here>:");
quizScoreX = sc.nextDouble();
quizScoreX /= 100;
quizScoreX *= .02;

这里唯一不同的是“测验”一词之后的数字。

您总是按照以下方式处理作业:

System.out.print("Assignment <insert number here>:");
aX_Score = sc.nextDouble();
aX_Score /= 100;
aX_Score *= <insert number here>;

这里唯一不同的是“作业”一词之后的数字以及你将得分乘以的数字。

看模式?

这意味着我们可以将每个测验和每个作业的得分分为两种方法:

private static double getQuizScore(int number) {
    System.out.print("Quiz " + Integer.toString(number) + ":");
    double quizScore = sc.nextDouble();
    quizScore /= 100;
    quizScore *= .02;
    return quizScore;
}

private static double getAssignmentScore(int number, double multiplier) {
    System.out.print("Assignment " + Integer.toString(number) + ":");
    doubke assignmentScore = sc.nextDouble();
    assignmentScore /= 100;
    assignmentScore *= multiplier;
    return assignmentScore;

}

另请注意,您将获得5个测验的分数。每次,你正在做的只是递增测验号码。然后你将它们加在一起。这可以简化为:

private static double getTotalQuizScore() {
    double total = 0;
    for (int i = 0 ; i < 5 ; i++) {
        total += getQuizScore(i + 1);
    }

    return total;
}

然后,您可以在代码中使用这两种方法:

        double totalQuizScores = getTotalQuizScore();
        totalQuizScores /= 5;

        double a1_Score = getAssignmentScore(1, 0.25);
        double a2_Score = getAssignmentScore(2, 0.25);
        double a3_Score = getAssignmentScore(3, 0.05);
        double a4_Score = getAssignmentScore(4, 0.05);
        double a5_Score = getAssignmentScore(5, 0.05);
        double a6_Score = getAssignmentScore(6, 0.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);

编辑:我忘了提及,你还需要将sc移到类级别,以便方法可以访问它:

移动此行:

Scanner sc = new Scanner(System.in);

在主方法之外,并在开头添加private static

private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
    // ...
}

答案 2 :(得分:1)

您似乎重复运行相同的计算:

System.out.print("Quiz 1:");
quizScore1 = sc.nextDouble();
quizScore1 /= 100;
quizScore1 *= .02;

这可以用方法

代替
double readScore(Scanner sc, String title, double weight)
{
    System.out.print(title);
    double result = sc.nextDouble();
    result /= 100.0;
    result *= weight;
    return result;
}

然后,你的主要功能如下:

quizScore1 = readScore(sc, "Quiz 1", 0.02);
:
a2_Score = readScore(sc, "Assignment 2", 0.025);
etc...

这样就可以从主函数中删除大量重复的代码。

之后,你应该学习数组,所以你没有a1_score到a6_score等...