如何将扫描器的输入与构造函数中的数据进行比较?

时间:2017-03-19 19:21:39

标签: java oop

所以让我说我有以下构造函数:

    Person(String firstName, String lastName, String address, int contact, int id)
{
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.contact = contact;
    this.id = id;
}

我创建了这个来存储该对象的几个副本。

Person[] test = new Person [5];
    test[0] = new Person("Bruce", "Wayne", "Gotham City", 3143334, 163146549);
test[1] = new Person("Charles", "Xavier", "Washington D.C.", 5094170, 306784107);
test[2] = new Person("Tony", "Stark", "Los Angeles", 9263526, 714508339);
test[3] = new Person("Clark", "Kent", "Smallville", 3786018, 235219986);
test[4] = new Person("Diana", "Prince", "Amazoness Island", 3277004, 424108528);

我想将用户输入扫描仪的内容与`id

进行比较

如果它与其中一个ID匹配,则继续该程序,否则中断;

这是一种简单的方法吗? Thanks.`

4 个答案:

答案 0 :(得分:1)

您需要使用Scanner来读取输入并将其与数组中的元素进行比较,如下面的代码所示:

        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();//gives the string
        int inputInt = Integer.parseInt(input);//convert to int
        boolean matchFound = false;//flag to keep the match
        for(int i=0; i<test.length;i++) {//iterate the array
            if(test[i].getId() == inputInt) {//match found
                matchFound = true;
                break;
            }
        }

        if(!matchFound) {
            //add code here to execute if match found
        } else {
            System.out.println(" MATCH NOT FOUND");
        }

答案 1 :(得分:0)

试试这个

Scanner sc = new Scanner(System.in);
boolean flag = true;
while(flag){
    int id = sc.nextInt();
    for(Person p : test){
        if(p.id==id){
           flag = true;
           break;
        }
        flag = false;
    }
}

这基本上有一个while循环,直到flagfalse。在其中,我们遍历Person数组,并检查id。如果找到id,我们将flag设为true,否则,循环继续,检查其他Person ID。最后,如果flag为真,则表示我们找到了匹配项,因此while循环继续运行。

答案 2 :(得分:-1)

您必须将输入转换为int,然后将其转换为数组的所有元素并进行比较。

 boolean match = false;
    for(int i=0;i<test.length;i++){
        if(input == test[i].getId()){
            match = true;
            break;
        }
    }

答案 3 :(得分:-1)

    import java.util.Scanner;

public class MainClass {

   
    static Scanner in = new Scanner(System.in);



    public static void main(String args[]){

        Person[] test = new Person [5];
        test[0] = new Person("Bruce", "Wayne", "Gotham City", 3143334, 163146549);
        test[1] = new Person("Charles", "Xavier", "Washington D.C.", 5094170, 306784107);
        test[2] = new Person("Tony", "Stark", "Los Angeles", 9263526, 714508339);
        test[3] = new Person("Clark", "Kent", "Smallville", 3786018, 235219986);
        test[4] = new Person("Diana", "Prince", "Amazoness Island", 3277004, 424108528);

        System.out.println("Please enter some id: ");
        int check = in.nextInt();
        for (int i = 0; i < test.length; i++) {
            int contact = test[i].id;
            if (check == contact){
                System.out.println(contact + "is found!");
            }
        }

    }


        static class Person {
        String firstName, lastName, address;
        int id, contact;

            Person(String firstName, String lastName, String address, int contact, int id)
            {
                this.firstName = firstName;
                this.lastName = lastName;
                this.address = address;
                this.contact = contact;
                this.id = id;
            }
       }
}