프로그램의 특정 부분에 문제가 있습니다. 내가하고 싶은 것은 반복, 검색 및 ArrayList에서 요소를 표시합니다.ArrayList의 특정 요소를 반복 검색하고 검색하는 방법
else if (menuChoice==3) {
System.out.println("Search Student:");
String search = input.nextLine();
for (Student student : students)
{
if (students.equals(search)){
System.out.println(student);
}
}
}
는 ArrayList의 접근을 통해 반복 것이라고 기대/디스플레이 그 목록에있는 특정 요소 :
그래서 나는 시도하는 코드의 주요 기능에 내 자신의 조각을 구현했습니다. 그것은 공백을 반환, 내가 뭘 잘못하고있어? 당신이 궁금해하는 경우여기에 전체 코드의 모든 이름은 다음의 고유 사용하는 경우
package student;
import java.util.*;
public class Student
{
private String r_name;
private int r_age;
private String r_course;
private String r_year;
private String r_section;
private String r_studno;
public Student(String name, int age, String course, String year, String section, String studno)
{
r_name = name;
r_age = age;
r_course = course;
r_year = year;
r_section = section;
r_studno = studno;
}
public String getName()
{
return r_name;
}
public int getAge()
{
return r_age;
}
public String getCourse()
{
return r_course;
}
public String getYear()
{
return r_year;
}
public String getSection()
{
return r_section;
}
public String getStudno()
{
return r_studno;
}
public String toString()
{
return "Name: " + r_name + ", Age: " + r_age +
", Course: " + r_course + ", Year: " + r_year +
", Section: " + r_section + ", Student Number: " + r_studno;
}
public static void main(String[] args)
{
ArrayList<Student> students = new ArrayList<Student>();
Scanner input = new Scanner(System.in);
int menuChoice = 4;
do {
System.out.println("\t\t\tStudent Record Menu");
System.out.println("\t\t1. Add Student\t2. View Students\t3. Search Student\t4. Exit");
try {
System.out.println("Enter a choice: ");
menuChoice = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
continue;
}
if (menuChoice==1)
{
System.out.println("Add a Student");
System.out.println("Full name:");
String name = input.nextLine();
int age = -1;
do {
try {
System.out.println("Age:");
age = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
System.out.println("Enter a number!");
continue;
}
} while (age <= 0);
System.out.println("Course:");
String course = input.nextLine();
System.out.println("Year:");
String year = input.nextLine();
System.out.println("Section:");
String section = input.nextLine();
System.out.println("Student Number:");
String studno = input.next();
Student student = new Student(name, age, course, year, section, studno);
students.add(student);
} else if (menuChoice==2) {
System.out.println("Students:");
for (Student student : students)
{
System.out.println(student);
}
} else if (menuChoice==3) {
System.out.println("Search Student:");
String search = input.nextLine();
for (Student student : students)
{
if (students.equals(search)){
System.out.println(student);
}
}
}
} while (menuChoice<4);
}
}
사용하려고하는 크레타레아. 이름, 코스, 섹션 또는 다른 이름으로 학생을 찾아야합니까? – talex
나는 위의 게시물에 명확하게 지정하지 않은 특정 요소 (이름, 나이, 코스, 연도 등 포함)의 전체 내용을 표시하고 싶었습니다. 미안합니다. – CinnamonRii
하지만 어떤 학생입니까? 특정 이름을 가진 모든 학생 또는 특정 학생에게? – talex