在找到低于某个数字的 2 的所有幂后找到循环的总和
Finding total sum of loop after finding all powers of 2 below certain number
总的来说,我是 java/programming 的新手,这是一项家庭作业。到目前为止,这是我所拥有的:当我 运行 它时,我得到了低于 n 输入的 2 的幂。例如,如果 n = 50,则输出为 2 + 4 + 8 + 16 + 32 + = -2
我希望 32 之后的 + 消失,但我不知道如何正确求和。在这种情况下,我希望总和 = 62。我尝试使用字符串生成器去掉最后两个字符,但这对我不起作用。
import java.util.Scanner;
public class Powers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n;
System.out.print("Enter the upper limit: ");
n = scan.nextInt();
int sum = 0;
int power = 1;
for (int i = 0; i <= n; i++) {
power = 2 * power;
if (power < n && 0 < power) {
System.out.print(power + " + ");
}
sum = sum + power;
}
System.out.println(" = " + sum);
}
}
这里有多个问题:
- 当达到上限时,您只需停止输出,继续求和。
- 您使用上限作为迭代次数,因此在您的示例中
50
的情况下,您对 1 到 2^50 之间的所有值求和,这就是结果的原因是负数,因为总和变得大于 int
可以保留的最大数。
关于如何打破循环的问题,break
;-)
您的打印总是输出 +
,这就是为什么您的输出中有 + =
。将输出更改为如下内容:
if (power < n && 0 < power) {
if (i != 0) {
System.out.print(" + ");
}
System.out.print(power);
}
我已经为您的代码添加了一些功能。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Type number:");
Scanner scanner = new Scanner(System.in);
int n = 0;
while (n == 0) { // to ask again if "n" is zero.
n = scanner.nextInt();
}
if (n != 0) {
scanner.close(); // to prevent resource leak
int sum = 0;
int power = 1;
for (int i = 0; i < n; i++) {
power *= 2;
sum += power;
System.out.print(power + " ");
if (sum + power * 2 < 0 | i == n - 1) {
// Should we step to the next iteration?
// If next "sum" will be bigger than the max value for
// integers
// or if this iteration is the last - it will type "sum",
// break "for" cycle and go the next line of code after
// "for" cycle.
// So in this case System.out.print("+ "); won't be
// executed.
System.out.print("= " + sum);
break;
}
System.out.print("+ ");
}
}
}
}
总的来说,我是 java/programming 的新手,这是一项家庭作业。到目前为止,这是我所拥有的:当我 运行 它时,我得到了低于 n 输入的 2 的幂。例如,如果 n = 50,则输出为 2 + 4 + 8 + 16 + 32 + = -2 我希望 32 之后的 + 消失,但我不知道如何正确求和。在这种情况下,我希望总和 = 62。我尝试使用字符串生成器去掉最后两个字符,但这对我不起作用。
import java.util.Scanner;
public class Powers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n;
System.out.print("Enter the upper limit: ");
n = scan.nextInt();
int sum = 0;
int power = 1;
for (int i = 0; i <= n; i++) {
power = 2 * power;
if (power < n && 0 < power) {
System.out.print(power + " + ");
}
sum = sum + power;
}
System.out.println(" = " + sum);
}
}
这里有多个问题:
- 当达到上限时,您只需停止输出,继续求和。
- 您使用上限作为迭代次数,因此在您的示例中
50
的情况下,您对 1 到 2^50 之间的所有值求和,这就是结果的原因是负数,因为总和变得大于int
可以保留的最大数。
关于如何打破循环的问题,break
;-)
您的打印总是输出 +
,这就是为什么您的输出中有 + =
。将输出更改为如下内容:
if (power < n && 0 < power) {
if (i != 0) {
System.out.print(" + ");
}
System.out.print(power);
}
我已经为您的代码添加了一些功能。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Type number:");
Scanner scanner = new Scanner(System.in);
int n = 0;
while (n == 0) { // to ask again if "n" is zero.
n = scanner.nextInt();
}
if (n != 0) {
scanner.close(); // to prevent resource leak
int sum = 0;
int power = 1;
for (int i = 0; i < n; i++) {
power *= 2;
sum += power;
System.out.print(power + " ");
if (sum + power * 2 < 0 | i == n - 1) {
// Should we step to the next iteration?
// If next "sum" will be bigger than the max value for
// integers
// or if this iteration is the last - it will type "sum",
// break "for" cycle and go the next line of code after
// "for" cycle.
// So in this case System.out.print("+ "); won't be
// executed.
System.out.print("= " + sum);
break;
}
System.out.print("+ ");
}
}
}
}