作者:拍友2502883387 | 来源:互联网 | 2024-09-30 10:25
我正在使用数组列表通过构建一个交互式菜单供用户选择来存储来自用户输入的值。到目前为止,我的两个选择是为用户提供向列表输入数据和读取列表的全部内容。到目前为止,我创建的代码由两个类组成。
我正在使用数组列表通过构建一个交互式菜单供用户选择来存储来自用户输入的值。到目前为止,我的两个选择是为用户提供向列表输入数据和读取列表的全部内容。到目前为止,我创建的代码由两个类组成。
我的主课,
package com.andrekreou;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to the personnel address book");
System.out.println("In the following menu, a whole selection of services is provided");
Scanner user_input = new Scanner(System.in);
while (true){
showMenu();
String selection = user_input.next();
if (selection.equals("1")){
System.out.println("Below you can see all of the data being provided");
for (String personnel : catalog) { } //ERROR: Cannot resolve symbol catalog
}else if (selection.equals("2")){
ArrayList catalog = new ArrayList<>();
Personnel p1 = new Personnel();
System.out.println("Please insert the data for the new contact");
System.out.println("Input the fullname:");
Scanner scan = new Scanner(System.in);
String full_name = scan.nextLine();
p1.setFull_name(full_name);
catalog.add(full_name);
System.out.println("You inserted the following fullname: "+p1.getFull_name());
System.out.println("Input the phonenumber:");
Scanner phone_number_input = new Scanner(System.in);
String phone_number = phone_number_input.next();
p1.setPhone_number(phone_number);
catalog.add(phone_number);
System.out.println("You inserted the following phonenumber: "+p1.getPhone_number());
System.out.println("Input the address:");
Scanner address_input = new Scanner(System.in);
String address = address_input.nextLine();
p1.setAddress(address);
catalog.add(address);
System.out.println("You inserted the following address: "+p1.getAddress());
System.out.println("???????? ??? ????????? e-mail:");
Scanner email_input = new Scanner(System.in);
String email = email_input.next();
p1.setEmail(email);
catalog.add(email);
System.out.println("You inserted the following e-mail: "+p1.getEmail());
System.out.println("???????? ??? ?????????? ????????:");
Scanner date_of_birth_input = new Scanner(System.in);
String date_of_birth = date_of_birth_input.nextLine();
p1.setDate_of_birth(date_of_birth);
catalog.add(date_of_birth);
System.out.println("You inserted the following: "+p1.getDate_of_birth());
System.out.println("???????? ??? ?????? ????:");
Scanner AMKA_input = new Scanner(System.in);
String AMKA = AMKA_input.next();
p1.setAMKA(AMKA);
catalog.add(AMKA);
System.out.println("You inserted the following ????: "+p1.getAMKA());
}
}
}
static void showMenu(){
System.out.println("1. View the whole contacts");
System.out.println("2. Insert a new contact");
System.out.println("Please give your choice");
}
}
以及我的人员类,带有 getter 和 setter 方法,以便存储来自用户输入的数据,
package com.andrekreou;
import java.io.Serializable;
public class Personnel implements Serializable {
private String full_name;
private String phone_number;
private String address;
private String email;
private String date_of_birth;
private String AMKA;
public String getFull_name() {
return full_name;
}
public void setFull_name(String full_name) {
this.full_name = full_name;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDate_of_birth() {
return date_of_birth;
}
public void setDate_of_birth(String date_of_birth) {
this.date_of_birth = date_of_birth;
}
public String getAMKA() {
return AMKA;
}
public void setAMKA(String AMKA) {
this.AMKA = AMKA;
}
}
我的问题是我想使用 foreach 循环在选项 1 中使用目录列表,但我不能,因为我收到代码中显示的“无法解析符号目录”错误。我究竟做错了什么?
回答
我不会争论解决方案的正确性,以及实现它的不同方式。然而,如果你想解决那个编译错误,这只是变量作用域的问题:你只需要catalog
在main
函数的开头移动列表的创建,以增加其作用域的方式,例如你可以把它作为第一条语句,如下所示:
public class Main {
public static void main(String[] args) {
ArrayList catalog = new ArrayList<>(); // Creation of catalog list
...
}
...
}