意外的类型误差,并在两个整数中乘以两个整数



我应该对我的程序实现一种方法,该方法将Intertall N1,N2中的所有整数乘以。这是我的代码:

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));
    }

}

当我尝试遵守时,我会收到错误:

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

有人可以告诉我怎么了?预先感谢。

您需要初始化a = 0并设置a = n1*n2而不是相反。另外n1 = n1++可以并且最好被n1++

替代

您基本上将两个数字的乘积设置为一个值(非初始化变量),该值无法正常工作

给定以下代码:

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);
    }
}

如果您运行FindBugs源代码分析仪,则标记为关注点 - 包含的行:

e = e++;

解释是:

错误:覆盖在 com.example.so.questions.so47660627 compilation andincrement.main(string [])

代码执行增量操作(例如,I ),然后 立即覆盖它。例如,i = i 立即覆盖 带有原始值的增量值。

在运行上述代码时,输出为:

 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

从上面的输出中,我们可以得出结论,邮政或前增量操作涉及创建一个临时变量,该变量存储原始值 - 在收入后操作的情况下,在增量之前以及在增量的情况下进行递增操作。提前的操作然后应用表达式进一步存储在临时变量中的结果。

最新更新