gpt4 book ai didi

java - 将数据传递给方法

转载 作者:行者123 更新时间:2023-12-01 13:06:03 25 4
gpt4 key购买 nike

我试图将“personListToPrint”传递给 main.java 并在使用开关 3 时让它出现,但是它不起作用。我不知道如何解决这个问题。有什么想法吗??

这里是main.java

package hartman;


import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Printer.printWelcome();

Scanner keyboard = new Scanner(System.in);
ArrayList<Person> personList = new ArrayList<>();
boolean keepRunning = true;
while (keepRunning) {
Printer.printMenu();
Printer.printPrompt("Please enter your operation: ");

String userSelection = keyboard.nextLine();

switch (userSelection) {
case "1":
Database.addPerson(personList);
break;
case "2":
Database.printDatabase(personList);
break;
case "3":
Printer.printSearchPersonTitle();
String searchFor = keyboard.nextLine();
Database.findPerson(searchFor);
Printer.printPersonList(personListToPrint);
break;
case "4":
keepRunning = false;
break;
default:
break;
}
}

Printer.printGoodBye();
keyboard.close();
}

}

这里是database.java

package hartman;

import java.util.ArrayList;
import java.util.Scanner;

public class Database {
static Scanner keyboard = new Scanner(System.in);
private static ArrayList<Person> personList = new ArrayList<Person>();

public Database() {

}

public static void addPerson(ArrayList<Person> personList) {
Printer.printAddPersonTitle();
Printer.printPrompt(" Enter first name: ");
String addFirstName = keyboard.nextLine();

Printer.printPrompt(" Enter last Name: ");
String addLastName = keyboard.nextLine();

Printer.printPrompt(" Enter social Security Number: ");
String addSocial = keyboard.nextLine();

Printer.printPrompt(" Enter year of birth: ");
int addYearBorn = Integer.parseInt(keyboard.nextLine());

System.out.printf("\n%s, %s saved!\n", addFirstName, addLastName);
Person person = new Person();
person.setFirstName(addFirstName);
person.setLastName(addLastName);
person.setSocialSecurityNumber(addSocial);
person.setYearBorn(addYearBorn);
personList.add(person);
}

public static void printDatabase(ArrayList<Person> personList) {
System.out
.printf("\nLast Name First Name Social Security Number Age\n");
System.out
.printf("=================== =================== ====================== ===\n");
for (Person p : personList) {

System.out.printf("%-20s%-21s%-24s%s\n", p.getLastName(),
p.getLastName(), p.getSocialSecurityNumber(), p.getAge());

}

}

public static ArrayList<Person> findPerson(String searchFor) {
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;

if (p.getFirstName().equalsIgnoreCase(searchFor)) {
isAMatch = true;

}
if (p.getLastName().equalsIgnoreCase(searchFor)) {
isAMatch = true;

}
if (p.getSocialSecurityNumber().contains(searchFor)) {
isAMatch = true;
;
} else if (String.format("%d", p.getAge()).equals(searchFor))
if (isAMatch) {

}
matches.add(p);
Printer.printPersonList(matches);
}
return matches;

}

}

最后,这是printer.java

package hartman;

import java.util.ArrayList;

public class Printer {
public static void printWelcome() {
System.out.printf("WELCOME TO PERSON DATABASE!\n");
}

public static void printGoodBye() {
System.out.printf("\nGOOD BYE!!\n");
}

public static void printMenu() {
System.out.printf("\nMain Menu\n");
System.out.printf("---------\n\n");
System.out.printf(" 1. Add a new Person to the database.\n");
System.out.printf(" 2. Print the database.\n");
System.out.printf(" 3. Search for a person in the database.\n");
System.out.printf(" 4. Exit the application.\n");
System.out.printf("\n");
}

public static void printPrintMenu() {
System.out.printf("Print\n\n");
}

public static void printAddPersonTitle() {
System.out.printf("\nAdd Person to Database\n\n");

}

public static void printPrompt(String promptForWhat) {
System.out.printf("%s", promptForWhat);
}

public static void printPersonSaved(Person personSaved) {
System.out.printf("%s", personSaved);
}

public static void printSearchPersonTitle() {
System.out.printf("\nSearch for Person in Database\n\n");
System.out.printf("Enter search value: ");
}

public static void printPersonList(ArrayList<Person> personListToPrint) {

System.out
.printf("\nLast Name First Name Social Security Number Age\n");
System.out
.printf("=================== =================== ====================== ===\n");
for (Person p : personListToPrint) {

System.out.printf("%-20s%-21s%-24s%s\n", p.getLastName(),
p.getLastName(), p.getSocialSecurityNumber(), p.getAge());

}

}
}

最佳答案

您没有定义变量

personListToPrint

将案例 3 替换为下一个

case "3":
Printer.printSearchPersonTitle();
String searchFor = keyboard.nextLine();
ArrayList<Person> personListToPrint = Database.findPerson(searchFor);
Printer.printPersonList(personListToPrint);
break;

现在可以编译了

关于java - 将数据传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23259038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com