簡體   English   中英

arraylist在單獨的循環方法中沒有正確打印出來? 包裝類或循環責備?

[英]arraylist isn't printing out correctly in a separate method for loop? wrapper class or loops to blame?

對於我的項目,我有一個arraylist供用戶在一個方法中輸入他們想要的任何名稱,只要它不停止。 然后在一個單獨的方法中,我剛輸入的值必須再次調用,所以我可以為每個名稱輸入更多信息。

例:

  1. 輸入名稱:
  2. 名字:鮑勃
  3. 姓名:喬
  4. name:停止這會觸發另一種提示更多信息的方法:

  5. bob:輸入年齡:輸入地址:

  6. 喬:輸入年齡:輸入地址:

    但是,現在,arraylist沒有正確打印出來,我正在重復某些名稱,而其他名稱沒有出現在第二種方法中。 此外,第二種方法中的循環不會結束。 我輸入我輸入的名稱的信息,但我不斷收到“name:”提示,並且沒有實際的arraylist值。 我懷疑我的循環出了問題,但我不知道如何修復它? 我也認為這個問題可能與我如何使用包裝器將值放入arraylist有關,但我不知道如何解決這個問題。

在第二種方法中,我嘗試交換反變量以在第二種方法中使用單獨的計數器跟蹤數組列表中的順序,但它似乎沒有什么區別。 在第一種方法中,我嘗試使用不同的boolean while循環交換循環,同時直接向上(!input.equals(“Stop”)),在前兩個選項內部的for循環計數器,如果循環,以及上面的一些組合。

這是我的代碼

Scanner console = new Scanner(System.in); private ArrayList<Directory> nameList; //i have to use object oriented programming to store values in the Directory Class public int i;

第一種方法:

private void addName() 
{
    Scanner LocalInput = new Scanner(System.in);
    Directory buffer = null;
    String ID = null;

    System.out.println("Enter Station Designations Below, Enter Stop to quit");
    boolean breaker = false;
    while(breaker ==false)
    {
        System.out.print("Name:  ");
        ID = (LocalInput.nextLine());
        if(ID.equals("Stop"))
            breaker = true;
        else
            buffer = new Directory(ID); 
            nameList.add(buffer); 
    }
}

第二種方法:

private void getInfo()
{
    Scanner LocalInput = new Scanner(System.in);

    Directory buffer;
    buffer = nameList.get(i); 
    double age; String address;


    System.out.println("Enter Temperatures below...");
    System.out.println("" + nameList.get(i));

    for (i = 0; i < nameList.size(); i++) 
    {

        System.out.println("Name: " + buffer.GetID()); //there's a getter and setter in the Directory class
        System.out.println( "Age:\t");
        age = LocalInput.nextDouble();
        System.out.print( "Address:\t");   
        address = LocalInput.nextLine();
        buffer= new Directory(age, address);
        nameList.add(buffer);
    }
}

對第一種方法的批判

我還沒有密切關注,但我強烈懷疑這是問題所在:

if(ID.equals("Stop"))
    breaker = true;
else
    buffer = new Directory(ID); 
    nameList.add(buffer); 

當ID不等於“停止”時,您似乎期望最后一個語句只執行,但實際上它總是會執行。 與(比如)Python不同,空格與Java無關。 如果您將語句視為塊,則需要大括號:

if(ID.equals("Stop"))
    breaker = true;
else {
    buffer = new Directory(ID); 
    nameList.add(buffer); 
}

就個人而言,我會在兩個部分周圍放置括號:

if (ID.equals("Stop")) {
    breaker = true;
} else {
    buffer = new Directory(ID); 
    nameList.add(buffer); 
}

......並且很可能擺脫了有些無關的buffer變量:

if (ID.equals("Stop")) {
    breaker = true;
} else {
    nameList.add(new Directory(ID));
}

還將擺脫breaker變量,並限制ID的范圍(更改其名稱,以符合常規約定),結果為:

while (true) {
    System.out.print("Name:  ");
    string id = LocalInput.nextLine();
    if (id.equals("Stop")) {
        break;
    }
    nameList.add(new Directory(ID));
}

第二種方法的批判

目前這真的很奇怪。 它根本不清楚聲明i變量的位置,或者為什么你只獲取buffer一次,或者為什么你只是添加到現有列表而不是改變現有對象。 我懷疑你真的想要:

for (Directory entry : nameList) {
    System.out.println("Name: " + entry.GetID());
    System.out.println( "Age:\t");
    double age = LocalInput.nextDouble();
    entry.setAge(age);
    System.out.print( "Address:\t");   
    String address = LocalInput.nextLine();
    entry.setAddress(address);
}

請注意,您的當前循環將一直持續到i等於nameList.size() - 但是您總是在循環中增加大小,因此您永遠不會終止。

private void getInfo()
{
    Scanner LocalInput = new Scanner(System.in);
    double age; String address;

    for (Directory name : nameList) {
    System.out.println("Name: " + name .GetID());
    System.out.println( "Age:\t");
    double age = LocalInput.nextDouble();
    name.setAge(age);
    System.out.print( "Address:\t");   
    String address = LocalInput.nextLine();
    name.setAddress(address);
}
}

get方法應該是這樣的

和添加必須 -

private void addName() 
{
    Scanner LocalInput = new Scanner(System.in);
    Directory buffer = null;
    String ID = null;

    System.out.println("Enter Station Designations Below, Enter Stop to quit");
    boolean breaker = false;
    while(breaker ==false)
    {
        System.out.print("Name:  ");
        ID = (LocalInput.nextLine());
        if(ID.equals("Stop"))
            breaker = true;
        else {
            buffer = new Directory(ID); 
            nameList.add(buffer); 
        }
    }
}

考慮使用break關鍵字,然后您不需要breaker標志和else分支:

while(true)
{
    System.out.print("Name:  ");
    ID = (LocalInput.nextLine());
    if(ID.equals("Stop"))
        break;

    buffer = new Directory(ID); 
    nameList.add(buffer); 
}

抱歉 ,已發布...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM