来自int []的Java int中的不兼容类型错误

时间:2014-05-20 18:49:37

标签: java

我编码通过简单地减去并保持计数器来进行二进制除法,但是当我尝试转换为二进制然后除以它时,我得到了不兼容类型错误。这是迄今为止的代码:

    import java.util.Scanner;

    public class Divisao{

public static int[] subtrair(int[] bin1 , int[] bin2){

int[] result;
result = new int [bin1.length];

    for (int i = bin1.length - 1; i>=0; i--){
        boolean flag = false;

        if (bin1[i] - bin2[i] == 0){
            result[i] = 0;
        }
        else if (bin1[i] - bin2[i] == 1){
            result[i] = 1;
        }
        else{
            bin1[i-1]--;
            bin1[i] = bin1[i] + 2;
            i++;
        }
    }
return result;

}

public static int[] binary(int n)
{
    int[] bin = new int[16];
    int ctr = 15;
    int num = n;
    /** complemento de 2 para negativos **/
    if (n < 0)
        num = 65536 + n;
    while (num != 0)
    {
        bin[ctr--] = num % 2;
        num /= 2;
    }
    return bin;
}

public static int divide(int[] bin1,int[] bin2){
    int cont = 0;
        for(int i=0;i<bin2.length-1;i++){
            while(bin1[i]>bin2[i]){
                subtrair(bin1,bin2);
                cont++;
            }
        }
    return cont;
}



public static void main(String[]args){

    Scanner scan = new Scanner(System.in);
    System.out.println("Algoritmo para divisão de binarios\n");
    Divisao d = new Divisao();

    /** Entrar dois números decimais **/
    System.out.println("Entre dois numeros decimais\n");
    int n1 = 0;
    int n2 = 0;
    int[] result;

    try{
        n1 = scan.nextInt();
        n2 = scan.nextInt();
    }
    catch(Exception e){
        System.out.println("Por favor, entre um numero entre -2,147,483,648 e 2,147,483,647");
        return;
    }


        int[] b1 = binary(n1);
        int[] b2 = binary(n2);
        result = d.divide(b1, b2);



    System.out.println ("fodeu");       
}
}

当我编译它时,它说&#34; b1&#34; (来自n1的二进制数组)是INT,参数需要INT []

HEELP

2 个答案:

答案 0 :(得分:3)

你的divide()函数返回一个int,而不是一个数组。您正试图将其存储在int []数组变量(结果)中。

答案 1 :(得分:0)

divide函数返回一个int,你试图将它存储在一个int数组中,你需要将它存储为int。