h*******9 发帖数: 68 | 1 刚刚开始学java,这周就要交网上作业,可是有个地方怎么试都不对,请各位好心人帮
帮忙!
我写了一个getFNSN(FN,SN);, 来模块化我的代码,可是应用起来下一行出来的总是原
始值,而不是用户的输入值,试来试去不知道怎样改才好。急死人了!
Result of adding 0.00 and 0.00 is 0.00.
部分相关代码如下:
{
public static void main(String[] args) {
float FN=0;
float SN=0;
{out.printf( "Please enter two numbers to add, separated by a space: ");
getFNSN(FN,SN);
out.printf("Result of adding %5.2f and %5.2f is %5.2f.n",FN, SN, FN+ SN);
}
static void getFNSN (float fn, float sn){
do {Scanner In = new Scanner(System.in);
try {
fn = In.nextFloat();
sn = In.nextFloat();
break;
}
catch (final InputMismatchException e) {
out.println("You have entered an invalid choice. Try again."
);
In.nextLine();
continue;
}
} while (true);}
}
结果:
What would you like to do? 1
Please enter two numbers to add, separated by a space: 2 3
Result of adding 0.00 and 0.00 is 0.00.
Press enter key to continue … | k**********g 发帖数: 989 | 2 float is a primitive type. It is called-by-value, so the caller's float
variable cannot be updated by the function even if the function modifies it.
(The function is modifying a copy of the value, not the caller's value.)
You can return the two values as a new float[2], a tuple, or a data object
that contains two float fields.
(A data object would be a reference type, which means that if the function
modify the data object's value, it will be seen by the caller.) | h*******9 发帖数: 68 | 3 Thank you for your answer! I got it a little bit. Could you give me an
example?
Thank you again!
it.
【在 k**********g 的大作中提到】 : float is a primitive type. It is called-by-value, so the caller's float : variable cannot be updated by the function even if the function modifies it. : (The function is modifying a copy of the value, not the caller's value.) : You can return the two values as a new float[2], a tuple, or a data object : that contains two float fields. : (A data object would be a reference type, which means that if the function : modify the data object's value, it will be seen by the caller.)
| h*******9 发帖数: 68 | 4 我按照你说的又试了几次,终于弄出来了~~谢谢!
it.
【在 k**********g 的大作中提到】 : float is a primitive type. It is called-by-value, so the caller's float : variable cannot be updated by the function even if the function modifies it. : (The function is modifying a copy of the value, not the caller's value.) : You can return the two values as a new float[2], a tuple, or a data object : that contains two float fields. : (A data object would be a reference type, which means that if the function : modify the data object's value, it will be seen by the caller.)
|
|