import java.util.Scanner;
public class PowersOf2
{
public static void main(String[] args)
{
int inputPowersOf2;
int PowerOf2 = 1;
int exponent;
int exponent2;
Scanner scan = new Scanner(System.in);
System.out.println("How many powers of 2 would you like printed?");
inputPowersOf2 = scan.nextInt();
System.out.println("\n\n");
if(inputPowersOf2 >= 2)
{
System.out.println("Here are the first " + inputPowersOf2 + " powers of 2:");
System.out.println();
}
else
{
System.out.println("Here is the first power of 2:");
System.out.println();
}
exponent2 = 0;
exponent = 0;
while(exponent <= inputPowersOf2)
{
System.out.print("2^" + exponent2 + " = ");
exponent2++;
System.out.println((PowerOf2 = 2 * PowerOf2) / 2);
exponent++;
}
}
}
为什么当我说给我 2 的 1 次方时它会打印
2^0
2^1
当我说给我 2 的 2 次方时,它会打印
2^0
2^1
2^2
等等...