1

I am supposed to implement a Method to my program that multiply all of the integers in the intervall n1,n2. This is my code:

static int productofIntervall (int n1, int n2){
    int a;
    while(n1 <= n2){
        n1*n2 = a;
        n1=n1++;
    }
    return(a);
}   
        public static void main(String[] args){
        System.out.println(productofIntervall(6,11));
    }

}

When I try to comply i recieve the error:

Main.java:6: error: unexpected type
                        (n1)*(n2)=a;
                            ^
  required: variable
  found:    value
1 error

Can anybody tell me what`s wrong? Thanks in advance.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Mert Engin
  • 11
  • 1
  • 1
    The way the statement is written is that you are trying to assign a to n1 * n2. Also, the assignment of n1 = n1++ is not doing what you think either. – user3745362 Dec 05 '17 at 18:47
  • This looks more like there is a test to identify compilation error. So its best you learn java until you can figure this one out yourself. PS: You can try using an IDE like eclipse which will save you some time in that it compiles on save. – Ravindra HV Dec 05 '17 at 19:02
  • There is a similar question [here](https://stackoverflow.com/questions/24564603/java-increment-and-assignment-operator). – Ravindra HV Dec 05 '17 at 19:23

2 Answers2

2

You need to initialize a = 0 and set a = n1*n2 not the other way around. Also n1 = n1++ can and is preferably replaced by just n1++

You are basically setting the product of two numbers to be a value (uninitialized variable) which won't work

Khaldoun Nd
  • 1,436
  • 12
  • 23
0

Given the below code:

package com.example.so.questions;

public class SO47660627CompilationAndIncrement {

    public static void main(String[] args) {

        int a = 0;
        int b = 1;
        int c = 0;
        int d = 1;
        int e = 0;
        int f = 0;
        int g = 1;
        int h = 1;
        int i = 0;
        int j = 0;

        a = b++;
        c = ++d;
        e = e++;
        f = ++f;
        i = g-(g++);
        j = h-(++h);

        System.out.println(" int a=0; b=1; a=b++ // a is : "+a+" and b is : "+b);
        System.out.println(" int c=0; d=1; c=++d // c is : "+c+" and d is : "+d);
        System.out.println(" int e=0; e = e++ ; // e is : "+e);
        System.out.println(" int f=0; f = ++f ; // f is : "+f);
        System.out.println(" int g=1; int i = g-(g++); // i is : "+ i);
        System.out.println(" int h=1; int j = h-(++h); // j is : "+ j);

    }

}

If you run FindBugs source code analyzer, it flags as a concern - the line comprising :

e = e++;

The explanation is :

Bug: Overwritten increment in com.example.so.questions.SO47660627CompilationAndIncrement.main(String[])

The code performs an increment operation (e.g., i++) and then immediately overwrites it. For example, i = i++ immediately overwrites the incremented value with the original value.

On running the above code, the output is :

 int a=0; b=1; a=b++ // a is : 1 and b is : 2
 int c=0; d=1; c=++d // c is : 2 and d is : 2
 int e=0; e = e++ ; // e is : 0
 int f=0; f = ++f ; // f is : 1
 int g=1; int i = g-(g++); // i is : 0
 int h=1; int j = h-(++h); // j is : -1

From the above output, we can conclude that the the post or pre increment operation involves the creation of a temporary variable that stores the original value - prior to increment operation in case of post-increment operation and after the increment in case of pre-increment operation and then applies the result stored in the temporary variable further in the expression.

Ravindra HV
  • 2,558
  • 1
  • 17
  • 26