【问题标题】:For each loop in java对于java中的每个循环
【发布时间】:2014-05-04 05:34:32
【问题描述】:
    package edu.tests.model;

    /**
     * TestResult represents the grade on a test for one student.
     * 
     * @author 
     * @version 
     */
    public class TestResult {

        private String id;
        private int grade;

        /**
         * Creates a new instance of TestResult with the specified
         * ID of the student who took the test and the grade the
         * student earned. 
         * 
         * @requires id != null && id.length() > 0 && grade >= 0
         * @ensures  getId().equals(id) && getGrade() == grade
         * 
         * @param id the student's ID
         * @param grade the student's grade on the test
         */
        public TestResult(String id, int grade) {
            this.id = id;
            this.grade = grade;
        }

        /**
         * Returns the ID of the student.
         * 
         * @requires nothing
         * 
         * @return the student's ID
         */
        public String getId() {
            return this.id;
        }

        /**
         * Returns the grade earned by the student.
         * 
         * @requires nothing
         * 
         * @return the student's test grade
         */
        public int getGrade() {
            return this.grade;
        }


        /** 
         * Returns true if the specified object is a TestResult instance
         * with the same student ID, and false otherwise.
         * 
         * @requires nothing
         * 
         * @return true iff someObject is a TestResult with the same student ID
         */
        @Override
        public boolean equals(Object someObject) {
            if (this == someObject) {
                return true;
            }
            if (someObject == null) {
                return false;
            }
            if (this.getClass() != someObject.getClass()) {
                return false;
            }
            TestResult aTestResult = (TestResult) someObject;

            return this.getId().equals(aTestResult.getId());
        }

    }

package edu.westga.tests.model;

import java.util.ArrayList;

/**
 * Gradebook represents the test results for all students who took a test.
 * 
 * @author
 * @version 
 */
public class Gradebook {

    // TODO #1: declare an instance variable called results, which is an
    // ArrayList of TestResult objects
    ArrayList<TestResult> results;

    /**
     * Creates a new Gradebook object with no test results.
     * 
     * @requires nothing
     * @ensures size() = 0
     */
    // TODO#2: implement a constructor with 0 parameters that
    // initializes the instance variable as an empty ArrayList
    public Gradebook() {
        this.results = new ArrayList<TestResult>();
    }

    /**
     * Returns the number of test results that have been added to this
     * Gradebook.
     * 
     * @requires nothing
     * 
     * @return how many test results this Gradebook records
     */
    // TODO#3: implement the method size, which returns an int and takes
    // 0 parameter
    public int size() {
        return this.results.size();

    }

    /**
     * Adds the specified test result to this Gradebook.
     * 
     * @requires aResult != null && !contains(aResult)
     * @ensures size() == size()@prev + 1 && contains(aResult)
     * 
     * @param aResult
     *            the test result to add to this Gradebook
     */
    // TODO#4: implement the method add, with 1 parameter called aResult of type
    // TestResult
    // Please read the comments for the method to understand what it does
    public int add(TestResult aResult) {
        return this.add(aResult);
    }

    /**
     * Returns true if a TestResult with the same ID as the specifed test result
     * has previously been added to this Gradebook, and false otherwise.
     * 
     * @requires aResult != null
     * 
     * @return true iff the aResult has the same ID as a TestResult that has
     *         already been added
     * 
     * @param aResult
     *            the test result to check
     */
    // TODO#5: implement the method contains, which returns a boolean
    // and takes 1 parameter of type TestResult
    public boolean contains(TestResult aResult) {
        return this.contains(aResult);
    }

    /**
     * Returns the average grade of the test results that have been added to
     * this Gradebook, or 0 if none have been added.
     * 
     * @requires nothing
     * 
     * @return the mean grade
     */
    // TODO#6: implement method averageGrade, which returns an int
    // and takes 0 parameters

    public int averageGrade() {
        for (int i = 0; i < results.size(); i++);

        {
            TestResult theResult = results.get(i);
            int averageGrade = theResult.getGrade();

        }

    }

}

大家好,我正在尝试编写一个 for-each 循环,它将返回已添加到成绩册的测试结果的平均成绩。我是编写这些循环的新手,所以请耐心等待,但是当我将 i 放在 get 方法的括号中时,结果出现编译错误。我在想我做对了,我显然没有,所以非常感谢任何帮助。提前致谢,

【问题讨论】:

  • 1) foreach 循环在哪里? 2) 错误是什么?
  • 您发布的代码中没有foreach。
  • foreach 循环看起来像这样:foreach(TestResult result : results) { ... }。你只有一个普通的 for 循环
  • 我最近写了一篇关于for和foreach区别的长答案:stackoverflow.com/questions/85190/…。它没有回答这个具体问题,但您可能会觉得它很有趣。

标签: java for-loop each


【解决方案1】:

首先,我怀疑您的代码是否可以编译,因为您的 averageGrade() 方法没有返回任何内容...

试试这个:

public int averageGrade() {
        int totalGrade=0;
        int i = 0;
        for (; i < results.size(); i++);
        {
            TestResult theResult = results.get(i);
            totalGrade += theResult.getGrade();
        }
        if (i>0)
           return totalGrade/i;
        else return 0;

    }

【讨论】:

  • 是的,我忘了添加返回 this.averageGrade,我刚刚添加了。
【解决方案2】:

你有

for (int i = 0; i < results.size(); i++);

        {
            TestResult theResult = results.get(i);
            int averageGrade = theResult.getGrade();

        }

for 循环后不能有分号,需要返回一个 int ......所以你很可能需要需要

int averageGrade;
for (int i = 0; i < results.size(); i++)
      {
            TestResult theResult = results.get(i);
            averageGrade = theResult.getGrade();

        }
return averageGrade;

Java 认为 For 循环是独立的。然后它会看到这些随机大括号,并且对它们为什么存在感到非常困惑,因此会出现编译时错误。它还认为您将返回一个 int,而您却没有,因此它再次感到困惑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-18
    • 2015-12-03
    • 2020-04-06
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多