m0_74028958 2023-04-01 21:12 采纳率: 100%
浏览 11
已结题

java自定义类本身在println怎么输出

#不知道怎么输出类
#【输入示例】
2 3.08 -2.04 5.06
【输出示例】
(2.0+3.1i) + (-2.0+5.1i) = 8.1i
(2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
(2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
(2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    double a1 = in.nextDouble();
    double b1 = in.nextDouble();
    double a2 = in.nextDouble();
    double b2 = in.nextDouble();
    Complex a = new Complex(a1, b1);
    Complex b = new Complex(a2, b2);
    Complex c = new Complex();
    System.out.print(a + " + " + b + " = ");
    c = a.add(b);
    c.print();
    System.out.print(a + " - " + b + " = ");
    c = a.sub(b);
    c.print();
    System.out.print(a + " * " + b + " = ");
    c = a.mul(b);
    c.print();
    System.out.print(a + " / " + b + " = ");
    c = a.div(b);
    c.print();
    in.close();
    }    
}

```java
class Complex {
    private double a,b;
    public Complex() {
        a=0;
        b=0;
    }
    public Complex(double q,double p) {
        a=q;
        b=p;
    }
    public double geta() {
        return a;
    }
    public double getb() {
        return b;
    }
    public void print() {
        if(a>0) {
            if(b>0) {
                System.out.printf("%.1f%.1fi/n",Math.abs(a),Math.abs(b));
            }
            else if(b==0) {
                System.out.printf("%.1f/n",Math.abs(a));
            }
            else {
                System.out.printf("%.1f-%.1fi/n",Math.abs(a),Math.abs(b));
            }
            
        }
        else if(a==0) {
            if(b>0) {
                System.out.printf("%.1fi/n",Math.abs(b));
            }
            else if(b==0) {
                System.out.printf("0.0/n");
            }
            else {
                System.out.printf("-%.1fi/n",Math.abs(b));
            }
        }
        else {
            if(b>0) {
                System.out.printf("-%.1f%.1fi/n",Math.abs(a),Math.abs(b));
            }
            else if(b==0) {
                System.out.printf("-%.1f/n",Math.abs(a));
            }
            else {
                System.out.printf("-%.1f-%.1fi/n",Math.abs(a),Math.abs(b));
            }
        }
        
    }
    public Complex add (Complex c) {
        Complex d=new Complex(a+c.geta(),b+c.getb());
        return d;
    }
    public Complex sub (Complex c) {
        Complex d=new Complex(a-c.geta(),b-c.getb());
        return d;
    }
    public Complex mul (Complex c) {
        Complex d=new Complex(a*c.geta()-(b*c.getb()),a*c.getb()+(b*c.geta()));
        return d;
    }
    public Complex div (Complex c) {
        Complex d=new Complex((a*c.geta()+(b*c.getb()))/(c.geta()*c.geta()+c.getb()*c.getb()),(a*c.getb()-(b*c.geta()))/(c.geta()*c.geta()+c.getb()*c.getb()));
        return d;
    }

}

```

  • 写回答

2条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-04-01 21:16
    关注

    可以重写 toString方法,自定义输出

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 创建了问题 4月1日

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择