Java中的循环处理

米哈尔·乌萨马(Mehar usama)

嘿,我在打印时在Java中有问题...我有一个程序需要在其中打印循环中的某些值。在循环的每个重复中,现在都形成一个新值,我希望在循环结束时应该先打印第一个重复的第一个值,然后再打印第二个,依此类推……但是在我的情况下,它仅打印上次执行的值在此处输入图片说明

在这里,我上传了输出的图片,您可以清楚地看到仅打印了最后一个循环的值,但是我想在第一行中应该在第二行中包含第一循环的值,在第二行和第三行中应该包含值应该有第三个循环的值。

这是我的代码

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();
}
mZed

Well on the second sight and as was mentioned above in the comments, you owerwrite your values in each iteration. You may need wether an ArrayList or a collection to store the values you receive in every iteration.

DO something like this.

    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);
         }
    }

In your Output-Funktion you just have to go through the ArrayList and maybe even set this function in a static context - but a structure like that is definitley the way to go.

Edit: Well it's a bit more complicated if you want to generate an output in lines, but remeber - key is the right DataStructure and an appropriate way to handle it, that works for you. :)

Edit 2: So in case we go through the array List in a second function in the same class it looks like this:

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

     }

这真的很基础-唯一的问题是通过使用适当的DataStructure来“获取”列表中所有数据的“技巧”-在这种情况下,我猜是ArrayList,但可能是您更希望使用集合或StringArray 。您会发现-精简的方法将带您朝正确的方向前进。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Java main方法中的循环和异常处理

来自分类Dev

在Python中处理for循环的索引

来自分类Dev

处理CDI中的循环依赖

来自分类Dev

Brainf * ck中的处理循环

来自分类Dev

并行处理python中的循环

来自分类Dev

并行处理python中的循环

来自分类Dev

python中的多处理(从for循环转到for循环的多处理)

来自分类Dev

对于批处理文件中的循环

来自分类Dev

在R中循环处理大量数据

来自分类Dev

批处理中for循环中的空白

来自分类Dev

处理JSON.net中的引用循环

来自分类Dev

在Matlab中无需for循环即可处理矩阵

来自分类Dev

批处理文件中的for循环问题

来自分类Dev

Julia中嵌套循环的并行处理

来自分类Dev

在std :: variant中处理循环依赖

来自分类Dev

我的for循环函数在R中的并行处理

来自分类Dev

批处理文件中的For循环

来自分类Dev

在Matlab中无需for循环即可处理矩阵

来自分类Dev

批处理脚本中的循环通配符匹配

来自分类Dev

批处理中for循环中的空白

来自分类Dev

如何处理NodeJS中的循环

来自分类Dev

如何处理C ++中的循环组合?

来自分类Dev

处理异步onload的函数中的无限循环

来自分类Dev

OpenMP中的调度程序对循环变量的处理

来自分类Dev

在批处理中的FOR循环末尾使用if条件

来自分类Dev

如何处理 Selenium 中的嵌套循环

来自分类Dev

Django模板中的For循环未处理

来自分类Dev

Java中的视频处理

来自分类Dev

Java中的Chracter处理