简体   繁体   中英

where do i add the public static void main(String[] args)

I am not sure how/where to add the public static void main(String[] args). I am getting an error to define the main method as: public static void main(String[] args). When I tried to add it in it gave me errors with the rest of the code. Any feedback is helpful, I have had this issue before and am not sure how to add the main method.

import java.util.ArrayList;

public class Main {


private ArrayList <String> studentNames = new ArrayList <String> ();
private char[] studentLetterGrades = { 'A', 'B', 'C','D','F'};
private double[][] studentsTestScores; //all students test scores


public String getStudentName(int studentIndex){
return studentNames.get(studentIndex);
}

public double calculateAverageTestScore(double [] studentsTestScores){
double studentTestScoreTotal = 0;
double studentTestScoreAverage; 

for( int currentStudentTestScore = 0; currentStudentTestScore < studentsTestScores.length;currentStudentTestScore++){  
    studentTestScoreTotal = studentTestScoreTotal + studentsTestScores[currentStudentTestScore];
}

studentTestScoreAverage = studentTestScoreTotal / studentsTestScores.length;
return studentTestScoreAverage;
}

public char getStudentLetterGrade(double studentTestScoreAverage){
char studentLetterGrade = 'Z';

if(studentTestScoreAverage < 60){
studentLetterGrade = 'F';
} else if(studentTestScoreAverage < 70){
studentLetterGrade = 'D';
} else if(studentTestScoreAverage < 80){
studentLetterGrade = 'C';
} else if(studentTestScoreAverage < 90){
studentLetterGrade = 'B';
 } else if(studentTestScoreAverage <= 100){
 studentLetterGrade = 'A';
}
return studentLetterGrade;
 }


public void setStudentName( String studentName){
studentNames.add(studentName);
}

 public void setStudentScore( int studentIndex, int scoreIndex, double studentScore){
 studentsTestScores[ studentIndex][scoreIndex] = studentScore;

 }
public double [][] getStudentTestScoresArray(){
return studentsTestScores;
}

 public Main( int numberOfStudents, int numberOfTestScoresPerStudent){
studentsTestScores = new double[numberOfStudents][numberOfTestScoresPerStudent];


}
}

the public static void main(String[] args) function should be encapsulated within a class for instance:

class Main{

 public static void main(String[] args) {

 }

}

in your case:

import java.util.ArrayList;

public class Main {


private ArrayList <String> studentNames = new ArrayList <String> ();
private char[] studentLetterGrades = { 'A', 'B', 'C','D','F'};
private double[][] studentsTestScores; //all students test scores


public String getStudentName(int studentIndex){
    return studentNames.get(studentIndex);
}

public double calculateAverageTestScore(double [] studentsTestScores){
   double studentTestScoreTotal = 0;
   double studentTestScoreAverage; 

   for( int currentStudentTestScore = 0; currentStudentTestScore < 
      studentsTestScores.length;currentStudentTestScore++){  
          studentTestScoreTotal = studentTestScoreTotal + studentsTestScores[currentStudentTestScore];
   }

   studentTestScoreAverage = studentTestScoreTotal / studentsTestScores.length;
   return studentTestScoreAverage;
}

public char getStudentLetterGrade(double studentTestScoreAverage){
   char studentLetterGrade = 'Z';

   if(studentTestScoreAverage < 60){
      studentLetterGrade = 'F';
   } else if(studentTestScoreAverage < 70){
      studentLetterGrade = 'D';
   } else if(studentTestScoreAverage < 80){
      studentLetterGrade = 'C';
   } else if(studentTestScoreAverage < 90){
      studentLetterGrade = 'B';
   } else if(studentTestScoreAverage <= 100){
       studentLetterGrade = 'A';
   }
   return studentLetterGrade;
}


public void setStudentName( String studentName){
   studentNames.add(studentName);
}

public void setStudentScore( int studentIndex, int scoreIndex, double studentScore){
    studentsTestScores[ studentIndex][scoreIndex] = studentScore;
}
public double [][] getStudentTestScoresArray(){
   return studentsTestScores;
}

public Main( int numberOfStudents, int numberOfTestScoresPerStudent){
   studentsTestScores = new double[numberOfStudents][numberOfTestScoresPerStudent];
}
// add the main method here 
}
// add the main method
   public static void main(String[] args) {

    }

but it's better to partition your classes for instance the main should be created alone under Main.java

PS: you can add at most one Main method in each class

Best Regards,

It is a method so it goes inside the class next to the other methods.

public class Main {

  public static void main(String[] args) {
  }

  public void setStudentName( String studentName){
    studentNames.add(studentName);
  }

  [... other methods ...]

}

The relative order of methods inside your class does not matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM