用参数java调用方法

时间:2015-09-25 12:11:41

标签: java

我看了,找不到像我想要的东西。我有一个方法,我需要从我的main方法调用3个参数。到目前为止,我已经尝试过在课堂上倾斜的所有内容,但我无法想象出来。这是我的Java编程课程。以下是我需要从主要方法调用的内容:

import java.util.*;// for scanner
import java.text.DecimalFormat;

public class Grades {

//Homework, exam1, and exam2 weights
static double homeworkWeight;
static double examOneWeight;
static double examTwoWeight;

//Homework
static int homeworkNumberOfAssignments;
static int homeworkAssignment1Score;
static int homeworkAssignment1Max;
static int homeworkAssignment2Score;
static int homeworkAssignment2Max;
static int homeworkAssignment3Score;
static int homeworkAssignment3Max;
static int homeworkSectionsAttended;
static int homeworkSectionsAttendedTotal;
static int homeworkSectionsAttendedMax;
double homeworkTotalPoints;
double homeworkMaxPoints;
double homeworkWeightedScore;

//Exam1
static int examOneScore;
static int examOneCurve;
static double examOneMaxPointsAvailable;
double examOneWeightedScore;

//Exam2
static int examTwoScore;
static int examTwoCurve;
static double examTwoMaxPointsAvailable;
double examTwoWeightedScore;

//Grades
static double courseGrade;
static double grade;

public static void main(String[] args) {

    Scanner console = new Scanner(System.in);
    showIntro();
    System.out.println("");
    System.out.print("Homework and Exam 1 weights? ");
    homeworkWeight = console.nextInt();
    examOneWeight = console.nextInt();
    examTwoWeight = 100 - homeworkWeight + examOneWeight;
    System.out.println("Using weights of " + homeworkWeight + " " + examOneWeight + " " + examTwoWeight);
    homework();
    System.out.println("");
    exam1();
    //System.out.println("");
    //exam2();
    //System.out.println("");
    //courseGrade(courseGrade; double homeworkWeightedScore; double examOneWeightedScore; double examTwoWeightedScore;);
    double d = courseGrade(homeworkWeightedScore, examTwoWeightedScore, examTwoWeightedScore);
    System.out.println("");
}//

//Shows the intro to the program to the user.
public static void showIntro() {

    System.out.println("This program accepts your homework scores and");
    System.out.println("scores from two exams as input and computes");
    System.out.println("your grades in the course.");
}

public static void homework() {

    Scanner console = new Scanner(System.in);
    System.out.println("");
    System.out.println("Homework:");
    System.out.print("Number of assignments? ");
    homeworkNumberOfAssignments = console.nextInt();
    System.out.print("Assignment 1 score and max? ");
    homeworkAssignment1Score = console.nextInt();
    homeworkAssignment1Max = console.nextInt();
    System.out.print("Assignment 2 score and max? ");
    homeworkAssignment2Score = console.nextInt();
    homeworkAssignment2Max = console.nextInt();
    System.out.print("Assignment 3 score and max? ");
    homeworkAssignment3Score = console.nextInt();
    homeworkAssignment3Max = console.nextInt();
    System.out.print("Sections attended? ");
    homeworkSectionsAttended = console.nextInt();
    homeworkSectionsAttendedTotal = homeworkSectionsAttended * 4;
    homeworkSectionsAttendedMax = 20;

    //Calculating total points earned
    double totalPoints = homeworkAssignment1Score + homeworkAssignment2Score + homeworkAssignment3Score + homeworkSectionsAttendedTotal;

    //Calutaing the max points available to be earned
    double maxPoints = homeworkAssignment1Max + homeworkAssignment2Max + homeworkAssignment3Max + homeworkSectionsAttendedMax;

    //Formatting with DecimalFormat to remove the decimal and 0 when displaying
    DecimalFormat df = new DecimalFormat("###.#");
    System.out.println(("Total points = ") + df.format(totalPoints) + " / " + df.format(maxPoints));

    //Calculating the weighted score by dividing totalPoints by maxPoints and then multiplying times homeworkWeight
    double homeworkWeightedScore = ((totalPoints / maxPoints) * homeworkWeight);

    //Printing out weighted score and rounding to the nearest hundreth with Math.round
    System.out.println("Weighted score = " + Math.round(homeworkWeightedScore * 100.0) / 100.0);

}

public static void exam1() {

    Scanner console = new Scanner(System.in);
    System.out.println("Exam 1:");
    System.out.print("Score? ");
    examOneScore = console.nextInt();
    System.out.print("Curve? ");
    examOneCurve = console.nextInt();
    System.out.println("Total points = " + examOneScore + " / " + examOneCurve);
    examOneMaxPointsAvailable = 100;
    double examOneWeightedScore = ((examOneScore / examOneMaxPointsAvailable) * examOneWeight);
    System.out.println("weighted score = " + Math.round(examOneWeightedScore * 100.0) / 100.0);

}

public static void exam2() {

    Scanner console = new Scanner(System.in);
    System.out.print("Exam 2:");
    System.out.print("Score? ");
    examTwoScore = console.nextInt();
    System.out.print("Curve? ");
    examTwoCurve = console.nextInt();
    System.out.print("Total points = ");
    System.out.println("weighted score = ");

}


  public double courseGrade(double homeworkWeightedScore, double   examOneWeightedScore, double examTwoWeightedScore) {

     return homeworkWeightedScore + examOneWeightedScore + examTwoWeightedScore;
 }

}

1 个答案:

答案 0 :(得分:0)

您的问题中缺少一些因素使我无法完全回答这些因素,但是:

courseGrade方法是否与您的public static void main方法在一个单独的类或同一个类中?

是:通过public SeparateClass instance = new SeparateClass();

内的public static void main创建单独类的新实例

之后,请使用main方法调用此方法:double grade = instance.courseGrade(homeworkWeightedScore, examTwoWeightedScore, examTwoWeightedScore);

否:将courseGrade方法设为静态,不能从静态方法调用非静态方法(将public double courseGrade替换为public static double courseGrade)。之后,在main方法内,执行以下操作:double d = courseGrade(homeworkWeightedScore, examTwoWeightedScore, examTwoWeightedScore);

我希望这会有所帮助。