编写一个产生以下输出的程序: 3,7,15,31,63,127
我不知道如何构架它。
答案 0 :(得分:0)
它以4 8 16 32 64递增值,依此类推。所以,这很容易。
// that is clearly a parameter
int inputLevel = 6;
// first value. That can be a parameter as well.
int value = 4;
while (i< inputLevel){
System.out.println(value-1);
value = value * 2 ;
i++;
}
答案 1 :(得分:0)
每一步的加法乘以2。
7 - 3 (second and first) = 4,
15 - 7 (third and second) = 8,
31 - 15 (forth and third) = 16
等等。
你可以看到差异是升序系列
4, 8, 16 and continue - 32, 64
答案 2 :(得分:0)
我认为你的任务是将数字翻倍并将其增加1,直到它为127.
int n=3;
while (n < 127) {
n=(n*2)+1;
System.out.print(n + " ");
}
答案 3 :(得分:0)
这背后的简单逻辑。
static String generateSequence(int x){
int temp =3;
StringBuilder result=new StringBuilder("3,");
for (int i = 1; i < x; i++) {
temp = (int) (temp + Math.pow(2, i+1));
result.append(String.valueOf(temp)+",");
}
return result.toString();
}
上面的函数将采用你想要的系列长度参数。
函数调用
System.out.println(generateSequence(5));
输出: 3,7,15,31,63,
函数调用2
的System.out.println(generateSequence(6));
3,7,15,31,63,127,
答案 4 :(得分:-3)
您可以使用:JOptionPane.showMessageDialog(null,&#34; 3,7,15,31,63,127&#34;)
否则你必须设置一个框架,一个jpanel,并在jpanel里面放一些像:JLabel lab = new JLabel(&#34; 3,7,15,31,63,127和#34 ;)