【问题标题】:Cannot Store Data into ArrayLists?无法将数据存储到 ArrayLists 中?
【发布时间】:2011-12-09 12:47:42
【问题描述】:

感谢所有帮助人员,但现在问题的性质已经改变,使用 Patrick 的建议,下面的循环正在运行,但它似乎没有将输入存储到相应的数组中,数据不断替换为 ArrayLists 而不是转到对 ArrayList 的下一个位置有什么建议吗?

  import java.util.ArrayList;
import java.util.Scanner;
public class Arrray {
    public static void main(String [] args){

        ArrayList<String> names;
        ArrayList<String> addr;


        do {
            names = new ArrayList<String>();
            addr = new ArrayList<String>();
            Scanner userInput = new Scanner(System.in);
            System.out.println("Name and Adreess are: " + names.size() + "**"
                    + addr.size());
            System.out.println("please Enter Your Name :");
            names.add(userInput.next());
            System.out.println("please enter your Address  :");
            addr.add(userInput.next());

            System.out.println("Do you want to add another entry? :(y/n)" );
            String ans =userInput.next();  // get the value from the user using scanner class
            if(ans.equals("n") || ans.equals("N"))
                break;

        } while (true);
        int n = names.size();
        int a = addr.size();
        for(int i =0; i<n && i<a; i++ )
            System.out.println("Name and address are as below:  "+ names.get(i)+"**"+ addr.get(i));




    }

}

【问题讨论】:

  • 你试过什么? (false) 作为循环条件将永远不会重新运行您的循环。您实际上在哪里尝试获取(y/n)选择的用户输入?很高兴看到在这里发布之前至少有一点努力
  • 并且您正在为每个用户重新创建列表,因此无论如何它都不起作用。
  • 对于 java 的人非常非常新,就像上周刚开始的时候一样,所以请原谅无知,不要只是尝试学习使用数组列表,这不是功课。

标签: java loops arraylist do-while


【解决方案1】:

while(true)break statement 结合使用:

do {
    if(input.next() == 'n'){
        break;
    }
} while(true);

【讨论】:

    【解决方案2】:

    从用户那里获取价值,如果用户输入 n 则中断,否则什么都没有

    System.out.println("Do you want to add another entry? :(y/n)" );
    String ans = .... // get the value from the user using scanner class
    if(ans.equalsIgnoreCase("n"))
         break;
    

    【讨论】:

    • 感谢帕特里克的回复,感谢您的帮助。它再次运行循环,但它似乎没有将数据存储到数组中,它一直用新条目替换它我想要得到的是填充两个 ArrayList 有什么建议吗?
    【解决方案3】:

    尝试捕捉该用户的输入

    System.out.println("Do you want to add another entry? :(y/n)");
    

    并在此期间使用该信息。

    【讨论】:

      【解决方案4】:

      你必须这样做:

      String choice = "";
      do {
          .
          .
          .
          .
          System.out.println("Do you want to add another entry? :(y/n)" );
          choice = userInput.next();
      } while (!(choice.equals("n") || choice.equals("N")));
      

      线

      choice = userInput.next();
      

      将读取用户输入,并与Stringequals 方法进行比较输入。循环将继续,直到选择 Nn

      【讨论】:

        【解决方案5】:

        导入 java.util.ArrayList; 导入 java.util.Scanner;

        公共类数组{

        public static void main(String[] args) {
        
            ArrayList<String> name = new ArrayList<String>();
            ArrayList<Integer> phone = new ArrayList<Integer>();
            Scanner scanner = new Scanner(System.in);
            String answer = "";
            do {
                System.out.println("Please enter your name: ");
                name.add(scanner.next());
                System.out.println("Please enter your number: ");
                phone.add(scanner.nextInt());
                System.out.println("Do you want to add a directory y/n?");
                answer = scanner.next();
            } while (answer.equals("y") || answer.equals("Y"));
            if (answer.equals("y") || answer.equals("Y")); //want it to go back to start another direcotry here
            else {
                System.out.println("Thanks for adding to the directory");
                for (int i = 0; i < name.size(); i++) {
                    System.out.print(name.get(i) + "\t");
                    System.out.print(phone.get(i));
                    System.out.println("");
                }
            }
        }
        

        }

        【讨论】:

          猜你喜欢
          • 2013-03-13
          • 1970-01-01
          • 2021-12-23
          • 1970-01-01
          • 1970-01-01
          • 2020-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多