我一直从方法中得到零

时间:2016-05-07 15:43:42

标签: java if-statement methods

这是主要方法

package javaapplication50;

import java.util.Scanner;

public class JavaApplication50 {

    public static void main(String[] args) {

                System.out.print(" > Enter student ID: ");

                int id = in.nextInt();

                System.out.print("> Enter student First Name: ");

                String name = in.next();

                System.out.print("> Enter student Last Name: ");
                String last = in.next();

                JavaApplication50 app = new JavaApplication50();

                System.out.println(" > Enter students Grades: ");

                double aptTestGrade = app.readStudentGrade(0, 100, " > Enter the grade of Aptitude Test: ", in);
                double introYearGrade = app.readStudentGrade(0, 100, " > Enter the grade of Introductory Year: ", in);
                double schoolGrade = app.readStudentGrade(0, 100, " > Enter the grade of School: ", in);

                System.out.println("");

                String AF;

                double finalgrade = 0;

                calculateFinalGrades(aptTestGrade, introYearGrade, schoolGrade);

                if (finalgrade >= 90 && finalgrade <= 100) {

                    AF = "Medicine";

                } else if (finalgrade >= 85 && finalgrade <= 90) {

                    AF = "Engineering";
                } else if (finalgrade >= 80 && finalgrade <= 85) {
                    AF = "Computing";

                } else if (finalgrade >= 75 && finalgrade <= 80) {
                    AF = "Science";

                } else if (finalgrade >= 70 && finalgrade <= 75) {

                    AF = "Economics";

                } else {

                    AF = "Not Assigned";

                }

                System.out.println("> " + name + " " + last + " (ID #" + id + ") has been added to the system.");

                System.out.println("> Final Grade:  " + finalgrade + "  Assigned Faculty:  " + AF);

我使用此方法强制用户输入0到100之间的测试等级。

    double readStudentGrade(double min, double max, String message, Scanner in) {
        while (true) {
            System.out.print(message);
            double input = in.nextDouble();
            if (input <= max && input >= min) {
                return input;
            } else {
                System.out.println("");
                System.out.println(" >    Invalid input! ((must be between " + min + " and " + max + ")");
                System.out.println(" >    Please try again.\n"
                        + "");
                System.out.println("");
            }
        }

    }

这种方法是计算最终成绩,但出于某种原因,我的成绩仍为零。

public static double calculateFinalGrades(double aptTestGrade, double introYearGrade, double schoolGrade) {

    double finalgrade;

    double finalgrade1 = (aptTestGrade * 30) / 100;

    double finalgrade2 = (introYearGrade * 50) / 100;

    double finalgrade3 = (schoolGrade * 20) / 100;

    finalgrade = finalgrade1 + finalgrade2 + finalgrade3;

    return finalgrade;

}

提前致谢。

3 个答案:

答案 0 :(得分:3)

您需要指定finalgrade变量:

double finalgrade = calculateFinalGrades(aptTestGrade, introYearGrade, schoolGrade);

答案 1 :(得分:3)

问题出在这里。

#include <boost/date_time/posix_time/posix_time.hpp>
namespace posixtime = boost::posix_time;
...
posixtime::ptime startTime = posixtime::microsec_clock::local_time();
while ((posixtime::microsec_clock::local_time() - start).total_seconds() < 30)
{
    ...

解决此问题:

double finalgrade = 0; // finalgrade = 0

calculateFinalGrades(aptTestGrade, introYearGrade, schoolGrade); // method returns finalgrade but return value never assigned to finalgrade

所以现在当方法返回时,它会将返回值赋给finalgrade并更新其值。

答案 2 :(得分:1)

finalgrade被分配为0,而不是需要保留calculateFinalGrades(aptTestGrade, introYearGrade, schoolGrade);,然后您正在调用calculateFinalGrades,但没有任何内容保留返回值,所以

添加:

finalgrade = calculateFinalGrades(aptTestGrade, introYearGrade, schoolGrade);

而不是:

double finalgrade = 0;
calculateFinalGrades(aptTestGrade, introYearGrade, schoolGrade);