Thread: hay
View Single Post
Old Mar 2nd, 2008, 10:33 PM   #3
kalli002
Newbie
 
Join Date: Mar 2008
Location: Sri lanka
Posts: 2
Rep Power: 0 kalli002 is on a distinguished road
Send a message via Skype™ to kalli002
Thumbs up Re: hay

Ya it works and thank you very much for that
another thing as i know for loop doing rept what it got inside so i thought by puting while loop inside for loop it will repet until the for loops condition "yes"
is that because when we hit "0"and stop the while loop then the compiler takes x value as "0" for the hole program

Quote:
Originally Posted by OpenLoop View Post
Please use CODE tags next time.

You need to re-initialize x inside the for loop. The last zero read in is preventing the while loop from executing:
(also initialize total if it's meant to be for each box)

java Syntax (Toggle Plain Text)
  1. int x=1;
  2. int total=0;
  3. int t1=0;
  4. for(int j=0;j<3;j++){
  5. System.out.println("Box 00"+(j+1)+"");
  6. x = 1;
  7. total = 0; //re-initialize the individual box total
  8. while(x!=0){
  9. System.out.print("Input egg wegiht ");
  10. x=input.nextInt();
  11. total=total+x;
  12.  
  13. }
  14. System.out.println("Total wegiht of the box : "+total);
  15. System.out.println();
  16. t1=total+t1;
  17. }
  18.  
  19. System.out.println("Grand Total :" +t1);
  20. }
  21. }
kalli002 is offline   Reply With Quote