简体   繁体   English

比较数组列表中的两个元素

[英]Comparing two elements in an Array List

The exercise is, to create an arraylist for a class, where a User can enter "Guestnumber" + "Guestname" + "Guestemail". 练习是为一个类创建一个数组列表,用户可以在其中输入“来宾编号” +“来宾名称” +“来宾邮件”。

Everything works fine expect if I create a new Guest with the Guestnumber 1 for example, the program should tell me "Guestnumber is existing, please choose a new one". 举例来说,如果我用Guestnumber 1创建一个新的Guest,那么一切都会很好,程序应该告诉我“ Guestnumber存在,请选择一个新的Guestnumber”。 I trief and came to this, not working conclusion: 我试着得出这个结论,而不是有效的结论:

public void gastAnlegen() {
    boolean pruef1 = true;
    boolean pruef2 = true;
    boolean pruef3 = true;
    boolean gastNummerPruefung = false; 
    while(gastNummerPruefung != true) {
        gastNummerPruefung = true;


    while (pruef1 != false) {
        System.out.println("Bitte Gästenummer eingeben: ");
        String gNr = input.next();
        try {
            gast = Integer.parseInt(gNr);
        } catch (NumberFormatException e) {
            continue;
        }
        pruef1 = false;
        for (Gast test : verwaltungG) {
            if (gast == test.getgNr()) {
                System.out.println("Diese Nummer ist leider schon vergeben");
                gastNummerPruefung = false;
            }
        }

        }
    }

The Problem now is that it wont go further than telling me to enter the Guestnumber ("Bitte Gästenummer eingeben") over and over again, so it kinda stops there. 现在的问题是,它不会比告诉我一遍又一遍地输入Guestnumber(“ BitteGästenummereingeben”)要走得更远,所以它就停在那里了。

I added The "bigger" while loop to test if the number is existing. 我添加了“更大”的while循环来测试数字是否存在。

Where is the error? 错误在哪里?

Gast ist the Class for get, set Gast ist Class获取,设置

gastNmmerPruefung boolean for the loop gastNmmerPruefung布尔值用于循环

before i added the big loop, it worked fine. 在添加大循环之前,它工作正常。

Edit: Okay i found one problem, I had to change "gastNmmerPruefung to true after the big while, but it still doesnt let me enter a new number after it says that the number already exists. 编辑:好的,我发现了一个问题,我不得不在很长时间之后将“ gastNmmerPruefung”更改为true,但是在说该数字已经存在之后,它仍然不允许我输入新的数字。

You also need to change your pruef1 back to true if you find that the number is already taken to make it go inside the inner while loop: 如果您发现已经使用该数字使它进入内部while循环,则还需要将pruef1更改为true:

    for (Gast test : verwaltungG) {
        if (gast == test.getgNr()) {
            System.out.println("Diese Nummer ist leider schon vergeben");
            gastNummerPruefung = false;
            pruef1 = true;
        }
    }

You really only need one boolean to check. 您实际上只需要检查一个布尔值。 Get the number from the user, check it, if it's found then ask again, otherwise your done. 从用户那里获取电话号码,检查是否找到,然后再次询问,否则请完成。

Try this: 尝试这个:

while(!gastNummerPruefung)
{
    System.out.println("Bitte Gästenummer eingeben: ");
    String gNr = input.next();
    try
    {
        gast = Integer.parseInt(gNr);
    }
    catch (NumberFormatException e)
    {
        continue;
    }

    for (Gast test : verwaltungG)
    {
        if (gast == test.getgNr())
        {
            System.out.println("Diese Nummer ist leider schon vergeben");
            gastNummerPruefung = false;
        }
        else
            gastNummerPruefung = true;
    }
}

You should check first inner loop.. Try this. 您应该检查第一个内部循环。 You have to faced problem with second inner loop 您必须面对第二个内循环的问题

while(gastNummerPruefung != true) 
gastNummerPruefung = false;
while (pruef1 != false) {
    System.out.println("Bitte Gästenummer eingeben: ");
    String gNr = input.next();
    try {
        gast = Integer.parseInt(gNr);
    } catch (NumberFormatException e) {
        continue;
    }
    pruef1 = true;  // change this in your code
    for (Gast test : verwaltungG) {
        if (gast == test.getgNr()) {
            System.out.println("Diese Nummer ist leider schon vergeben");
            gastNummerPruefung = false;
        }
    }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM