嘿,我在打印时在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();
}
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] 删除。
我来说两句