Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 2nd, 2008, 9:32 PM   #1
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 hay

im very new to JAVA i goot an assignment to write a program that is
take 3 boxes and it has "n" number of eggs after each box done with the counting it have to print the totel of it and after all 3 done max total have to print so far i done as below
import java.util.*;
class egg3{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
int x=1;
int total=0;
int t1=0;
for(int j=0;j<3;j++){
System.out.println("Box 00"+(j+1)+"");

while(x!=0){
System.out.print("Input egg wegiht ");
x=input.nextInt();
total=total+x;

}
System.out.println("Total wegiht of the box : "+total);
System.out.println();
t1=total+t1;
}

System.out.println("Grand Total :" +t1);
}
}
this can enter any number of valus as u can see when we give "0" i stopes and print total ,but it wont continu to other 2 as well

what shud i do ples explane
kalli002 is offline   Reply With Quote
Old Mar 2nd, 2008, 10:33 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: hay

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. }
OpenLoop is offline   Reply With Quote
Old Mar 2nd, 2008, 11: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
Old Mar 3rd, 2008, 8:10 AM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: hay

Quote:
Originally Posted by kalli002 View Post
is that because when we hit "0"and stop the while loop then the compiler takes x value as "0" for the hole program
You got the right idea. In most high level languages, the value of a variable remain the same until explicitly changed, drops out of scope (when you're out of main()), or when the program exits. If x was zero the last time while() was executed, it will remain zero for the rest of the runtime (unless you re-initialize it like I did)
OpenLoop is offline   Reply With Quote
Old Mar 3rd, 2008, 8:32 AM   #5
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 342
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: hay

use white space formating like this. Use 4 spaces instead of tabs(set in editor), use tabs for inside methods, classes, if and loops. Place spaces between operands and operators. It makes the code more "human readable".

java Syntax (Toggle Plain Text)
  1. public class Test
  2. {
  3. public static main void(Strings[] args)
  4. {
  5. int x = 1;
  6. int total = 0;
  7. int t1 = 0;
  8.  
  9. for(int j = 0; j < 3; j++)
  10. {
  11. System.out.println("Box 00" + (j + 1) + "");
  12. x = 1;
  13. total = 0;//re-initialize the individual box total
  14.  
  15. while(x! = 0)
  16. {
  17. System.out.print("Input egg wegiht ");
  18. x = input.nextInt();
  19. total = total+x;
  20. }
  21.  
  22. System.out.println("Total wegiht of the box : " + total);
  23. System.out.println();
  24. t1 = total + t1;
  25. }
  26.  
  27. System.out.println("Grand Total :" + t1);
  28. }
  29. }
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC