PDA

View Full Version : for


Rikus
09-14-2006, 11:02 AM
ok so im doing java programming at school and im as of today totally clueless.:(

before i even begin, i just want u all to know im no idiot im just starting with java.
anyway

i need to write a little programme to add up all numbers from 10 to 20 using "for" (ps. i have no idea what it's called in, my home language it's for- lus, i dont know what the "lus" part is in english) this is how the for- looks when used:

for ( int i = 10; i<=20; i = whatever)

i just used the numbers 10 and 20 for numbers to show you what i need to do.
Just to clarify a few thing i want to: 10 + 11 + 12 + 13 + 14...... up until 20.

please help asap

rabbitmonkey
09-14-2006, 11:23 AM
You'll need another variable. Inside the loop, do variable += i;. The end,

Rikus
09-14-2006, 11:35 AM
i think i know what you mean but im not very sure how to do it.
so i did some more searching and i found something that actually works, but the only problem is it is not in my stupid handbook so im not sure if i may use it yet i'll ask tomorrow at school but take a look in any case:

// The "TienTotTwintig" class. bl 81 no 2
public class TienTotTwintig
{
public static void main (String [] args)
{
int getalle [] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
for (int i = 0, totaal = 0 ; i < getalle.length ; i++)
{
totaal += getalle[i];
System.out.println (totaal);
}
System.out.println ();
} // main method
} // TienTotTwintig class

rabbitmonkey
09-14-2006, 01:26 PM
public class Main
{
public static void main(String [] args)
{
int total = 0;
for(int i = 10; i < 20; i++)
{
total += i;
System.out.println(total);
}
}
}

Rikus
09-14-2006, 01:33 PM
ooooo ok thank you verrrry much i really needed that