为什么我不能使用我的主静态方法?

问题描述 投票:-2回答:1

我正在编写日记程序。

  1. 在日记类中,我制作了名为“ addStudent”的方法但是我不知道为什么它在我的主体中不可见?

[EDIT我添加了代码,对不起一个代码块,而不是3个代码,但是lombok导入导致格式化问题

EDIT2我删除了剩余的问题

public class Main {
    public static void main(String[] args) {

    }
}

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@Data

public class Diary {


        List<Student> listOfStudents = new ArrayList<>();



    public static void addStudent(Student... students){
        Student student = new Student(Student.askAboutGrades(), Student.askAboutIndexNumber(), Student.askAboutName(), Student.askAboutSurname());

    }
}



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

@AllArgsConstructor
@Data

public class Student {
    List<Double> grades = new ArrayList<>();
    String indexNumber;
    String name;
    String surname;

    public  static String askAboutName() {
        System.out.println("Type the name");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        while
        (!(name.matches("[a-zA-Z]+"))) {
            System.out.println("incorrect name, try again");
            name = scanner.nextLine();
        }
        System.out.println("Name seems good");
        return name;
    }

    public static String askAboutSurname() {
        System.out.println("Type the surname");
        Scanner scanner = new Scanner(System.in);
        String surname = scanner.nextLine();
        while (!(surname.matches("[a-zA-Z]+"))) {
            System.out.println("incorrect surname, try again");
            surname = scanner.nextLine();
        }
        System.out.println("surname seems good");
        return surname;
    }

    public static String askAboutIndexNumber() {
        System.out.println("Type the index number (6 digits!)");
        Scanner scanner = new Scanner(System.in);
        String indexNumber = scanner.nextLine();
        while (!indexNumber.matches("\\d{6}")) {
            System.out.println("incorrect index number, try again");
            indexNumber = scanner.nextLine();
        }
        System.out.println("index seems good");
        return indexNumber;
    }

    public static List<Double> askAboutGrades() {
        System.out.println("Type the grades,separated by comma (\",\")");//todo check if works and set limiter from 2 to 5.5
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();

        String[] arrayOfGrades = input.split(",");
        List<String> listOfGrades = new ArrayList<>(Arrays.asList(arrayOfGrades));
        List<Double> listOfGradesDouble = new ArrayList<>();
        String temp;
        for (int i = 0; i < listOfGrades.size(); i++) {
            temp = listOfGrades.get(i);
            listOfGradesDouble.add(Double.parseDouble(temp));
        }
        return listOfGradesDouble;
    }
}

Github:https://github.com/Karol011/SDA/tree/master/zadania/src/main/java/Kolekcje/ListyKolekcje/Diary

java static args
1个回答
0
投票

哇。我会尽力:1.你怎么称呼它?应该像

Diary.addStudent();
  1. Ehmmm。您打算如何从静态方法访问非静态列表?以及为什么它是静态的?

  2. 听起来很奇怪。

  3. 弄清楚什么是静态的以及何时使用它。在大多数情况下,您不需要它。另外,我将删除所有的龙目岛,并尝试使用纯Java来解决问题。恕我直言。

Powodzenia)

© www.soinside.com 2019 - 2024. All rights reserved.