简体   繁体   English

如何在Java中按降序打印结果?

[英]How to print the result in descending order in java?

I managed to get the result if I enter the base and exponent, but the output should be 如果输入基数和指数,我设法得到了结果,但是输出应该是

For example: the output should look like this 例如:输出应如下所示

>> base:5 exponent: 2 >>基数:5指数:2

5^2 = 25 5 ^ 2 = 25
5^1 = 5 5 ^ 1 = 5

I need help to put something somewhere to make this happen... 我需要帮助将某物放置在某处以实现此目标...

import java.util.Scanner;

public class recursion {

    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int base = 0;
        int expo = 0;

        System.out.print("Enter number for base ");
        for (int i = 0; i < 1; i++)
            base = scanner.nextInt();

        System.out.print("Enter number for exponent ");
        for (int j = 0; j < 1; j++)
            expo = scanner.nextInt();

        System.out.println(base + "^" +expo +" = " +  pow(base,expo));

    }


    public static int pow(int x, int p) {

        System.out.println(x + "^" +p +" = " );
        if (p == 0)
            return 1;
        if (p % 2 == 0) {
            int a = pow(x, (p / 2));
            return  a * a; // This line
        } else {
            int a = pow(x, ((p - 1) / 2));
            return x * a * a; // This line
        }

    }

}

Firstly, the following code snippets demands a review: 首先,以下代码片段需要进行审查:

{
    for (int i = 0; i < 1; i++)
    /*
     * This for loop is unnecessary.
     * It asserts that the following clause is run only once,
     * which is true for any statements anyway.
     */
        // ...
}

    return a * a;
} /* if (p % 2 == 0) */ else {
    /*
     * Statements are unnecessarily nested within else clause.
     * The corresponding then clause does not complete normally.
     * Moving the following code snippet out of an else block
     * would have the same effect, but simplifies the control
     * statements.
     */
    int a = pow(x, ((p - 1) / 2));
    return x * a * a;
}

Within your pow() method, you have a System.out.println() method. pow()方法中,您有一个System.out.println()方法。 You're calling it for debugging, but it's unnecessary as the process returns normally. 您正在调用它进行调试,但是由于该过程正常返回,因此没有必要。 As you're looking for printing the operations for exponent as "from user-specified exponent -> 1" ( "in descending order" ), use a loop to print your System.out.println(base + "^" + expo + " = " + TestClass.pow(base, expo)); 当您要以“从用户指定的指数-> 1”( “降序” )打印指数操作时,请使用循环来打印System.out.println(base + "^" + expo + " = " + TestClass.pow(base, expo)); :

    do // New!
        System.out.println(base + "^" + expo + " = " + TestClass.pow(base, expo));
    while (expo-- > 1); // New!

} /* main( args ) */

and you can remove the debugging line in pow() . 您可以在pow()删除调试行。

Example: ( >> denotes STDIN) 示例:( >>表示STDIN)

Enter number for base >> 5 输入基数>> 5
Enter number for exponent >> 2 输入指数>> 2的数字
5^2 = 25 5 ^ 2 = 25
5^1 = 5 5 ^ 1 = 5

Enter number for base >> 4 输入基数>> 4
Enter number for exponent >> 5 输入指数>> 5
4^5 = 1024 4 ^ 5 = 1024
4^4 = 256 4 ^ 4 = 256
4^3 = 64 4 ^ 3 = 64
4^2 = 16 4 ^ 2 = 16
4^1 = 4 4 ^ 1 = 4

View a live code demo. 查看实时代码演示。

void power(int base, int exp){

//Use for loop to iterate through each exp down to 0
for(int i=exp; i>=0; i--){
     int result= exponent(base,i);
     System.out.println(base + "^" + i + "=" + result)//Will display result as 5^2=25
}

//Recursively computes the result of b^e. //递归计算b ^ e的结果。

int exponent(int b, int e){
 if(e==0){//Base case occurs when e=0.
    return (1);
  }
return (b * exponent(b,e-1));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM