我需要计算select
p.*,
(
select
count(distinct s.song_id)
from
playlist_songs ps
join songs s on s.song_id = ps.song_id
join artists art on s.artist_id = art.artist_id
where p.playlist_id = ps.playlist_id
) number_of_songs
from
playlist p
where
number_of_songs > 1;
的总和,其中2^0+2^1+2^2+...+2^n
是用户输入的数字。主要问题是我不知道如何使用while循环来总结n
的不同结果。
以下是我尝试的内容:
2^n
答案 0 :(得分:2)
只有你必须做的是:
import java.util.Scanner;
public class SumOfThePowers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type a power: ");
int power = Integer.parseInt(reader.nextLine());
double sum = Math.pow(2,power+ 1 ) - 1;
System.out.println("The sum is: " + sum);
}
}
在此link解释数学表达式
答案 1 :(得分:0)
你的条件是倒退的,应该是:
while (i <= power)
答案 2 :(得分:0)
你计算权力之和,然后完全忽略它,只打印出2 ^ i的结果。你应该打印出总和,如:
while (i <= power) {
sum += Math.pow(number, i);
i++;
}
System.out.println("The sum is: " + sum);
对于样式点,这不会处理负面的力量,所以你需要测试它。
答案 3 :(得分:0)
一切都很好,只是一些变化。 将零件代码更改为
System.out.println("Type a power: ");
int power = Integer.parseInt(reader.nextLine());
int number = 2;
int i = 0;
double sum = 0;
/*Remove this --------> while(power <= i) {*/
while (i <= power) {//it should be this
/*Remove this -------> Math.pow(number, i);*/
sum = sum + Math.pow(number, i);
i = i + 1;
}
System.out.println("The sum is: " + sum);
答案 4 :(得分:0)
不明白你为什么要在这种情况下循环。你可以这样做:
System.out.println("The sum is: "+(Math.pow(2, power+1)-1 ));
但如果你真的想使用循环试试这个:
Scanner reader = new Scanner(System.in);
System.out.println("Type a power: ");
int power = Integer.parseInt(reader.nextLine());
int number = 2;
int i = 0;
double sum = 0;
while(i<=power) {
sum = sum + Math.pow(number, i);
i = i + 1;
}
int result = (int)Math.pow(number, i);
System.out.println("The sum is: " + sum);
答案 5 :(得分:0)
这是一个解释逻辑的解决方案。 while循环需要某种变量来控制迭代次数。该变量当然需要在内部循环中更新。
显然,您可以使用Math.pow()函数计算幂的总和。使用它不需要import语句。我希望这有帮助。祝你好运。
/* Scanner and variable to get and hold user input */
Scanner scan = new Scanner( System.in );
int userInput = 0;
/* Variable to hold the sum, initialized to 0.0 */
double sum = 0.0;
/* Prompt the user, and obtain the reply */
System.out.print( "Enter the exponent: ");
userInput = scan.nextInt();
/* The while loop and it's initialized counter */
int counter = 0;
while( counter <= userInput ){
/* Add each power to sum using Math.pow() */
sum = sum + Math.pow( 2, counter );
/* Watch the output as the loop runs */
System.out.println( "Sum: " + sum );
counter++; // Increment counter, so the loop exits properly
} // End while loop
答案 6 :(得分:-1)
public class SumofSquare {
public static void main(String[] args) {
// TODO Auto-generated method stub
String c = "123";
char d[] = c.toCharArray();
int a[] = new int[d.length + 1];
for (int i = 0; i < d.length; i++)
a[i] = d[i] - 48;
int r = 0;
for (int i = 0; i < c.length(); i++)
r = r + (int) Math.pow(a[i], a[i + 1]);
System.out.println(r);
}
}
答案 7 :(得分:-1)
import java.util.Scanner;
public class SumOfThePowers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type a number:");
int power=Integer.parseInt(reader.nextLine());
int number=2;
int i=0;
int result=0;
while (power>=i) {
result += (int)Math.pow(number, i);
i++;
}
System.out.println("The result is "+result);
}
}
答案 8 :(得分:-2)
import java.util.Scanner;
public class SumOfThePowers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type a number:");
double power=Double.parseDouble(reader.nextLine());
int number=2;
int i=0;
double sum=0;
while (power>=i) {
sum=sum+Math.pow(number, i);
i++;
}
System.out.println("The sum is "+sum);
}