gpt4 book ai didi

java - 可变长度参数列表, "user-defined type"搞乱了参数

转载 作者:行者123 更新时间:2023-11-30 04:13:51 25 4
gpt4 key购买 nike

自从提出我的第一个问题以来,我在过去的两周里已经取得了很大的进步,我已经编写了一个井字游戏,使用网格布局和按钮按下来制作 x 和 o 并声明获胜者和执行复数算术的类< em>(例如,通过 cAdd(z1,z2) z1=-3-4i 加 z2=7+i) 并正确计算(例如 sin(z1) 和 log(z2))。 (由名为 Derive 的 CAS 确认并通过帮助实现。)

到目前为止没有问题。

因此,我决定增强 cAdd 以接受两个以上的复数(例如,cAdd(z1,z2,z3)),并发现以下可变长度参数列表的语法:

public ComplexNumber cAdd ( ComplexNumber ... a);

但是,虽然 cAdd(z1,z2) 的双参数版本工作正常,但“可变参数”版本(例如,cAdd(z1,z2,z3))给出了正确的结果,但更改了参数值,因此如果 z1 是在以后的计算中使用,它不是最初的样子。

这是代码,后面是输出。

  class ComplexNumber {

double real, imag;

public ComplexNumber() { // default constructor
}

public ComplexNumber(double a, double b) { // initializer constructor
real = a;
imag = b;
}

public class CBug {

public static void cPrint(String s, ComplexNumber z) {
System.out.println(s + z.real + " + " + z.imag + "i");
}

public static void dump(int n, ComplexNumber z, ComplexNumber [] a){
System.out.print("Dump " + n + "; sum so far is " );
cPrint("z = ", z);
for (int i = 0; i < a.length; i++)
cPrint("" + i + ": " ,a[i]);
System.out.println("");
}

public static ComplexNumber cAdd(ComplexNumber ... a) {
ComplexNumber z = a[0];
for (int i = 1; i < a.length; i++){
dump(i,z,a);
z.real = z.real + a[i].real;
z.imag = z.imag + a[i].imag;
}
dump(99,z,a);
return z;
}

public static void main(String[] args) {
ComplexNumber sum = new ComplexNumber();
ComplexNumber z1 = new ComplexNumber(1000,500);
ComplexNumber z2 = new ComplexNumber(7, 1);
ComplexNumber z3 = new ComplexNumber(100,100);
cPrint("z1 = ", z1);
cPrint("z2 = ", z2);
cPrint("z3 = ", z3);

System.out.println("");

System.out.println("++++++++++++++++++++++");
sum = cAdd(z1, z2, z3) ;
cPrint ("z1 + z2 + z3 = " , sum);
System.out.println("--------------------");

cPrint("z1 = ", z1);
cPrint("z2 = ", z2);
cPrint("z3 = ", z3);
}
}

输出:

run:
z1 = 1000.0 + 500.0i
z2 = 7.0 + 1.0i
z3 = 100.0 + 100.0i

++++++++++++++++++++++
Dump 1; sum so far is z = 1000.0 + 500.0i [correct]
0: 1000.0 + 500.0i
1: 7.0 + 1.0i
2: 100.0 + 100.0i

Dump 2; sum so far is z = 1007.0 + 501.0i [correct]
0: 1007.0 + 501.0i [but WHY DID ARG[0] CHANGE to "z so far"?]
1: 7.0 + 1.0i
2: 100.0 + 100.0i

Dump 99; sum so far is z = 1107.0 + 601.0i [correct]
0: 1107.0 + 601.0i [WHY DID ARG[0] CHANGE AGAIN to new "z so far"?]
1: 7.0 + 1.0i
2: 100.0 + 100.0i

z1 + z2 + z3 = 1107.0 + 601.0i [correct]
--------------------
z1 = 1107.0 + 601.0i [WHY DID z1 (a.k.a. arg[0]) CHANGE?]
z2 = 7.0 + 1.0i
z3 = 100.0 + 100.0i
BUILD SUCCESSFUL (total time: 0 seconds)

无法在后续计算中使用第一个参数,因为 cAdd 损坏了它。

==============================

这是我现在对 cAdd 的了解(因为我最初的“修复”[在下面的评论中描述]有点古怪]:

 public static ComplexNumber cAdd(ComplexNumber ... a) {
ComplexNumber z = new ComplexNumber();
for (int i = 0; i < a.length; i++){
z.real += a[i].real;
z.imag += a[i].imag;
}
return z;
}

=================================

最佳答案

当您在 cAdd 中创建变量 z 时,您将其引用到 a[0]。现在,za[0] 都引用同一个对象。然后你改变它。

您应该做的是复制 z 的第一个元素,这样当您修改 z 时,就不会修改 a[0 ]。像这样的事情:

ComplexNumber z = new ComplexNumber(a[0].real, a[0].imag);

关于java - 可变长度参数列表, "user-defined type"搞乱了参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18926687/

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