gpt4 book ai didi

java - "p"无法解析为变量

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

我必须使用 switch 语句来允许用户选择他们想要执行的操作,如果他们选择“1”,则将允许他们将一个人添加到数据库中。在“1”的 switch 语句中,我收到一个语法错误,指出“p”无法解析为变量。然而,我已经尝试了我能想到的一切来让它发挥作用,但它不会。有什么想法吗?

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(p);
break;
case "2":
Database.printDatabase(personList);
break;
case "3":
Printer.printSearchPersonTitle();
String searchFor = keyboard.nextLine();
Database.findPerson(searchFor);
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;

public Database() {

}

public static void addPerson(Person personList2) {
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(personList2);
}

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;
;
}
if (String.format("%d", p.getAge()).equals(searchFor))
if (isAMatch) {

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

}

}

最佳答案

编译器无法将 p 解析为变量,因为您在任何地方都没有声明 p。

更好的解决方案:

我认为直接在数据库中进行人员创建过程要好得多,因此请执行以下操作:将 Database.java 更改为:

public static void addPerson() {
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);
}

将第一个代码更改为:

Database.addPerson();

关于java - "p"无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23232767/

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