简体   繁体   English

我在哪里添加公共 static void main(String[] args)

[英]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).我不确定如何/在哪里添加公共 static void main(String[] args)。 I am getting an error to define the main method as: public static void main(String[] args).我在将主要方法定义为时出错:public static void main(String[] args)。 When I tried to add it in it gave me errors with the rest of the code.当我尝试添加它时,它给了我代码的 rest 错误。 Any feedback is helpful, I have had this issue before and am not sure how to add the main method.任何反馈都是有帮助的,我之前遇到过这个问题,不知道如何添加 main 方法。

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:公共 static void main(String[] args) function 应封装在 class 中,例如:

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但最好对类进行分区,例如 main 应该在 Main.java 下单独创建

PS: you can add at most one Main method in each class PS:每个class最多可以添加一个Main方法

Best Regards,此致,

It is a method so it goes inside the class next to the other methods.这是一种方法,因此它在其他方法旁边进入 class 内部。

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. class 中方法的相对顺序无关紧要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该把 public static void main(String[] args) 放在哪里? - Where do I put public static void main(String[] args)? 我应该在哪里放置“公共静态void main(String [] args)”? - Where will i put “Public static void main(String[] args)”? 我必须在公共静态void main(String args [])中使用名称args吗? - Do I have to use the name args in public static void main(String args[]) 需要添加“ public static void main(String [] args)”, - Need to add a “public static void main(String[] args)”, 为什么 eclipse 在我写 public static void main(String[] args) 的行中显示错误 - Why eclipse show me error in the line where i have written public static void main(String[] args) 了解公共静态void main(String [] args) - Understanding public static void main(String[] args) 对main方法的误解/“ public static void main(String [] args)”的观点 - Misunderstanding main method/the point of “public static void main(String[] args)” public static void main(String args[]) 为什么 string args[] 是强制性的,为什么我们不使用 public static void main(Object args[])? - public static void main(String args[]) why string args[] is compulsory ,why we not use public static void main(Object args[])? 使用 int 而不是 String:public static void main (int[] args) - Using int Instead Of String: public static void main (int[] args) 代码签名和编写步骤 public static void main(String args[] ) - Code signature and steps to write public static void main(String args[] )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM