Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图找到负指数的答案,只有正指数和0有效_Java - Fatal编程技术网

Java 试图找到负指数的答案,只有正指数和0有效

Java 试图找到负指数的答案,只有正指数和0有效,java,Java,当我试图找到答案时,我不能用数学课。在计算答案时,我必须使用for循环 package Powers; import TerminalIO.KeyboardReader; public class powers { public static void main(String [] args) { KeyboardReader reader= new KeyboardReader(); //Variables int base; //The base number

当我试图找到答案时,我不能用数学课。在计算答案时,我必须使用for循环

package Powers;

import TerminalIO.KeyboardReader; 

public class powers {
public static void main(String [] args) 
{
    KeyboardReader reader= new KeyboardReader();
    //Variables
    int base; //The base number for the calculations
    int minE; //The minimum exponent that will be used
    int maxE; //The maximum exponent that will be used
    int answer; //Weather the user wishes to repeat or not
    double result = 1; //The result of the base to the power of the exponent

    //Inputs
    do { /* Start of repeat */  base = reader.readInt ("Please enter the base. "); 
    minE = reader.readInt ("Please enter the minimum exponent. ");
    maxE = reader.readInt ("Please enter the maximum exponent. ");
        //Error Checking
        while (minE > maxE) {
            System.out.println("There seems to be an error, your minimum exponent is greater then your maximum exponent. Please re-enter your values");
            minE = reader.readInt("Enter your minimum exponent. ");
            maxE = reader.readInt("Enter your maximum exponent. ");
        }
        //End of Error Checking

    //Calculations          
        //Output    
        System.out.println(" Base          Exponent          Result");
        System.out.println();
        int expo; //The exponent by which the base will be powered to
        for (expo = minE; expo <= maxE; expo++) {

            //For exponent being 0
            result = 1; 
            //For exponent being <0
            if (expo <0) {
                for (int i= minE;i != 0;i = i + 1) {
                    result = result/base;
                }
            }

            if (expo == 0) {
                result = 1 ;
            }

            //For exponent being 1
            if (expo == 1) {
                result = base;
            }
            //For exponent being >1
            if (expo > 1) {
                for (int i=1;i<=expo;i++) {
                    result = result*base;
                }
            }



            System.out.println( "    " + base + "                 " + expo + "             " + result); 
        }
        System.out.println();
        answer = reader.readInt ("Do you wish to repeat? Enter 1 if yes.");
    } while (answer == 1); //End of repeat

}
正如你所看到的,只有0和正功,而不是负功

我刚刚在负循环上添加了一个“计数器”。您总是在所有负值计算中存储“-2”(或最低值)

您必须添加
+1(-2+1=-1)
,以便正确划分

这只是一个逻辑问题

    int counter = 0;
    for (expo = minE; expo <= maxE; expo++) {

        //For exponent being 0
        result = 1; 
        //For exponent being <0
        if (expo < 0) {
            for (int i = (minE + counter); i != 0; i = i + 1) {
                result = result / base;
            }
            counter++;
        }

您错误地初始化了
i
。而不是
i=minE
将其更改为
i=expo

for (int i = expo; i != 0; i = i + 1) {
    result = result/base;
}

expo
为负时执行的
for
循环根本不使用
expo
。这就是问题所在。哦,很好,我只是让事情变得更难。但仍然有效+1
 Base          Exponent          Result

    2                 -3             0.125
    2                 -2             0.25
    2                 -1             0.5
    2                 0             1.0
    2                 1             2.0
    2                 2             4.0
    2                 3             8.0
for (int i = expo; i != 0; i = i + 1) {
    result = result/base;
}