在Java中......
打印以下数字:(1,2,4,8,16 ......等)。
到目前为止,我有这个:
int[] b = new int[16];
for (int i = 0; i < 16; i++)
答案 0 :(得分:3)
long number = 1;
for (int i = 0; i < 16; ++i) {
System.out.println(number);
number *= 2L;
}
答案 1 :(得分:2)
另一种简单的方法(和恕我直言,更好地表达了想要的东西):
int limit = 100; // or whatever is requested
for (int i = 1; i <= limit; i *= 2) {
System.out.println(i);
}
答案 2 :(得分:0)
你可以在循环体中保留多个变量:
String character = scanner.findInLine(ID);
if ((character.trim().equals(ID)) { // by trimming I'm removing the line separator and then use equals to do a string comparision.
Decision = true;
System.out.println("they are a match: " + ID);
} else {
Decision = false;
}
答案 3 :(得分:0)
使用按位移位并以int i = 1
:
for(int i = 1; i < 16; i <<=1){
/*...*/
}