Go Back   Java Programming Forums > Java Standard Edition Programming Help > Loops & Control Statements


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-02-2010, 12:43 PM
Junior Member
 

Join Date: Feb 2010
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Pulse_Irl is on a distinguished road

I'm feeling Fine
Default for loop and while loop problems

Write a program to do the following:
· Use a for loop to sum all of the numbers between 1 and 50 that are divisible by 5.
· Use a while loop to sum all of the numbers between 1 and 50 that are divisible by 5.
Check that you are getting the answer you expected!

Hi im getting a compile error for my for loop and ive no idea where to start with the while loop can anybody could point me in the right direction for my while loop

Java Code
class WS1Q2{
	public static void main(String[] args){
		int x, total= 0;
		for(x=1;x<=50;x++)
		{
			if(x / 5)
			{
				total = total + x;
			}
		}
		System.out.println("The total is "+total);
	}//close main
}//close class



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 12-02-2010, 10:10 AM
Junior Member
 

Join Date: Feb 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Zhdophanti is on a distinguished road
Default Re: for loop and while loop problems

Try this (didn't compile it but i think it should work that way). And by the way, try to read the errors most time they will tell you whats wrong

"if (x / 5) {" -> x / 5 is no boolean expression, inside the brackets of an if-condition there must always be an expression which should be true or false

Java Code
class WS1Q2{
	public static void main(String[] args){
		int total= 0;
		for(int x=1;x<=50;x++) // as x is only need inside the loop initialize it there
		{
			if((x % 5) == 0) // % is modulo function, if x is dividable by 5 it will be zero
			{
				total = total + x;
			}
		}
		System.out.println("The total is "+total);
	}//close main
}//close class
Reply With Quote
  #3 (permalink)  
Old 12-02-2010, 10:28 AM
Member
 

Join Date: Jan 2010
Location: Oxford, UK
Posts: 30
Thanks: 2
Thanked 7 Times in 7 Posts
Shambolic is on a distinguished road
Default Re: for loop and while loop problems

If you think about it, you can easily turn any for loop into a while loop.
Consider the following:
Java Code
for(initialise; condition; increment)
{
    //stuff
}
How can you write that as a while loop? I'm sure you can figure it out!
Reply With Quote
  #4 (permalink)  
Old 21-02-2010, 01:27 AM
Junior Member
 

Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
mspray is on a distinguished road

I'm feeling Nerdy
Default Re: for loop and while loop problems

Here is a total solution incase you are still having difficulties


Java Code
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int total = 0;
        for(int i = 0; i <= 50; i++){
            if(i%5 == 0){
                total = total + i;
            }
            System.out.println("i is " + i + " Total is " + total);
        }
        total = 0;
        int x = 0;
        while(x <= 50){
            if(x%5 == 0){
                total = total + x;
            }
            x++;
            System.out.println("x is " + x + " total is " + total);
        }

    }

}
Reply With Quote
  #5 (permalink)  
Old 03-05-2010, 07:09 AM
Junior Member
 

Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
lacome1102 is on a distinguished road
Default Re: for loop and while loop problems

all your post are very good!
__________________
Raleigh Press Release Service
Reply With Quote
Reply

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
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with do-while loop sk8rrr Loops & Control Statements 2 10-02-2010 11:33 PM
hi. i want to rewrite this do loop into a while loop. etidd Loops & Control Statements 3 26-01-2010 09:27 PM
loop or what silverspoon34 Loops & Control Statements 5 19-11-2009 06:10 PM
Need help with loop SwEeTAcTioN Loops & Control Statements 8 25-10-2009 09:59 PM
[SOLVED] for loop lotus Loops & Control Statements 2 13-07-2009 04:47 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:53 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.