开发者社区> 问答> 正文

为什么我不能从主菜单使用静态方法?

我正在写日记程序。

在日记类中,我制作了一个名为“ addStudent”的方法,但是我不知道为什么它在我的主体中不可见? 编辑我添加了代码,对不起一个代码块,而不是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

展开
收起
垚tutu 2019-12-19 17:01:26 795 0
1 条回答
写回答
取消 提交回答
  • 以下工作对我来说很好,只需将JLabel添加到JPanel并将JPanel添加到JPopupMenu

    JPanel panelLabel = new JPanel();
    JLabel lblSomeText = new JLabel("Some text"); lblSomeText.setFont(menuItem.getFont()); lblSomeText.setForeground(menuItem.getForeground()); panelLabel.add(lblSomeText); popupMenu.add(panelLabel);

    2021-02-08 18:24:21
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
对象的生命期管理 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载