0

到目前为止,我已经在网上寻求帮助,但没有任何东西对我有用。这就是我现在所拥有的,你能告诉我它有什么问题吗?

导入 java.util.*;

class employeeProgram{

    public static void main(String[] args){

            for (int count = 0; count < 1; ){

                    Scanner input = new Scanner(System.in);

                    ArrayList<String> employeeNames = new ArrayList<String>();

                    String commnd;

                    print("What would you like to do? (Add, Remove, List, Exit): ");

                    commnd = input.next();

                    if ("Add".equals(commnd)){

                            print("Enter the name of the employee you'd like to add to the list: ");
                            employeeNames.add(input.next());


                    } else if ("Remove".equals(commnd)){

                            print("Enter the name of the employee you'd like to remove from the list: ");

                            employeeNames.remove(input.next());

                    } else if ("List".equals(commnd)){

                            for (int j = 0; j < employeeNames.size(); j++){

                                    println(employeeNames.get(j));
                                    println(j);

                            }


                            //println(employeeNames);

                    } else if ("Exit".equals(commnd)){

                            input.close();
                            break;

                    } else {

                            println("Invalid command.");

                    }
            }
    }
    public static void println(Object x){

            System.out.println(x);

    }
    public static void print(Object x){

            System.out.print(x);

    }

}

for 循环还没有开始,我知道很多,因为“println(j)”也没有工作。当我尝试我已经注释掉的部分时,我从输入列表中得到的只是“[]”,没有别的。我是初学者,这是我第一个使用 ArrayLists 的程序,所以提前感谢任何决定帮助我的人:)

4

5 回答 5

2

在第一个循环之外定义您的employeeNames 列表。forArrayList 会随着循环的每次迭代而重新初始化。

于 2013-10-08T16:27:58.057 回答
0

我会尝试其他方法而不是 for 循环:

尽管您在这里所拥有的在技术上可能有效,但 for...循环适用于确定性过程。在这里,您将其用作无限循环,希望稍后强制中断。这是危险的,可能会导致错误。

我会改变这个:

for (int count = 0; count < 1; ){

对此:

 bool done = false;
 while(!done) {

然后,以后,当你想爆发的时候。发出这一行:

 done = true;
于 2013-10-08T16:34:55.190 回答
0

for (int count = 0; count < 1; count++)

于 2013-10-08T16:28:17.700 回答
0

您需要在循环之外定义您的arraylist,否则每次迭代都会创建一个新的arrayList,并且之前对前一个arraylist 所做的更改会被垃圾收集。

import java.util.*;

class employeeProgram{

    public static void main(String[] args){

            ArrayList<String> employeeNames = new ArrayList<String>();

            for (int count = 0; count < 1; ){

                    Scanner input = new Scanner(System.in);



                    String commnd;

                    print("What would you like to do? (Add, Remove, List, Exit): ");

                    commnd = input.next();

                    if ("Add".equals(commnd)){

                            print("Enter the name of the employee you'd like to add to the list: ");
                            employeeNames.add(input.next());


                    } else if ("Remove".equals(commnd)){

                            print("Enter the name of the employee you'd like to remove from the list: ");

                            employeeNames.remove(input.next());

                    } else if ("List".equals(commnd)){

                            for (int j = 0; j < employeeNames.size(); j++){

                                    println(employeeNames.get(j));
                                    println(j);

                            }


                            //println(employeeNames);

                    } else if ("Exit".equals(commnd)){

                            input.close();
                            break;

                    } else {

                            println("Invalid command.");

                    }
            }
    }
    public static void println(Object x){

            System.out.println(x);

    }
    public static void print(Object x){

            System.out.print(x);

    }

}

此外,我将对这个程序进行一些基本的更改,首先 - 应该使用 for 循环,你知道你将进行多少次迭代。在您的情况下,while 循环(或者更好的是 do-while 循环)更适合。

最后创建一个内衬函数来封装 system.out.print 和 println 似乎比必要的要多。

于 2013-10-08T16:29:32.973 回答
0

每次您提出问题时,您都在创建一个新的 ArrayList。在初始循环之外创建它。

ArrayList<String> employeeNames = new ArrayList<String>();
于 2013-10-08T16:29:49.397 回答