Java 为什么我不能使用静态方法从我的主要?
我在做日记节目Java 为什么我不能使用静态方法从我的主要?,java,static,args,Java,Static,Args,我在做日记节目 在日记课上,我做了一个叫做“addStudent”的方法 但我不明白为什么它在我的主屏幕上是看不见的 编辑我添加了代码,很抱歉只添加了一个块,而不是3个块,但是lombok导入导致了格式问题 EDIT2我删除了剩下的问题 public class Main { public static void main(String[] args) { } } import lombok.AllArgsConstructor; import lombok.Data; i
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;
}
}
公共类主{
公共静态void main(字符串[]args){
}
}
导入lombok.allargsconstuctor;
导入龙目数据;
导入java.util.ArrayList;
导入java.util.List;
@AllArgsConstructor
@资料
公开课日记{
List-listOfStudents=new-ArrayList();
公共静态无效添加学生(学生…学生){
学生=新学生(Student.askAboutGrades(),Student.askAboutIndexNumber(),Student.askAboutName(),Student.askaboutburName());
}
}
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入java.util.Scanner;
@AllArgsConstructor
@资料
公立班学生{
列表等级=新的ArrayList();
字符串索引编号;
字符串名;
串姓;
公共静态字符串askAboutName(){
System.out.println(“键入名称”);
扫描仪=新的扫描仪(System.in);
字符串名称=scanner.nextLine();
虽然
(!(name.matches(“[a-zA-Z]+”)){
System.out.println(“名称不正确,请重试”);
name=scanner.nextLine();
}
System.out.println(“名称似乎不错”);
返回名称;
}
公共静态字符串askaboutburname(){
System.out.println(“键入姓氏”);
扫描仪=新的扫描仪(System.in);
字符串姓氏=scanner.nextLine();
while(!(姓氏匹配(“[a-zA-Z]+”)){
System.out.println(“姓氏不正确,请重试”);
姓氏=scanner.nextLine();
}
System.out.println(“姓氏似乎不错”);
返回姓氏;
}
公共静态字符串askAboutIndexNumber(){
System.out.println(“键入索引号(6位!)”;
扫描仪=新的扫描仪(System.in);
字符串indexNumber=scanner.nextLine();
而(!indexNumber.matches(\\d{6}”)){
System.out.println(“索引号不正确,请重试”);
indexNumber=scanner.nextLine();
}
System.out.println(“索引似乎不错”);
返回索引编号;
}
公共静态列表askAboutGrades(){
System.out.println(“键入等级,用逗号(\”,\”)分隔”);//todo检查是否有效,并将限制器从2设置为5.5
扫描仪=新的扫描仪(System.in);
字符串输入=scanner.nextLine();
字符串[]arrayOfGrades=input.split(“,”);
List listOfGrades=新的ArrayList(Arrays.asList(arrayOfGrades));
List listOfGradesDouble=new ArrayList();
字符串温度;
对于(int i=0;i
Github:
哇。我会尝试:
1.你怎么称呼它?应该是这样的
Diary.addStudent();
Powodzenia)请在问题中发布您的代码。请发布代码,不要链接到代码(或代码图像)。除上述内容外,请在每篇文章中提出一个问题。对于@PradeepSimha指出的内容,不要在一次邮递中问多个问题:@WJS我试图添加代码,但它不能很好地处理从Lombok导入的内容,不知道为什么。给我一点时间,我会解决这个问题的,真是个愚蠢的错误。我现在觉得自己像个傻瓜。谢谢,我只是想通过键入方法名来调用它。。。它是静态的,因为我想从“自身”而不是从object@santana011所以,若你们坚持它应该是静态的,那个么,很可能,你们会希望列表是静态的。然而,我不认为它必须是静态的。我想了一会儿,你是对的。让它静止,改变它是没有意义的。