方法返回零

时间:2012-03-06 00:03:42

标签: java

假设返回19个完整席位的值,剩下68个学生! 请帮助,根据我的理解,我将返回正确的值并将它们分配给正确的变量!

public class JetCalculator
{
    public static void main(String[] args)
    {
        int totalStudents = 3013;
        int jetCapacity = 155;
        int jets;
        int students;
        jets = calculateJets(jetCapacity, totalStudents);
        students = calculateStudents(jetCapacity, totalStudents, jets);

        System.out.println("A total of "+ totalStudents + " student require "+ jets + " full seated jets.");
        System.out.println("There will be " + students + " students remaining");
        System.out.println("_____________________________________________");
        System.out.println(jets);
        System.out.println(jetCapacity);
        System.out.println(students);
    }

    public static int calculateJets(int totalStudents, int jetCapacity)
    {
        int fullJets;
        fullJets = totalStudents / jetCapacity;
        return fullJets;
    }

    public static int calculateStudents( int jetCapacity, int totalStudents, int jets)
    {
        int remainingStudents;
        remainingStudents = jetCapacity * jets;
        return remainingStudents;
    }
}

3 个答案:

答案 0 :(得分:5)

以这种方式致电calculateJets

jets = calculateJets(jetCapacity, totalStudents);

但是这个方法的参数名称暗示你已经在调用中切换了它们的顺序

public static int calculateJets(int totalStudents, int jetCapacity)

这意味着您实际上正在使用整数运算155 / 3013 0

答案 1 :(得分:1)

您将参数传递回前方。

您通过传递容量然后向学生调用calculateJetscalculateJets(jetCapacity, totalStudents);但该方法会向学生询问容量:calculateJets(int totalStudents, int jetCapacity)

这是整个类接口中参数顺序一致性的一个很好的论据。

为了帮助将来调试,请尝试在方法开头使用println来查看发生的情况:

System.out.println("Called calculateJets with totalStudents of " + totalStudents + " and jetCapacity of " + jetCapacity);

答案 2 :(得分:0)

完全基于方法名称:您的意思是说remainingStudents = totalStudents - (jetCapacity * jets);吗?