【发布时间】:2014-05-05 22:33:59
【问题描述】:
我怎样才能将其称为几乎在我的代码末尾显示
/**
* getData method shows the data stored in
* the StudentReport1 object
*
* @param data The data to show
*/
public static void getData(StudentReport1 data, double average, double gpa)
{
JOptionPane.showMessageDialog(null, "Name :" + data.getName() + "\nGrade"
+ data.getSGrade() + "\nSchool: "
+ data.getSName()
+ "\nAverage: " + average +
"\nGPA: " + gpa + ".");
}
在从 setData 方法获取信息后,我在最后显示它,但问题是我以后如何调用它,它一直说:错误不能应用于给定类型;要求:StudenteReport1,双倍,双倍;发现:没有参数;原因:实际参数列表和形式参数列表的长度不同
这是我的全部代码
import javax.swing.*;
/**
* The ReportGenerator class uses the StudentReport1 class to simulate the report
* of a student. Holds fields for number of classes,grades received, average, and gpa.
*
* @author Luis Fierro
* @version 4/16/2014
*/
public class ReportGenerator
{
/**
* getData method creates a StudentReport1 object
* pobulated with data from the user
*
* @return A reference to StudentReport1
*/
public static StudentReport1 setData()
{
//variables to hold the information
String name;
String sGrade;
String sName;
//read the information
name=JOptionPane.showInputDialog("Enter your name: ");
sGrade=JOptionPane.showInputDialog("Enter your grade in school: ");
sName=JOptionPane.showInputDialog("Enter the name of your school: ");
//create a StudentReport entry
StudentReport1 data = new StudentReport1(name, sGrade, sName);
//return a reference to the object
return data;
}
public static double getAverage(double total, int numOfClasses)
{
return total / numOfClasses;
}
public static double getGpa(double average)
{
return (average*4)/100;
}
/**
* getData method shows the data stored in
* the StudentReport1 object
*
* @param data The data to show
*/
public static void getData(StudentReport1 data, double average, double gpa)
{
JOptionPane.showMessageDialog(null, "Name :" + data.getName() + "\nGrade"
+ data.getSGrade() + "\nSchool: "
+ data.getSName()
+ "\nAverage: " + average +
"\nGPA: " + gpa + ".");
}
public static void main(String [] args)
{
// declare variables
int numOfGrades=0; //number of classes of the student
int grades; //grades received in each class
double average; //average= sum of all grades / number of classes
double gpa; //average converted to scale 1-4
int total=0; //total of the grades
String trash; //to convert s tring to double
setData();
//ask number of grades
trash=JOptionPane.showInputDialog("Number of grades:");
numOfGrades=Integer.parseInt(trash);
//get the grades added together in order to calculate the average
for (int i = 1; i <=numOfGrades; i++)
{
trash=JOptionPane.showInputDialog("Test " + i + " : " );
grades=Integer.parseInt(trash);
total+=grades;
}
// Get the average
average = getAverage(total, numOfGrades);
// Get the gpa
gpa = getGpa(average);
//display all of the information
getData(StudentReport1, average, gpa);
JOptionPane.showMessageDialog(null, "The students average is: " + average + "\nThe
student's GPA is: " + gpa);
}
}
【问题讨论】:
-
在您尝试调用此
static方法的位置发布代码。 -
您了解错误吗?再读一遍。您似乎在没有参数的情况下调用它,如下所示:
getData(),而它需要 3 个参数。 -
来吧,错误信息就在那里。它说您的方法需要
StudentReport1和两个double参数,但您没有提供任何参数。这有什么令人困惑的?
标签: java static-methods getter-setter