'how do I get the correct amount out of the cycles?

I have code that I adapted from the university's methodical index. The sum should be 15, but it's not the same number. Please tell me how to fix it. Thanks in advance!

static void forMethod(int n, int a, int s){
       n = 3;
       a = 2;
       s = 0;
       for (int i = 1; 1 <= 3; ++i){
            s += 1 + a + Math.pow(a, i);
            System.out.println(s);
       }
}


Solution 1:[1]

Could it be that your function is supposed to add powers in a given range? The typos in your code suggest that you tried to find the solution through trial and error.

If my guess is correct, s could be the start value and n could be the end value. a would be the value to be raised. The result would be 15 with the values you used!

    public static void main(String[] args){
        System.out.println(forMethod(3, 2, 0));
    }

    static int forMethod(int n, int a, int s){
        //n = 3;
        //a = 2;
        //s = 0;
        int result = 0;
        for (int i = s; i <= n; i++){
             result += Math.pow(a, i);
        }
        return result;
    }

Solution 2:[2]

Lets break down what you've got there line by line

static void forMethod(int n, int a, int s){

This is a method named forMethod, it doesn't specify it's modifier so it's package private. That means that it can only be accessed by the package it's in. It requires 3 numbers to be passed into it to work. These 3 numbers are called parameters. They're of type int which means they have to be integer values. So if you were to call this function (make the code in the method run from outside of this method) you would want to call it, and pass 3 integer values into it for example forMethod(1,2,3)

   n = 3;
   a = 2;
   s = 0;

This here takes the integers n,a, and s and sets them equal to 3,2, and 0 respectively. This is why people are confused. Why would someone write over whatever was passed into the function with set terms like this?

   for (int i = 1; 1 <= 3; ++i){

This right here is a for loop, a loop is a piece of code that makes something happen again and again. A for loop lets you decide how many times you want that thing to happen. What it's saying is (the integer i = 0) While i is less than three is probably what they meant to say, so you can replace that 1 with a lowercase i (while i is less than or equal to 3 run the following code inside of the squiggly braces{})(Every time you run the code add 1 to i)

        s += 1 + a + Math.pow(a, i);

+= is a way of saying add to, so s is equal to s +1 since s is 0 that makes s 1 + a (a=2) so that makes it 3. Math.pow raises it to a power so it's saying to take a raised to the power of i and add it to the rest of that

        System.out.println(s);

This prints out S to the console }

This closes the for loop

}

This closes the method

Solution 3:[3]

If you could explain what you want the code to do, that would be great! It seems like you don't quite understand what the code does, so I will try to explain it to you and you can maybe find the logical error yourself ?

 static void forMethod(int n, int a, int s){
         n = 3;
         a = 2;
         s = 0;
         for (int i = 1; 1 <= 3; ++i){
              s += 1 + a + Math.pow(a, i);
              System.out.println(s);
         }
  }

This is a method (also called a function by some) called forMethod which takes in *3 Parameter * int n, int a, int s.

What is an Parameter?

A parameter is a variable used to define a particular value during a function definition.

You have defined the function forMethod to require anyone who wants to use (call) the function to provide 3 numbers (int) which you call n a and s

Mistake:

You don't need to set n=3 a=2 and s=0 inside your function. When you call the function forMethod, it will be provided with the arguments you are requiering.

Let me show you with some code:

class Main{
  public static void main(String[] args) {
    Numbers n = new Numbers();
    n.printSum(5,5,5);
  }
}

class Numbers{
  static void printSum(int a, int b, int c){
    System.out.println(a+b+c); //will print 5+5+5 = 15
  }
}

As you can see from the code above, the Main class calls the function printSum and provides it with 3 arguments a,b and c. In our example, we provide it with the numbers 5 5 and 5. the printSum function adds the 3 parameters (a, b, c) together and prints the result.

Back to your method

With this in mind, we can already fix parts of your function:

  static void forMethod(int n, int a, int s){
         for (int i = 1; 1 < n; i++){
              s += 1 + a + Math.pow(a, i); //<-- sum = sum + 1 + a + a^i
              System.out.println(s);
         }
  }

Nice!

Now we have cleaned up your code a bit. You can now use this function, something like this:

class Main{
  public static void main(String[] args) {
    Numbers n = new Numbers();
    n.forMethod(3,2,0); //n = 3, a = 2, s = 0    
  }
}

Now we need to fix your for loop

 for (int i = 1; 1 <= 3; ++i)

For loops have accepts 3 arugments:

  • int i = 1; This means "The start value is int i which has a value of 1"
  • 1 <=3; This means "While the value, 1, is lesser or equal to 3, keep looping"
  • ++i This means "Before you start looping, increase i with 1"

A very common for loop is this:

for(int i = 0; i < stop_value_here; i++);

The only diffrence from the one above and your is:

  • i starts on zero.
  • Keep looping while i is less than the stopping value.
  • AFTER each loop (not before) increase i by 1.

for example:

for(int i = 0; i < 3; i++)

This will loop 3 times

Try it out with this code! It will print "I am looping" 3 times.

class Main{
  public static void main(String[] args) {
    for(int i = 0; i < 3; i++){
      System.out.println("I am looping!");
    }
  }
}

Your issue

Your for loop, will loop for infinity:

for (int i = 1; 1 <= 3; ++i)

Due to: "Keep looping while 1 <=3" 1 is always less or equal to 3, so it will loop forever.

Fixing your code... kinda

I have no clue, how you are suppose to get 15 out of your method, but this will atleast get you closer:

class Main{
  public static void main(String[] args) {
    Numbers n = new Numbers();
    n.forMethod(3,2,0);
  }
}


  static void forMethod(int n, int a, int s){
         for (int i = 0; i < n; i++){
              s += 1 + a + Math.pow(a, i);
              System.out.println(s); //will print 4,9 and 16
         }
  }
}

I hope this helps ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 QuinncyJones
Solution 2 sss
Solution 3 CodeAddict