私はプログラミングが初めてで、Javaを学ぶことにしました。私はちょうど一次元の配列についての読書を終えたばかりで、私は検索に問題があります。Java用の単一配列の文字列の問題
私が作成したこのプログラムの概要は、どのくらい多くの学生がクラスに登録されるかをユーザーに尋ねることです。ユーザは次に、配列の長さに基づいて学生の名前を入力する。それから私は、ユーザーに学生名を検索させることができるようにしたい。どのように私はこれを達成することができますか?私が達成したいのは、ユーザーがファーストネームを入力すると、ファーストネームが一致するフルネームのリストを返すことです。私は本当にこれで苦労しています。高度な方法を教えてください。私は自分の本のペースペースにとどまりたいです。
私は、Javaプログラミングの包括的なバージョン第10版の紹介を使用しています。
import java.util.Scanner;
public class classSystem {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Weclome instructure to your Class System!");
System.out.println("Follow each steps to turn in your work instructor.");
System.out.println("\n1.) Enroll Students:");
System.out.print("\nHow many students are enrolled? ");
int studentAmount = input.nextInt();
String[] enrolledStudents = getStudentAttendance(studentAmount);
System.out.println("Here is your attendance list:");
for (int count = 0; count < enrolledStudents.length; count++) {
System.out.print("\n\t" + (count + 1) + ".) " + enrolledStudents[count]);
}
System.out.print("\n\nWhat sudent do you want to search: ");
String studentSearch = input.nextLine();
System.out.println(getStudent(enrolledStudents, studentSearch));
}
public static String[] getStudentAttendance(int studentAmount)
{
Scanner input = new Scanner(System.in);
String[] enrolledStudents = new String[studentAmount];
System.out.println("Input the students names:");
for (int count = 0; count < enrolledStudents.length; count++)
{
System.out.print((count + 1) + ".) ");
enrolledStudents[count] = input.nextLine();
}
return enrolledStudents;
}
public static String getStudent(String[] enrolledStudents, String StudentSearch)
{
for (int count = 0; count < enrolledStudents.length; count++)
{
if(StudentSearch.equals(enrolledStudents[count]))
{
return getStudent;
}
}
}
}
アドバイス:インデントが非常に重要です。あなたの本の例を見てみると、 'if'と' for'と '{'の間の他のコードブロックの内容が1つインデントされていることがわかります。また、名前の表記法もあります。変数名とメソッド名はCamelCaseの方が低いですが、クラス、インターフェース、列挙名はCamelCaseの上位です。 – RealSkeptic