我已经在下面包含了我的程序,基本上我的问题是如果我输入一个姓氏以允许我找到我要删除的学生如何将输入的字符串与数组中的姓氏变量进行比较?
当我输入姓氏并尝试将其与数组中已有的条目进行比较而不是比较姓氏时,它将输入的字符串与该实例中的所有内容进行比较。这是在findStudent方法中完成的。
我想因为我在这里传递整个阵列姓氏包含我为该学生输入的所有内容(姓氏,姓名,考试成绩)。如果我尝试传递surname参数,我会收到错误消息。
我已经搜索过这个问题是否已经回答了,但我能找到的只是将输入的字符串与已经定义的数组进行比较,而不是根据用户输入创建的数组。 任何帮助将不胜感激!
由于
import java.util.Scanner;
/**
*
* @author Connor
*/
public class Student {
//declare student variables
private String surname;
private String forename;
private int mark1, mark2, mark3;
private double dblScore;
private static String course = "French";
//constructor
//===========================================================
//
// MODULE : Student
// RETURN TYPE : None
// PARAMETERS : args : String newForename, String newSurname, int newMark1, int newMark2, int newMark3
// DESCRIPTION : Construct Student object
//============================================================
public Student(String newForename, String newSurname, int newMark1, int newMark2, int newMark3) {
this.forename = newForename;
this.surname = newSurname;
this.mark1 = newMark1;
this.mark2 = newMark2;
this.mark3 = newMark3;
this.dblScore = (this.mark1 + this.mark2 + this.mark3) / 3;
}
//===========================================================
//
// MODULE : getCourse
// RETURN TYPE : String course
// PARAMETERS : args :
// DESCRIPTION : get course name
//============================================================
public static String getCourse() {
return course;
}
//===========================================================
//
// MODULE : setCourse
// RETURN TYPE : course
// PARAMETERS : args :
// DESCRIPTION : set course name
//============================================================
public static String setCourse(String newCourse) {
course = newCourse;
return course;
}
//display student details method
//===========================================================
//
// MODULE : displaydetails
// RETURN TYPE : None
// PARAMETERS : args : [DEFAULT]
// DESCRIPTION : write out student details -
// name, course and exam marks
//============================================================
public void displaydetails() {
System.out.println(" ");
System.out.println("Student details - ");
System.out.println("Name - " + surname + " " + forename);
System.out.println("Course - " + course);
System.out.println("exam scores - " + mark1 + " " + mark2 + " " + mark3);
System.out.println("Overall score - " + dblScore);
System.out.println(" ");
}
}
class StudentMenu {
//===========================================================
//
// MODULE : add student
// RETURN TYPE : None
// PARAMETERS : args : studentArray, numStudents
// DESCRIPTION : requests user input to add student to array.
// Upto 6 students can be entered
//============================================================
public static void addStudent(Student[] studentArray, int numStudents) {
String newSurname, newForename;
int newMark1, newMark2, newMark3;
Scanner input = new Scanner(System.in);
System.out.println("1. Add a student");
System.out.println(" ");
System.out.println("Student surname - ");
newSurname = input.next();
input.nextLine();
System.out.println("Student forename - ");
newForename = input.next();
input.nextLine();
System.out.println("First exam mark - ");
newMark1 = input.nextInt();
input.nextLine();
System.out.println("Second exam mark - ");
newMark2 = input.nextInt();
input.nextLine();
System.out.println("Third exam mark - ");
newMark3 = input.nextInt();
input.nextLine();
studentArray[numStudents] = new Student(newSurname, newForename, newMark1, newMark2, newMark3);
}
//===========================================================
//
// MODULE : find student
// RETURN TYPE : int position
// PARAMETERS : args : surname, delSurname, position
// DESCRIPTION : finds the position of the selected student
// in the array
//============================================================
public static void findStudent(Student[] surname,String delSurname, int position, int totalStudents) {
int index;
for (index = 0; index < totalStudents; index++) {
// if (surname[index].equals(delSurname)) {
if (surname[index].equals(delSurname)) {
position = index;
} else {
index++;
}
}
}
//===========================================================
//
// MODULE : delete student
// RETURN TYPE : None
// PARAMETERS : args : myClass, position, NUM_STUDENTS
// DESCRIPTION : deletes chosen student from array. Moves
// all other entries down by one
//============================================================
public static void deleteStudent(Student[] myClass, int position, int NUM_STUDENTS) {
int index = 0;
for (index = position + 1; index < NUM_STUDENTS; index++) {
myClass[index - 1] = myClass[index];
}
}
//===========================================================
//
// MODULE : main method
// RETURN TYPE : None
// PARAMETERS : args : myClass, position, NUM_STUDENTS
// DESCRIPTION : runs menu program allowing user input
// to add/modify/delete student information
//============================================================
public static void main(String[] args) {
//declare scanner
Scanner input = new Scanner(System.in);
//declare variables
boolean menu = true;
int option;
int totalStudents = 0;
final int NUM_STUDENTS = 6;
int position = 0;
String delSurname, newCourse;
//array
Student[] myClass;
myClass = new Student[NUM_STUDENTS];
//main method
//menu
while (menu != false) {
System.out.println("1. Add a student");
System.out.println("2. Delete student");
System.out.println("3. Display all students");
System.out.println("4. Change course details");
System.out.println("5. Search for student");
System.out.println("6. Exit program");
// enter choice
System.out.print("Please enter selection - ");
option = input.nextInt();
System.out.println(" ");
// calling methods using switch
if ((option > 6) || (option < 1)) {
System.out.println("Invalid selection made - reenter. Between options 1-6 only.");
} else {
switch (option) {
case 1:
//option 1 - add student
addStudent(myClass, totalStudents);
totalStudents++;
break;
case 2:
//option 2 - delete student
System.out.println("Student surname to delete - ");
delSurname = input.next();
input.nextLine();
findStudent(myClass,delSurname, position, totalStudents);
if (position >= 0 && position < NUM_STUDENTS) {
deleteStudent(myClass, position, NUM_STUDENTS);
}
totalStudents--;
break;
case 3:
//option 3 - display details
Student.getCourse();
for (int index = 0; index < totalStudents; index++) {
myClass[index].displaydetails();
}
break;
case 4:
//option 4 - change course
Student.getCourse();
System.out.println();
System.out.println("Enter new course details - ");
newCourse = input.next();
input.nextLine();
Student.setCourse(newCourse);
break;
case 5:
//option 5 - search for student by name
System.out.println("Student surname to display - ");
delSurname = input.next();
input.nextLine();
findStudent(myClass,delSurname, position, totalStudents);
if (position >= 0 && position < NUM_STUDENTS) {
myClass[position].displaydetails();
}
break;
case 6:
//option 6 - exit program
menu = false;
break;
}
}
}
}
}
答案 0 :(得分:0)
您必须检查是否surname[index].surname.euqals(delSurname)
,因为在surname
数组中您有Student
而不是字符串
现在你的代码检查Student类的实例是否等于String类的实例,但不能。