This site requires JavaScript, please enable it in your browser!
Greenfoot back
Bl4ckJK
Bl4ckJK wrote ...

2012/11/18

How can I build a program, which gives me the numbers 1,2,4,8,16?

Bl4ckJK Bl4ckJK

2012/11/18

#
Hey, I want to build a program to "system.out.print" this numbers 1,2,4,8,16... I only know, that this works with a for-loop, but how? Pls help me :)
SPower SPower

2012/11/18

#
Something like this?:
for (int i = 1; i <= 16; i *= 2) {
    System.out.println(i+"");
}
Bl4ckJK Bl4ckJK

2012/11/18

#
yea exactly can you explain me this pls? :D
SPower SPower

2012/11/18

#
A for loop needs an int:
for (int i = 1;
in this case, it starts off as 1. The for loop will continue as long as i is smaller or equal to 16:
i <= 16;
And every time the code between the {} is executed, i will be multiplied by 2:
i *= 2)
Bl4ckJK Bl4ckJK

2012/11/18

#
okay thx :)
groengeel groengeel

2012/11/18

#
Please-make-my-homework-topics ftw.
Sl3nDeRm4n Sl3nDeRm4n

2012/11/18

#
groengeel wrote...
Please-make-my-homework-topics ftw.
true
davmac davmac

2012/11/19

#
for (int i = 0; i < 1; i = 1) {
    System.out.println("1");
    System.out.println("2");
    System.out.println("4");
    System.out.println("8");
    System.out.println("16");
}
fixed that for you.
mjrb4 mjrb4

2012/11/19

#
for(int _=((byte)"_".charAt(0)-97^3)+4;_<=(((int)Math.pow(3, 5.7)^3^45^542)/15)*4;_*=4^6)System.out.println(_);
If I were doubly cheeky, I'd say this improves on SPower's example by not needlessly concatenating a string to the iterator ;)
danpost danpost

2012/11/19

#
Then there is this:
for(int i=0;i<20;i++)System.out.println((int)Math.pow(2,i));
You need to login to post a reply.