函数不会返回我需要的值

时间:2018-01-27 16:04:52

标签: java function return

我有一个函数,其中数组中的第一个值需要确定一个int变量,我试图返回到main函数,以便将结果存储为变量。但是,执行此操作后,变量的输出始终为0(这就是我设置它以便初始化函数中的变量),即使array [0]符合函数中列出的条件。这是代码......

    public static int findWhichLocation (int array []){
        int location = 0;
        if (array[0]>99 && array[0]<200){
            location = 1;
        }
        else if (array[0]>199 && array[0]<300){
            location = 2;
        }
        else if (array[0]>299 && array[0]<=400){
            location = 3;
        }
        else if (array[0]>399 && array[0]<500){
            location = 4;
        }
        else if (array[0]>499 && array[0]<600){
            location = 5;
        }
        return location;
    }

我该怎么做才能确保函数返回我希望它返回的int,而不仅仅是我用来初始化变量的int ???

编辑: 而不是使用变量位置,我现在只是返回数字而不是变量。

    public static int findWhichLocation (int array []){
    if (array[0]>99 && array[0]<200){
        return 1;
    }
    else if (array[0]>199 && array[0]<300){
        return 2;
    }
    else if (array[0]>299 && array[0]<=400){
        return 3;
    }
    else if (array[0]>399 && array[0]<500){
        return 4;
    }
    else if (array[0]>499 && array[0]<600){
        return 5;
    }

    }

但是现在我收到一个错误,说该方法需要返回int类型,但我返回一个int,但我不知道为什么它不起作用。我该怎么做?

3 个答案:

答案 0 :(得分:0)

我使用此代码测试了您的方法,一切正常。您应该仔细检查数组值。

public class main {
    public static void main(String args[]){
            int [] a = new int [4];
            a[0] = 200;
            System.out.println( findWhichLocation(a) );
    }
    public static int findWhichLocation (int array []){
        int location = 0;
        if (array[0]>99 && array[0]<200){
            location = 1;
        }
        else if (array[0]>199 && array[0]<300){
            location = 2;
        }
        else if (array[0]>299 && array[0]<=400){
            location = 3;
        }
        else if (array[0]>399 && array[0]<500){
            location = 4;
        }
        else if (array[0]>499 && array[0]<600){
            location = 5;
        }
        return location;
    }
}

此代码输出2.

第二个代码块问题是您应该在方法结束括号之前提供return 0

public static int findWhichLocation (int array []){
    if (array[0]>99 && array[0]<200){
        return 1;
    }
    else if (array[0]>199 && array[0]<300){
        return 2;
    }
    else if (array[0]>299 && array[0]<=400){
        return 3;
    }
    else if (array[0]>399 && array[0]<500){
        return 4;
    }
    else if (array[0]>499 && array[0]<600){
        return 5;
    }
    // add this return
    return 0;
}

建议

你可以通过这种方法完成这项工作:

public static int findWhichLocation (int array []){
    return array[0] / 100;
}

答案 1 :(得分:0)

在不知道您尚未提供的数组[0]的值的情况下,无法回答您的问题。在方法的修订版中,当值<100或> = 600时,您将无法返回。如其他人所述,您的方法的两个版本都有错误,其中使用&lt; = 400而不是&lt; 400;将数组[0]赋值给一个变量,这样你就不必多次检查它会减少缺陷的可能性,例如意外编码错误的索引;并且由于您试图确定数百个范围,因此更容易除以100.此外,方法名称findByLocation无法准确描述该方法正在执行的操作。

尝试:

dependencies {
    runtime 'org.xhtmlrenderer:core-renderer:R8'
    runtime 'com.lowagie:itext:2.1.0'
}

然后用:

调用它
const algorithm = 'aes-256-gcm';
const iv = Buffer.alloc(16);
const key = Buffer.alloc(256/8);

const cipher = crypto.createCipheriv(algorithm, key, iv);
const read_stream = fs.createReadStream(path.resolve(os.homedir(), 'Desktop', 'abstract.pdf'));
const encrypted_write_stream = fs.createWriteStream(path.resolve(os.homedir(), 'Desktop', 'abstract.enc.pdf'));

cipher.on('finish', () => 
{
    const tag = cipher.getAuthTag();
    console.log('File encrypted. Tag is', tag.toString('hex'));

    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    decipher.setAuthTag(tag);

    const encrypted_read_stream = fs.createReadStream(path.resolve(os.homedir(), 'Desktop', 'abstract.enc.pdf'));
    const write_stream = fs.createWriteStream(path.resolve(os.homedir(), 'Desktop', 'abstract.decrypted.pdf'));

    decipher.on('error', console.error);
    decipher.on('finish', () => 
    {
        console.log('Decrypted successfully');
    });

    encrypted_read_stream.pipe(decipher).pipe(write_stream);
});

read_stream.pipe(cipher).pipe(encrypted_write_stream);

答案 2 :(得分:0)

第二个代码块只是在你拥有的if else梯形图之后错过了一个返回。

 public static int findWhichLocation (int array []){
if (array[0]>99 && array[0]<200){
    return 1;
}
else if (array[0]>199 && array[0]<300){
    return 2;
}
else if (array[0]>299 && array[0]<=400){
    return 3;
}
else if (array[0]>399 && array[0]<500){
    return 4;
}
else if (array[0]>499 && array[0]<600){
    return 5;
}
return 0; // added
}

你必须添加这个,因为如果你没有满足if else梯形图中的所有条件,那么该方法将不会返回任何内容。这就是为什么没有返回int的错误出现了。