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