如何检查用户输入是java中数组的一部分

2sbarzqh  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(540)

我想问问这是否可行。所以我有这个程序将进入一个for循环,以获得用户对主题数量的输入。之后,他将输入数组中列出的主题作为向导。我的目标是检查他的对象是否真的在我制作的数组中。我制作了一个程序,但我不知道程序将检查内容的部分放在哪里。
我的目标是:
为您选择的科目输入相应的代码:用户将输入8输入您希望注册的科目数:能够键入整个科目名称,如(math6100)微积分1
然后程序将检查输入的对象是否是数组中元素的一部分
更新:我做了另一个,但问题是我不知道将代码片段放在哪里,它将检查用户输入的内容,以获取他希望注册的主题列表。
代码如下:

  1. private static void check(String[] arr, String toCheckValue){
  2. boolean test=Arrays.asList(arr).contains(toCheckValue);
  3. System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);

}

  1. public static void main(String[] args){
  2. String arr[]={"(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1", "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1", "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1"};
  3. Scanner input1=new Scanner(System.in);
  4. System.out.print("\nEnter the number of subjects you wish to enroll: ");
  5. int number_subjects1=input1.nextInt();
  6. String []subjects1=new String[number_subjects1];
  7. //else statement when user exceeds the number of possible number of subjects
  8. if(number_subjects1<=8){
  9. for(int counter=0; counter<number_subjects1; counter++){
  10. System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): " + (counter+1));
  11. subjects1[counter]=input1.next();
  12. }
  13. String toCheckValue=subjects1[0];
  14. System.out.println("Array: " +Arrays.toString(arr));
  15. check(arr, toCheckValue);
  16. System.out.println("\nPlease check if these are your preferred subjects:");
  17. for(int counter=0; counter<number_subjects1; counter++){
  18. System.out.println(subjects1[counter]);
  19. }System.out.println("**********************************\n" + "\tNothing Follows");
  20. System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
  21. Scanner character=new Scanner(System.in);
  22. String answer_1subjectserrors=character.nextLine();
  23. System.out.println(answer_1subjectserrors + "Based on your answer, you need to refresh thae page and try again.");
  24. }

} }

i1icjdpr

i1icjdpr1#

我认为问题在于,您正在对照一个数组检查课程代码,该数组包含课程代码和课程描述。
您要求用户输入类代码,但随后使用该代码检查其是否存在于包含代码和描述的数组中。这个 contains 在里面 List (集合)与 contains 在里面 String .
我已经稍微修改了你的代码,所以你可能会得到想要的结果。

  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. public class SOQuestion {
  5. private static void check(String[] arr, String toCheckValue){
  6. List courses = Arrays.asList(arr);
  7. boolean test=courses.contains(toCheckValue);;
  8. System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
  9. }
  10. public static void main(String[] args) {
  11. String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
  12. "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
  13. "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
  14. String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };
  15. Scanner input1 = new Scanner(System.in);
  16. System.out.print("\nEnter the number of subjects you wish to enroll: ");
  17. int number_subjects1 = input1.nextInt();
  18. String[] subjects1 = new String[number_subjects1];
  19. // else statement when user exceeds the number of possible number of subjects
  20. if (number_subjects1 <= 8) {
  21. for (int counter = 0; counter < number_subjects1; counter++) {
  22. System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
  23. + (counter + 1));
  24. subjects1[counter] = input1.next();
  25. }
  26. String toCheckValue = subjects1[0];
  27. System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
  28. check(class_codes, toCheckValue);
  29. System.out.println("\nPlease check if these are your preferred subjects:");
  30. for (int counter = 0; counter < number_subjects1; counter++) {
  31. System.out.println(subjects1[counter]);
  32. }
  33. System.out.println("**********************************\n" + "\tNothing Follows");
  34. System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
  35. Scanner character = new Scanner(System.in);
  36. String answer_1subjectserrors = character.nextLine();
  37. System.out.println(
  38. answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
  39. }
  40. }
  41. }

调试时,始终尝试将语句分解为步骤,以便知道错误所在。例如,而不是 boolean test=Arrays.asList(arr).contains(toCheckValue); 将其分解为如下两个步骤:

  1. List courses = Arrays.asList(arr);
  2. boolean test=courses.contains(toCheckValue);

这样,您将更容易检查问题。
第二个要求是始终查看api。浏览api,看看您正在使用的方法,以便更好地理解它。例如,如果您正在使用 contains 方法 List 然后在此处查找api:https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/list.html#contains(java.lang.object)
当然,因为这是oracle的java,所以解释不精确&不直接,但通常是有帮助的。
我建议使用与普通阵列不同的数据结构。因为您已经在使用 List 为什么不使用另一个集合数据结构,如 HashMap ?

展开查看全部
vwkv1x7d

vwkv1x7d2#

最初的海报可能希望看到一个稍微经过重构和清理的代码版本&尝试找出如何检查所有课程,因为这是他的下一个问题。我认为,这一点应该随着代码的重构变得更加明显:

  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. public class SOQuestion {
  5. public static String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
  6. "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
  7. "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
  8. public static String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };
  9. public static void main(String[] args) {
  10. Scanner input = new Scanner(System.in);
  11. int desired_number_of_subjects = input_desired_number_of_subjects(input);
  12. String[] desired_subjects = enter_subjects(desired_number_of_subjects, input);
  13. String toCheckValue = desired_subjects[0];
  14. System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
  15. check(class_codes, toCheckValue);
  16. pls_confirm_desired_subjects(desired_number_of_subjects, desired_subjects);
  17. System.out.println("**********************************\n" + "\tNothing Follows");
  18. System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
  19. Scanner character = new Scanner(System.in);
  20. String answer_1subjectserrors = character.nextLine();
  21. System.out.println(
  22. answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
  23. }
  24. private static int input_desired_number_of_subjects(Scanner input) {
  25. System.out.print("\nEnter the number of subjects you wish to enroll: ");
  26. int take_number_of_subjects = input.nextInt();
  27. // TODO: else statement when user exceeds the number of possible number of subjects
  28. return take_number_of_subjects;
  29. }
  30. private static String[] enter_subjects(int desired_subjects_count , Scanner input_desired_subjects) {
  31. String[] subjects_totake = new String[desired_subjects_count];
  32. if (desired_subjects_count <= 8) {
  33. for (int counter = 0; counter < desired_subjects_count; counter++) {
  34. System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
  35. + (counter + 1));
  36. subjects_totake[counter] = input_desired_subjects.next();
  37. }
  38. }
  39. return subjects_totake;
  40. }
  41. private static void check(String[] arr, String toCheckValue){
  42. List courses = Arrays.asList(arr);
  43. boolean test=courses.contains(toCheckValue);
  44. System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
  45. }
  46. private static void pls_confirm_desired_subjects(int take_number_of_subjects, String[] take_subjects) {
  47. System.out.println("\nPlease check if these are your preferred subjects:");
  48. for (int counter = 0; counter < take_number_of_subjects; counter++) {
  49. System.out.println(take_subjects[counter]);
  50. }
  51. }
  52. }

我将很快编辑上面的内容,但提示是:您可以使用 for 循环以检查输入的 desired_subjects 数组并对每个主题进行检查,也许?
以下检查所有课程(尽管我不会这样检查课程)

  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. public class SOQuestion {
  5. public static String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
  6. "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
  7. "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
  8. public static String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };
  9. public static void main(String[] args) {
  10. Scanner input = new Scanner(System.in);
  11. int desired_number_of_subjects = input_desired_number_of_subjects(input);
  12. String[] desired_subjects = enter_subjects(desired_number_of_subjects, input);
  13. check_all_desired_subjects(desired_subjects);
  14. pls_confirm_desired_subjects(desired_number_of_subjects, desired_subjects);
  15. System.out.println("**********************************\n" + "\tNothing Follows");
  16. System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
  17. Scanner character = new Scanner(System.in);
  18. String answer_1subjectserrors = character.nextLine();
  19. System.out.println(
  20. answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
  21. }
  22. private static int input_desired_number_of_subjects(Scanner input) {
  23. System.out.print("\nEnter the number of subjects you wish to enroll: ");
  24. int take_number_of_subjects = input.nextInt();
  25. // TODO: else statement when user exceeds the number of possible number of subjects
  26. return take_number_of_subjects;
  27. }
  28. private static String[] enter_subjects(int desired_subjects_count , Scanner input_desired_subjects) {
  29. String[] subjects_totake = new String[desired_subjects_count];
  30. if (desired_subjects_count <= 8) {
  31. for (int counter = 0; counter < desired_subjects_count; counter++) {
  32. System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
  33. + (counter + 1));
  34. subjects_totake[counter] = input_desired_subjects.next();
  35. }
  36. }
  37. return subjects_totake;
  38. }
  39. private static void check_all_desired_subjects(String[] desired_subjects) {
  40. System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
  41. for (String subject_code_to_check:desired_subjects ) {
  42. check(class_codes, subject_code_to_check);
  43. }
  44. }
  45. private static void check(String[] arr, String toCheckValue){
  46. List courses = Arrays.asList(arr);
  47. boolean test=courses.contains(toCheckValue);
  48. System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
  49. }
  50. private static void pls_confirm_desired_subjects(int take_number_of_subjects, String[] take_subjects) {
  51. System.out.println("\nPlease check if these are your preferred subjects:");
  52. for (int counter = 0; counter < take_number_of_subjects; counter++) {
  53. System.out.println(take_subjects[counter]);
  54. }
  55. }
  56. }
展开查看全部

相关问题