我试图在这个程序中使用返回值,但它不会编译

时间:2011-07-20 17:31:57

标签: java

我尝试将总分和加权分数设为返回值,并从扫描仪控制台传递参数,并从静态void方法调用返回值。

import java.util.*;

public class Grades {
    public static void main (String[] args) {

        intro();

        Scanner console = new Scanner(System.in);

        int totalScoreMain = totalScore(score, curveNumber);
        double weightedScoreMain = weightedScore(weight, score, curveNumber);
        double weightedScore2Main = weightedScore2(weight2, sections, sumScore);

        for(int i = 1; i <= 2; exam++); {
            System.out.println("Exam i");
            exam();
        }
        homework();
}

public static void intro () {

        System.out.println("This program reads exam/homework scores");
        System.out.println("and reports your overall course grade.");
        System.out.println();
    }


public static void exam () {

        System.out.print("What is its weight (0-100)?");
        double weight = console.nextInt();
        System.out.print("Score earned?");
        int score = console.nextInt();
        System.out.print("Was there a curve (1=yes, 2=no)?");
        int curve = console.nextInt();
            if (curve == 1) {
                System.out.print("How much was the curve?");
                int curveNumber = console.nextInt();    
            } else if (curve == 2) {
                int curveNumber = 0;
            }

        totalScore(score, curveNumber);
        weightedscore(weight, score, curveNumber);

        System.out.println("Total points = " + totalScoreMain + "/" + "100");
        System.out.println("Weighted score = " + weightedScoreMain + "/" + weight);
    }

public static int totalScore (int score, int curveNumber) {

        int totalScore = Math.min(score + curveNumber, 100);
        return totalScore;
    }

public static double weightedScore (int weight, int score, int curveNumber) {

        double weightedScore = (score + curveNumber) * weight/100;
        return weightedScore;
    }

public static void homework () {

        System.out.print("What is its weight (0-100)?");
            int weight2 = console.nextInt();
        System.out.print("Number of assignments?");
            int number = console.nextInt();
            int sumScore = 0;
            int sumMax = 0;

        for(int i = 1; i <= number; i++) {
            System.out.println("Assignment " + i + "score and max?");
                int aScore = console.nextInt();
                int aScoreMax = console.nextInt();

                sumScore = sumScore + aScore;
                sumMax = sumMax + aScoreMax; }

            System.out.print("How many sections attended?");
                int section = console.nextInt();
                int sections = Math.min(3 * section, 20);
            System.out.println("Section points = " + sections);

            weightedScore2(weight2, sections, sumScore);

                System.out.println("Total points = " + (sections + sumScore) + "/" + sumMax);
                System.out.println("Weighted score = " + weightedScore2 + "/" + weight2);
        }


public static double weightedScore2(int weight2, int sections, int sumScore) {

            int weightedScore2 = weight2/100 * (sections + sumScore);
            return weightedScore2;
        }
    }       

有人可以帮我理解我做错了什么吗?我不明白为什么我粘贴的东西不起作用。

2 个答案:

答案 0 :(得分:2)

你似乎还没有理解变量的范围。程序的设计方式,main方法创建一个名为console的Scanner对象,但其引用只能在此方法中使用。为了在exam()中使用它,你必须以某种方式传递引用或者使它成为某种成员(或者是wahtever,不要试图在那里修复它们,因为它有更多的破坏)。但是,您很可能只想在exam()中创建对象,因为它仅用于该方法。

它与重量,分数等相同。你试图在主方法中使用它们,但那时,它们根本就不存在!

你应该尝试以几乎空的main方法开始,逐行添加并始终编译并尝试运行它,看看它是否像你想要的那样工作。如果没有,请就该特定问题寻求帮助。

在您发布的代码中,有很多错误,几乎不可能找到一个好的开始,就如何做出不同的建议提供建议。

答案 1 :(得分:1)

您永远不会声明得分,curveNumber或体重。在众多其他问题中。 编辑:另外,“我”在你的for循环中变成未申报的“考试”。