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

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

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

private static void check(String[] arr, String toCheckValue){
boolean test=Arrays.asList(arr).contains(toCheckValue);

System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);

}

public static void main(String[] args){
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"};

Scanner input1=new Scanner(System.in);
                        System.out.print("\nEnter the number of subjects you wish to enroll: ");
                        int number_subjects1=input1.nextInt();
                        String []subjects1=new String[number_subjects1];

                        //else statement when user exceeds the number of possible number of subjects

                        if(number_subjects1<=8){
                            for(int counter=0; counter<number_subjects1; counter++){
                                System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): " + (counter+1));
                                subjects1[counter]=input1.next();

                            }
                            String toCheckValue=subjects1[0];
                            System.out.println("Array: " +Arrays.toString(arr));
                                check(arr, toCheckValue);

                            System.out.println("\nPlease check if these are your preferred subjects:");
                            for(int counter=0; counter<number_subjects1; counter++){ 
                                System.out.println(subjects1[counter]);
                            }System.out.println("**********************************\n" + "\tNothing Follows");

                            System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
                            Scanner character=new Scanner(System.in);
                            String answer_1subjectserrors=character.nextLine();
                            System.out.println(answer_1subjectserrors + "Based on your answer, you need to refresh thae page and try again.");
                        }

} }

i1icjdpr

i1icjdpr1#

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

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class SOQuestion {

    private static void check(String[] arr, String toCheckValue){
        List courses = Arrays.asList(arr);

        boolean test=courses.contains(toCheckValue);;

        System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
    }

    public static void main(String[] args) {
        String class_codes_and_descriptions[] = { "(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" };
        String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };
        Scanner input1 = new Scanner(System.in);
        System.out.print("\nEnter the number of subjects you wish to enroll: ");
        int number_subjects1 = input1.nextInt();
        String[] subjects1 = new String[number_subjects1];

        // else statement when user exceeds the number of possible number of subjects

        if (number_subjects1 <= 8) {
            for (int counter = 0; counter < number_subjects1; counter++) {
                System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
                        + (counter + 1));
                subjects1[counter] = input1.next();

            }
            String toCheckValue = subjects1[0];
            System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
            check(class_codes, toCheckValue);

            System.out.println("\nPlease check if these are your preferred subjects:");
            for (int counter = 0; counter < number_subjects1; counter++) {
                System.out.println(subjects1[counter]);
            }
            System.out.println("**********************************\n" + "\tNothing Follows");

            System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
            Scanner character = new Scanner(System.in);
            String answer_1subjectserrors = character.nextLine();
            System.out.println(
                    answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
        }

    }

}

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

List courses = Arrays.asList(arr);

        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#

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

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class SOQuestion {

    public static String class_codes_and_descriptions[] = { "(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" };
    public static String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int desired_number_of_subjects = input_desired_number_of_subjects(input);

        String[] desired_subjects = enter_subjects(desired_number_of_subjects, input);

            String toCheckValue = desired_subjects[0];
            System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
            check(class_codes, toCheckValue);

            pls_confirm_desired_subjects(desired_number_of_subjects, desired_subjects);
            System.out.println("**********************************\n" + "\tNothing Follows");

            System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
            Scanner character = new Scanner(System.in);
            String answer_1subjectserrors = character.nextLine();
            System.out.println(
                    answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
        }

    private static int input_desired_number_of_subjects(Scanner input) {
        System.out.print("\nEnter the number of subjects you wish to enroll: ");
        int take_number_of_subjects = input.nextInt();
        // TODO: else statement when user exceeds the number of possible number of subjects
        return take_number_of_subjects;
    }

    private static String[] enter_subjects(int desired_subjects_count , Scanner input_desired_subjects) {
        String[] subjects_totake = new String[desired_subjects_count];
        if (desired_subjects_count <= 8) {
            for (int counter = 0; counter < desired_subjects_count; counter++) {
                System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
                        + (counter + 1));
                subjects_totake[counter] = input_desired_subjects.next();

            }
        }
        return subjects_totake;
    }

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

    private static void pls_confirm_desired_subjects(int take_number_of_subjects, String[] take_subjects) {
        System.out.println("\nPlease check if these are your preferred subjects:");
        for (int counter = 0; counter < take_number_of_subjects; counter++) {
            System.out.println(take_subjects[counter]);
        }
    }       
}

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

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class SOQuestion {

    public static String class_codes_and_descriptions[] = { "(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" };
    public static String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int desired_number_of_subjects = input_desired_number_of_subjects(input);

        String[] desired_subjects = enter_subjects(desired_number_of_subjects, input);

            check_all_desired_subjects(desired_subjects);

            pls_confirm_desired_subjects(desired_number_of_subjects, desired_subjects);
            System.out.println("**********************************\n" + "\tNothing Follows");

            System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
            Scanner character = new Scanner(System.in);
            String answer_1subjectserrors = character.nextLine();
            System.out.println(
                    answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
        }

    private static int input_desired_number_of_subjects(Scanner input) {
        System.out.print("\nEnter the number of subjects you wish to enroll: ");
        int take_number_of_subjects = input.nextInt();
        // TODO: else statement when user exceeds the number of possible number of subjects
        return take_number_of_subjects;
    }

    private static String[] enter_subjects(int desired_subjects_count , Scanner input_desired_subjects) {
        String[] subjects_totake = new String[desired_subjects_count];
        if (desired_subjects_count <= 8) {
            for (int counter = 0; counter < desired_subjects_count; counter++) {
                System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
                        + (counter + 1));
                subjects_totake[counter] = input_desired_subjects.next();

            }
        }
        return subjects_totake;
    }

    private static void check_all_desired_subjects(String[] desired_subjects) {
        System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
        for (String subject_code_to_check:desired_subjects ) {
            check(class_codes, subject_code_to_check);      
        }
    }

    private static void check(String[] arr, String toCheckValue){
        List courses = Arrays.asList(arr);
        boolean test=courses.contains(toCheckValue);
        System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
    }
    private static void pls_confirm_desired_subjects(int take_number_of_subjects, String[] take_subjects) {
        System.out.println("\nPlease check if these are your preferred subjects:");
        for (int counter = 0; counter < take_number_of_subjects; counter++) {
            System.out.println(take_subjects[counter]);
        }
    }

}

相关问题