java中的循环处理
嘿,我在打印时遇到了java问题。。。我有一个程序,我需要打印一些循环中的值。在循环的每一次重复中,都会形成一个新的值,现在我想应该是循环结束的时候,第一次重复的第一个值应该打印出来,然后再打印第二个,依此类推。。。但在我的例子中,它只是打印最后一次执行的值 在这里我上传了我的输出图片,你可以清楚地看到只有最后一个循环的值被打印出来 但是我想在第一行应该有第一个循环的值,在第二行应该有第二个循环的值,在第三行应该有第三个循环的值 这是我的密码java中的循环处理,java,eclipse,loops,Java,Eclipse,Loops,嘿,我在打印时遇到了java问题。。。我有一个程序,我需要打印一些循环中的值。在循环的每一次重复中,都会形成一个新的值,现在我想应该是循环结束的时候,第一次重复的第一个值应该打印出来,然后再打印第二个,依此类推。。。但在我的例子中,它只是打印最后一次执行的值 在这里我上传了我的输出图片,你可以清楚地看到只有最后一个循环的值被打印出来 但是我想在第一行应该有第一个循环的值,在第二行应该有第二个循环的值,在第三行应该有第三个循环的值 这是我的密码 public class ManufactureCl
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();
}
公共类制造类{
私有静态void可输出(){
System.out.println(
“\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------”);
System.out.println(|订单号|单元号| WA消耗的时间| WB消耗的时间| WH消耗的时间|有效单元|缺陷单元|本订单消耗的总时间|包装数量|);
System.out.println(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
对于(inti=1;i事实上,所描述的问题不应该那么难:)很难从您的第一个Snipplet(现在更好)中读取此内容-对我来说,所描述的问题表明,预期值没有得到正确处理,您只得到了循环的最后一次迭代
如果您的代码看起来像您提供的snipplet,我敢打赌这可能是您的缩进样式的问题-这可能会导致编译器出现问题
Example - would work fine:
startprog
for 10 do left 20
if firstvariable do up 30
endif
if secondvariable do right 40 + 30
endif
endfor
endprog
我们看到一种缩进良好的编程风格,这显然在上面起作用。
在Opposite下面——一个奇怪的案例结构,会自动打破循环
Example - might be the problem:
for 10 do left 20
if firstvariable do up 30
endif
myLoopBreakingFunction(); //will leave the loop becaue of wrong indent_count
if secondvariable do right 40 + 30
endif
endfor
endprog
只需尝试将命令由工作站生产的单位
和权限()
排队,看看这是否奏效。第二眼,正如上面的评论中所提到的,您必须在每次迭代中写入您的值。您可能需要一个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);
}
}
公共类制造类{
静态ArrayList myArrayList=新建ArrayList();
函数public static void main(字符串[]args)引发InterruptedException{
//(...)
对于(loop_for_orders=1;loop_for_orders请将您的问题简化为比这短得多的问题。另外,请正确缩进您的代码,并严格遵守Java命名约定。我猜您并不真正理解什么是static
的意思。请阅读有关我的循环问题的文章。我已经减少了代码,大量的f代码,所有内容都在一个类中,并且是静态的,请尝试正确利用OOP。您的问题是:您正在覆盖循环的每个步骤中打印的值,然后打印上一次迭代的结果。您需要将要打印的值存储在列表中,而不是每次都覆盖它们。但是,这只是一个肮脏的修复。我认为建议您重构整个代码。显然,您还没有阅读我的评论:您的代码仍然庞大,它仍然不遵守Java命名约定,它仍然有大量的静态变量,我怀疑您在这几秒钟内没有阅读我链接的页面。@Philipp我已经减少了我知道程序如何执行的代码。我也知道这一点循环只打印迭代的最后一个值,因为我在循环结束时打印。。是否有任何方法可以在循环结束时打印我不理解的每个循环值…你能再详细说明一下吗?你能在teamviewer上提供帮助吗?我写了第二个答案:)希望两者都有帮助。我只是说:小心右角的空格,让函数调用与for-Loop保持一致。在数组列表中添加值后,如何在循环末尾打印这些值我将为其编写一个函数:)好的,伙计,我在等……让我们输入输出字符串,如果有值1,那么我将如何打印它,比如:printThisOutputLine:(对于循环i)System.out.prinln(ManufactureClassOBject.myArrayList[j]);很难在注释中解释这一点,因为缺少在这里编写代码的可能性。
public void printMyOutputLine(){
for(j=0;j<this.myArrayList.size();j++)
{
System.out.println(this.myArrayList[j]);
}
}