-5

Sorry there was a mistake earlier in this. This actually worked, but I'm confused where the value of result is involved in the for loop.

import java.util.Scanner;

  public class PowerLoop
{
     public static void main(String [] args) 
 {

       exponent(1);
}

  public static int exponent(int result) // defining method for raising base to power

{

     int base; // defining user input variables
     int power;
 {
    Scanner reader = new Scanner(System.in); // scanner is called reader

   System.out.println("Please enter the base."); // first input is base
   base = reader.nextInt();

   System.out.println("Please enter the exponent."); 
   power = reader.nextInt();
{
    for (int i = 1; i <= power; i++)
 {
     result = base * result;
 }
     System.out.println("Result: " + base + " ^ " + power + " = " + result);

   return 0;

}
eri
  • 5
  • 2
  • I thought of it like this: if I typed in 2 as the base in the program and 3 as the power, then I see that int i <= 3 for the checking condition, but I’m really confused as to where the program gets the value for result to multiply base. – eri Oct 03 '18 at 01:59
  • 1
    This code is not valid java (for example, too many opening braces). But `result` is declared in the parameters for the method `exponent`, and then used in the `for` loop. But please fix the code to a Minimal, verifiable example, as there is no check for `<=3` in the above code. – KevinO Oct 03 '18 at 02:03
  • Also please strive to post well formatted code with proper indentation. This will 1) make your code easier for others to read, and 2) help you reduce errors from mismatched braces and such. – Hovercraft Full Of Eels Oct 03 '18 at 02:17
  • I'm not allowed to use Math.pow for my Java class – eri Oct 03 '18 at 03:10

1 Answers1

0

In your for loop, the variable result Is created outside of the for loops scope. Essentially, you are just changing the result variable that has already been created. If you attempted to create a new variable within the for loop such as "newResult = base * result", it would fail if you attempted to use it outside of the for loop. Here is a simple example that optimizes your code:

//Start getting the proper variables in your MAIN method. 
Scanner reader = new Scanner(System.in);//create scanner here. 
System.out.println("Enter a base number: ");
int base = reader.nextInt();
System.out.println("Enter an exponent: ");
int power = reader.nextInt();
out.println(exponent(base, power));

Then you could optimize the exponent method like so:

public static int exponent(int base, int power) {
    double r = Math.pow(base, power);
    int result = (int)r;
    return result;

}

Now, for your specific question, lets imagine changing the for loop in your example to this below, this is the important point:

for(int i = 1; i <= power; i++)
{
   int newResult = base * result;
}
System.out.println("Result: " + base + " ^ " + power + " = " + newResult);

That code above would generate an error because you are trying to access a variable outside of the scope it was created in. However, since result is passed to the method in your example as a parameter, it is considered to be accessible by the for loop that is created later within the scope of the method. Essentially, the result variable that you already defined will simply reference a new value inside of your for loop. If we attempted to create a new result variable in the for loop such as newResult, you would not be able to reference it outside of the scope of that for-loop.

Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27