samedi 24 janvier 2015

How do I use if statements to get a base^0 to = 1?

I am attempting to write a program that calculates exponents using loops and if statement and arrays,I am very new to java. I am using an if statement in my loop to say if exp == 0 answer = 1, then store that into an array, but it always comes out as 0 no matter what. How do I fix this?



int[] expList = new int[numExponents];

for(int i = 0; i < expList.length; i++)
{
System.out.print("Enter the base: ");
int base = in.nextInt();

System.out.print("Enter the exponent: ");
int exp = in.nextInt();

int answer = 1;
for(int number = 0; number < exp; number++)
{
if (exp == 0)
{
answer = 1;
expList[number] = answer;
}
if (exp >= 1)
{
answer *= base;
expList[number] = answer;
}
}
}
System.out.println(Arrays.toString(expList))


Note that numExponents is the number of exponents the user wants to calculate.


Aucun commentaire:

Enregistrer un commentaire