gpt4 book ai didi

java - 从Java方法返回后如何访问返回数据

转载 作者:行者123 更新时间:2023-12-01 19:02:22 25 4
gpt4 key购买 nike

I'm a Java newbie, and this is a real basic, basic question, so please don't miss the tree for the forest. I just need to know how to access the data coming back from the method

    private static int toInt( String number )
{
int multiplicand = 0;
// do stuff to figure out a multiplicand
return multiplicand;
}

public static void main( String[ ] args )
{
int anotherVariable = 53;
toInt( args[ 0 ] );

So, what do I say here after I've come back from the toInt method in order to access what is in multiplicand?
Lets say I want to multiply what came back in multiplicand * anotherVariable. I cannot use multiplicand because the editor just says "cannot resolve multiplcand to a variable."

Thank you in advance to all the great programmers with the patience to tolerate us newbies.

最佳答案

将函数调用的结果分配给一个变量,然后使用它。

int result = toInt("1");

您现在可以根据需要使用结果;它包含您在方法中返回的任何内容,即被乘数的值。因此,就您而言,您现在可以这样做

int anotherResult = 结果 * anotherVariable;

请注意,您也可以这样做

int anotherResult = toInt(args[0]) * anotherVariable;

无需将 toInt 调用的结果分配给中间变量。

我更喜欢将函数调用分配给变量;使调试更容易。如果您想多次使用结果,那么使用变量几乎总是更好。

关于java - 从Java方法返回后如何访问返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11751290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com