java - java中的循环处理

标签 java eclipse loops

嘿,我在打印时遇到了java问题...我有一个程序,我需要在循环中打印一些值。在循环的每次重复中,现在都会形成一个新值,我希望当循环结束时第一次重复的第一个值应该打印,然后打印第二个,然后依此类推......但在我的情况下,它只打印最后一次执行的值enter image description here

这里我上传了我的输出图片,你可以清楚地看到只打印了最后一个循环的值 但我希望在第一行中应该有第一个循环的值,在第二行中应该有第二个循环的值,在第三行中应该有第三个循环的值。

这是我的代码

public class ManufactureClass {

        private static void outputtable() {


                    System.out.println(
                            "\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                    System.out.println("| No. of Order | No. of Units | Time Consumed by WA | Time Consumed by WB | Time Consumed by WH | Effective Units | Defective Units | Total Time Consumed By this Order | No. of Packages |");
                    System.out.println(
                            "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                    for(int i=1;i<=total_orders;i++) {
                    long sum=Long.sum((time_consumed_by_Work_Station_H_in_thread_sleep_for_units_greater_than_30/1000), (time_consumed_by_Work_Station_B_in_thread_sleep/1000));
                    long sum1=Long.sum((time_consumed_by_Work_Station_A_in_integer/1000), sum);
                    total_time_consumed_by_order=Long.toString(sum1)+" seconds";
                    if(units_in_an_order<=30) {
                        System.out.println("|       "+loop_for_orders+"       |      "+number_of_units_in_order+"      | "+actual_time_by_Work_Station_A_in_seconds+" | "+actual_time_by_Work_Station_B_in_seconds+" | "+actual_time_by_Work_Station_H_in_seconds_for_units_less_than_30+" | "+effective_units+" | "+defective_units+" | "+total_time_consumed_by_order+" | "+number_of_packages_for_units_less_than_30+" | ");
                        System.out.println(
                                "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                        }else {

                        System.out.println("|      "+loop_for_orders+"       |      "+number_of_units_in_order+"      | "+actual_time_by_Work_Station_A_in_seconds+" | "+actual_time_by_Work_Station_B_in_seconds+" | "+actual_time_by_Work_Station_H_in_seconds_for_units_greater_than_30+" | "+effective_units+" | "+defective_units+" | "+total_time_consumed_by_order+" | "+number_of_packages_for_units_greater_than_30+" | ");
                        System.out.println(
                                "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                        }
            }
        }
        public static void main(String[] args) throws InterruptedException {

            display_header();
            display_msg();

            for(loop_for_orders=1;loop_for_orders<=total_orders;loop_for_orders++) {
                Random rand = new Random(); 
                units_in_an_order = rand.nextInt(200);
                number_of_units_in_order=Integer.toString(units_in_an_order);
                int a=10;
                int b=100;
                defective_units =(float) (0.01*units_in_an_order);
                effective_units=units_in_an_order-defective_units;
            int zero1=0;
            int zero2=0;
            time_consumed_by_Work_Station_A = "" + units_in_an_order + zero1+zero2;
            time_consumed_by_Work_Station_A_in_integer = Long.parseLong(time_consumed_by_Work_Station_A);
            interval = units_in_an_order;
        Manufactured_units_by_work_station_A();
        permission();




        }
}


    outputtable();
}

最佳答案

好吧,在第二次看到时,正如上面评论中提到的,您在每次迭代中都写下了您的值。您可能需要 ArrayList 或集合来存储每次迭代中收到的值。

做这样的事情。

    public class ManufactureClass {

      static ArrayList myArrayList = new ArrayList();

     function  public static void main(String[] args) throws InterruptedException {

      //(...)

        for(loop_for_orders=1;loop_for_orders<=total_orders;loop_for_orders++) {
              myArrayList.add(yourOutputString);
         }
    }

在你的 Output-Funktion 中,你只需要遍历 ArrayList,甚至可以在静态上下文中设置这个函数 - 但这样的结构绝对是正确的方法。

编辑:如果您想生成行输出,那就有点复杂了,但请记住 - key 是正确的 DataStructure 和处理它的适当方法,这对您有用。 :)

编辑2:所以如果我们在同一个类的第二个函数中遍历数组List,它看起来像这样:

    public void printMyOutputLine(){
            for(j=0;j<this.myArrayList.size();j++)
            {
               System.out.println(this.myArrayList[j]);
             }

     }

这真的很基本 - 唯一的问题是通过使用适当的 DataStructure 来获取列表中所有数据的“技巧” - 在本例中我猜是 ArrayList,但可能你更愿意使用集合或 StringArray。你会明白的 - 描述的方法将引导你走向正确的方向。

关于java - java中的循环处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892319/

相关文章:

python - 在 Python 中调用其他函数来制作子图

C++:在数组中寻找最大值的问题

java - 如何编写java程序读取5个数字并计算有多少个数字的值是0-9

eclipse - 为什么 Eclipse 看不到我的 Samsung Galaxy 手机,即使我可以使用驱动器盘符传输文件?

Eclipse -- 手动安装插件

java - 如何将两种不同的字体应用到TextView?

java - 如何使用Socket将图片从PC发送到Android?

eclipse - 从 Eclipse 内部将本地 Git 存储库推送到远程 Google Cloud Repository?

java - Twilio TaskRouter 工作人员 session

java - 如何在 Ubuntu Server 14 中安装 Java JNA?