扫描多少基数和指数值的Java程序然后显示它们的总和

时间:2017-09-14 10:59:06

标签: java

我想使用for循环来编写一个扫描多少个数的java程序,例如,如果我输入4并且指数是2那么计算应该是这样的:1²+2²+3²+4²。得到总和,输出应为30。

我试着用exponent编写一个for循环程序,但只想出了如何得到1值之和,如果我输入4而指数是2,它只将4乘以指数得到总和16。

到目前为止,这是我的代码,我被困住了:

public static void main(String[] args){
    Scanner in = new Scanner(System.in);

    int base = 0, exponent = 0;
    long result = 1;
    System.out.print("Enter #: ");
    base = in.nextInt();
    System.out.print("Enter exponent: ");
    exponent = in.nextInt();

    for (;exponent! = 0; --exponent){
       result *= base;
     }
     System.out.println("Answer = " + result);
 }

谢谢你,希望你们能帮我解决这个问题。

3 个答案:

答案 0 :(得分:1)

你走了:

import java.util.Scanner;

public class Main {

public static void main(String[] args){

    Scanner in = new Scanner(System.in);

    int base = 0, exponent = 0;
    double result = 0;
    System.out.print("Enter #: ");
    base = in.nextInt();
    System.out.print("Enter exponent: ");
    exponent = in.nextInt();

    int count = 1;

    while(count<=base){
        result = result + Math.pow(count, exponent);
        System.out.println("PartialAmount = " + result);
        count++;
    }

    System.out.println("Answer = " + result);
  }
}

答案 1 :(得分:0)

试试这个:

public class Exponents extends ConsoleProgram {

  public void run(){
      for (int n = 0; n <= 10; n++) {

            println("2 to the power of " + n + " = " + raiseIntPower(n)); 

     } 

  }
private int raiseIntPower (int n){
   int total = 0;
   for( int n = 0; n <= 10; n++){
     total = Math.pow(2, n);

  } 

return total;

答案 2 :(得分:0)

使用数学函数

package com.stackoverflow;

import java.util.Scanner;

public class SumOfExponents {

    public static void main(String[] args) {
        int base = 0, exponent = 0;
        long result = 0;
        Scanner in = new Scanner(System.in);
        try {
            System.out.print("Enter base value: ");
            base = in.nextInt();
            System.out.print("Enter exponent: ");
            exponent = in.nextInt();
        } catch (Exception e) {
            System.err.println("Illigal number entered");
            in.close();
            return;
        }
        String expression = "";
        for(int i=1; i<=base; i++){
            result += Math.pow(i, exponent);
            expression += " + "+String.join("^", Integer.toString(i), Integer.toString(exponent));
        }
        System.out.println(expression.substring(3)+" = "+result);
        in.close();
    }
}

不使用任何数学函数

package com.stackoverflow;

import java.util.Scanner;

public class SumOfExponents {

public static void main(String[] args) {
    int base = 0, exponent = 0;
    long result = 0;
    Scanner in = new Scanner(System.in);
    try {
        System.out.print("Enter base value: ");
        base = in.nextInt();
        System.out.print("Enter exponent: ");
        exponent = in.nextInt();
    } catch (Exception e) {
        System.err.println("Illigal number entered");
        in.close();
        return;
    }
    String expression = "";
    for(int i=1; i<=base; i++){
        long temp = 1;
        for(int j=0; j<exponent; j++){
            temp *= i;  
        }
        result += temp;
        expression += " + "+String.join("^", Integer.toString(i), Integer.toString(exponent));
    }
    System.out.println(expression.substring(3)+" = "+result);
    in.close();
}

}