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

时间:2015-01-14 04:33:22

标签: 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 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

}

}

出现的示例答案是

Please enter the base. 2
Please enter the minimum exponent. -2
Please enter the maximum exponent. 2
Base          Exponent          Result

2                 -2             0.25
2                 -1             0.25
2                 0             1.0
2                 1             2.0
2                 2             4.0

正如你只能看到0和积极的工作,而不是负面的。

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

我刚刚添加了一个&#34;计数器&#34;在负循环上。你总是存储&#34; -2&#34;所有负值计算的(或最低值)。

您必须添加+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++;
        }

输出示例:

 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