如果两个名称相同,则程序将错误

时间:2018-11-21 09:32:38

标签: java

Error My Codes

我制作了一个询问全名,地址等的程序,我希望该程序检测两个全名是否相同。我已经有了if else语句来检测全名是否相同,但是我在返回部分不断出错。我想再次返回名称输入过程,但是程序继续转到我想要的地址输入。

示例输出

1。您的名字:杰里科

1。您的中间名:Manarang

1。您的姓氏:Navarro

输入barangay .... etc

2。您的名字:杰里科

2。您的中间名:Manarang

2。您的姓氏:Navarro

(错误,请重试)

2。您的名字:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("How many names do you like to enter? ");
    int user = input.nextInt();
    String DATA[][][] = new String[user][4][3];

    for(int x=0;x<user;x++){
        System.out.print("\n" + (x+1) +".Enter your first name: ");
        DATA[x][0][0] = new Scanner(System.in).nextLine();
        System.out.print((x+1) +".Enter your midle name: ");
        DATA[x][0][1] = new Scanner(System.in).nextLine();
        System.out.print((x+1) +".Enter your last name: ");
        DATA[x][0][2] = new Scanner(System.in).nextLine();
        for(int y=0;y<user;y++){
                if(x == y){
                }
                else if(DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])){
                    System.out.println("Ops! Your name is already inputed.");
                    System.out.println("Please Try again.");
                    x-=1;
                    break;

            }
        }

        System.out.print("\n" +(x+1) +".Enter your barangay: ");
        DATA[x][1][0] = new Scanner(System.in).nextLine();
        System.out.print((x+1) +".Enter your city: ");
        DATA[x][1][1] = new Scanner(System.in).nextLine();
        System.out.print((x+1) +".Enter your province: ");
        DATA[x][1][2] = new Scanner(System.in).nextLine();
        System.out.print("\n" +(x+1) +".Enter your mailing address: ");
        DATA[x][2][0] = new Scanner(System.in).nextLine();
        System.out.print((x+1) +".Enter your contact number: ");
        DATA[x][2][1] = new Scanner(System.in).nextLine();
        System.out.print((x+1) +".Enter your email address: ");
        DATA[x][2][2] = new Scanner(System.in).nextLine();
        System.out.print("\n" +(x+1) +".Enter your elementary school: ");
        DATA[x][3][0] = new Scanner(System.in).nextLine();
        System.out.print((x+1) +".Enter your secondary school: ");
        DATA[x][3][1] = new Scanner(System.in).nextLine();
        System.out.print((x+1) +".Enter your tertiary: ");
        DATA[x][3][2] = new Scanner(System.in).nextLine();


    }
}

}

2 个答案:

答案 0 :(得分:1)

此代码有很多问题。这根本不是实现此目的的好方法。但是,由于您是新手,并且仍然在使用Java,因此现在您可以对代码进行这样的更改,并使它正常工作。

import java.util.Scanner;

public class Main {

    static boolean flag;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("How many names do you like to enter? ");
        int user = input.nextInt();
        String DATA[][][] = new String[user][4][3];

        for (int x = 0; x < user; x++) {
            flag = false;
            System.out.print("\n" + (x + 1) + ".Enter your first name: ");
            DATA[x][0][0] = new Scanner(System.in).nextLine();
            System.out.print((x + 1) + ".Enter your midle name: ");
            DATA[x][0][1] = new Scanner(System.in).nextLine();
            System.out.print((x + 1) + ".Enter your last name: ");
            DATA[x][0][2] = new Scanner(System.in).nextLine();
            for (int y = 0; y < user; y++) {
                if (x == y) {
                } else if (DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])) {
                    System.out.println("Ops! Your name is already inputed.");
                    System.out.println("Please Try again.");
                    x -= 1;
                    flag = true;
                    break;
                }
            }

            if (flag){
                continue;
            }

            System.out.print("\n" + (x + 1) + ".Enter your barangay: ");
            DATA[x][1][0] = new Scanner(System.in).nextLine();
            System.out.print((x + 1) + ".Enter your city: ");
            DATA[x][1][1] = new Scanner(System.in).nextLine();
            System.out.print((x + 1) + ".Enter your province: ");
            DATA[x][1][2] = new Scanner(System.in).nextLine();
            System.out.print("\n" + (x + 1) + ".Enter your mailing address: ");
            DATA[x][2][0] = new Scanner(System.in).nextLine();
            System.out.print((x + 1) + ".Enter your contact number: ");
            DATA[x][2][1] = new Scanner(System.in).nextLine();
            System.out.print((x + 1) + ".Enter your email address: ");
            DATA[x][2][2] = new Scanner(System.in).nextLine();
            System.out.print("\n" + (x + 1) + ".Enter your elementary school: ");
            DATA[x][3][0] = new Scanner(System.in).nextLine();
            System.out.print((x + 1) + ".Enter your secondary school: ");
            DATA[x][3][1] = new Scanner(System.in).nextLine();
            System.out.print((x + 1) + ".Enter your tertiary: ");
            DATA[x][3][2] = new Scanner(System.in).nextLine();


        }
    }
}

再次使用它来破解此代码。这不是正确的方法。您必须重构整个代码才能正确完成它。 :)

答案 1 :(得分:1)

同意。对我来说,最好提供替代方法,因为阅读您的解决方案确实很困难。这是我的报价。我认为这可能对您更清楚。 P.S。。在超过15年的发展过程中,我遇到了多次使用2D以上阵列的问题。不要使用class而不是3D ...数组。

private static final class User {

    private final String firstName;
    private final String middleName;
    private final String lastName;

    private String barangay;
    private String city;
    private String province;
    private String mailingAddress;
    private String contactNumber;
    private String email;
    private String elementarySchool;
    private String secondarySchool;
    private String tertiary;

    public User(String firstName, String middleName, String lastName) {
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!(obj instanceof User))
            return false;
        User user = (User)obj;
        return Objects.equals(firstName, user.firstName) && Objects.equals(middleName, user.middleName)
                && Objects.equals(lastName, user.lastName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(firstName, middleName, lastName);
    }
}

private static User getUser(Scanner scan, int i) {
    System.out.print("\n" + i + ". Enter your first name: ");
    String firstName = scan.next();

    System.out.print(i + ". Enter your middle name: ");
    String middleName = scan.next();

    System.out.print(i + ". Enter your last name: ");
    String lastName = scan.next();

    return new User(firstName, middleName, lastName);
}

private static User getUserAdditionalInfo(User user, Scanner scan, int i) {
    System.out.print("\n" + i + ". Enter your barangay: ");
    user.barangay = scan.nextLine();

    System.out.print(i + ". Enter your city: ");
    user.city = scan.nextLine();

    System.out.print(i + ". Enter your province: ");
    user.province = scan.nextLine();

    System.out.print("\n" + i + ". Enter your mailing address: ");
    user.mailingAddress = scan.nextLine();

    System.out.print(i + ". Enter your contact number: ");
    user.contactNumber = scan.nextLine();

    System.out.print(i + ". Enter your email address: ");
    user.email = scan.nextLine();

    System.out.print("\n" + i + ". Enter your elementary school: ");
    user.elementarySchool = scan.nextLine();

    System.out.print(i + ". Enter your secondary school: ");
    user.secondarySchool = scan.nextLine();

    System.out.print((i + 1) + ".Enter your tertiary: ");
    user.tertiary = scan.nextLine();

    return user;
}

private static Set<User> getUsers() {
    try (Scanner scan = new Scanner(System.in)) {
        System.out.print("How many names do you like to enter? ");
        Set<User> users = new LinkedHashSet<>();

        for (int i = 1, total = scan.nextInt(); i <= total; ) {
            User user = getUser(scan, i);

            if (users.contains(user)) {
                System.out.println("Ops! Your name is already inputed.");
                System.out.println("Please Try again.");
            } else {
                users.add(getUserAdditionalInfo(user, scan, i));
                i++;
            }
        }

        return Collections.unmodifiableSet(users);
    }
}

public static void main(String[] args) {
    Set<User> users = getUsers();
}