输入整数输出复数JAVA_Java小程序:输入一组复数并打印出来

程序运行截图:

0818b9ca8b590ca3270a3433284dd417.png

代码:

复数类:

/**

* 定义复数类

*

*

*/

public class ComplexNumbe {

double realNumber, imaginaryNumbe;// 定义实数,虚数

public ComplexNumbe(double realNumber, double imaginaryNumb) {

this.realNumber = realNumber;

this.imaginaryNumbe = imaginaryNumb;

}

public ComplexNumbe(double realNumber) {

this(realNumber, 0);

}

public ComplexNumbe() {

this(0, 0);

}

public String toString() {

if (realNumber == 0 && imaginaryNumbe == 0) {

return "0";

} else if (imaginaryNumbe == 0) {

return realNumber + "";

} else if (realNumber == 0) {

return "" + imaginaryNumbe + "i";

} else {

return "(" + realNumber + " + " + imaginaryNumbe + "i" + ")";

}

}

public ComplexNumbe(ComplexNumbe complexNumbe) {

this(complexNumbe.realNumber, complexNumbe.imaginaryNumbe);

}

}测试类:

public class Test {

public static final void main(String[] args) {

System.out.println("该程序接受用户输入的一组复数并答应出来");

System.out.println("输入复数的个数:");

Scanner scanner = new Scanner(System.in);

int num = Integer.valueOf(scanner.nextLine());

int i = 0;

List listC = new ArrayList();

while (num != 0) {

ComplexNumbe cNum = null;

System.out.print("输入第 " + ++i + " 个复数");

System.out.print("输入实数:");

scanner = new Scanner(System.in);

int real = Integer.valueOf(scanner.nextLine());

System.out.print("输入虚数:");

scanner = new Scanner(System.in);

int im = Integer.valueOf(scanner.nextLine());

if (real == 0 && im == 0) {

cNum = new ComplexNumbe();

} else if (im == 0) {

cNum = new ComplexNumbe(real);

} else {

cNum = new ComplexNumbe(real, im);

}

listC.add(cNum);

--num;

}

for (ComplexNumbe cNumbe : listC) {

System.out.println("复数:" + cNumbe.toString());

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值