2015-09-25 124 views
-6

我看了,找不到像我想做的事情。我有一個方法需要3個參數,我需要從我的主要方法調用。到目前爲止,我已經嘗試了我在課堂上所學到的一切,但我無法想象這一點。這是我的Java程序設計課程。以下是我需要從我的主要方法調用:帶參數的調用方法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; 
} 

}

+0

這個方法和主要方法在同一個類中嗎? – McNultyyy

+1

請顯示一個簡短但完整的程序來展示問題。你顯然沒有嘗試*所有*,但我們不知道你有什麼*嘗試過,所以我們無法幫助你。 –

回答

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);

我希望這有助於。