Могу ли я и как я могу вызвать основной метод из другого метода в Java? Пожалуйста, объясните, используя код ниже:Вызов главного метода Java
public class arraysAndMethods {
public void printArray(double[] arr) {
int x = public arraysAndMethods.main(int[] studGrades);
// pass array in main mehthod to other methods
//int a=main(args[]);
for (int i = 0; i < studGrades.lenght; i++)
System.out.print(studGrades[i] + " ");
}// end of printArray method
public static void main(String args[]){
java.util.Scanner input = new java.util.Scanner(System.in); // input scanner
System.out.println("What is the size of the class?");
int n = input.nextInt();
double studGrades[] = new double[n];
for (int i = 0; i < studGrades.length;i++) {
System.out.println("What is the grade of student #" + (i+1));
studGrades[i] = input.nextDouble();
} // end of for loop
}// end of main method
}//end of class
ПОСЛЕ EDIT:
Я понял это для прохождения в аргументах, за исключением. Использовать ли возвращаемое значение из основного? А так как возвращаемое значение из main - это массив, должны ли параметры для вызова main иметь скобки? Должна ли быть разница между этими скобками?
public class arraysAndMethods {
public void printArray(double[] arr) {
int x = arraysAndMethods.main(double[i] arr);//error with paremeters
for (int i = 0; i < studGrades.lenght; i++)
System.out.print(studGrades[i] + " ");
}// end of printArray method
public static double[] main(String args[]){// double array
java.util.Scanner input = new java.util.Scanner(System.in); // input scanner
System.out.println("What is the size of the class?");
int n = input.nextInt();
double[] arr = new double[n];// declare and initialize array to have n many elements
for (int i = 0; i < arr.length;i++) {// input grades for each students
System.out.println("What is the grade of student #" + (i+1));
arr[i] = input.nextDouble();
} // end of for loop
return arr;
}// end of main method
}// end of class
Это еще один метод: 'arraysAndMethods.main (new String [] {" foo "," bar "})' ... –
Зачем вам это нужно? – Wabonano
Просто выбросьте это; если вы не выполняете какую-то странную загрузку или операцию загрузки/управления, вы должны ** ** ** напрямую вызывать метод 'main'. – Qix