Topic: 关于编写一个复数类 |
Print this page |
1.关于编写一个复数类 | Copy to clipboard |
Posted by: EchizenSyusuke Posted on: 2007-04-08 20:14 我想问一下,要编写一个复数类,并实现其加减法的演示操作,应该怎么写? 我写了一段加法的代码,如下: import javax.swing.JOptionPane; public class complex //定义复数类 { public static void main( String args[] ) public void add(int shi,int xu) { int a,b,i; //前两个数用来记录实部跟虚部。 for(i=1;i<=2;i++) { ShiNumber = JOptionPane.showInputDialog("请输入第"i"个数的实部:"); XuNumber = JOptionPane.showInputDialog("请输入第"i+1"个数的虚部:"); a = Integer.parseInt(ShiNumber); b = Integer.parseInt(XuNumber); a=a+a; b=b+b; } JOptionPane.showMessageDialog(null, "此次的复数相加的结果是:z=" + a+b+"i"); } 但是有好多错误,我是个新手,希望大家能帮下忙,谢谢。 |
2.Re:关于编写一个复数类 [Re: EchizenSyusuke] | Copy to clipboard |
Posted by: denghouming Posted on: 2007-04-22 18:36 //如果这是作业的话以后不要来问了,要自己去写,这次是例外哦!! class Complex { public static void main (String [] args) { Complex c1=new Complex(3,4); Complex c2=new Complex(1,2); Complex c3=c1.addComplex(c2); System.out.println(c3.toString(c3)); } int realpart; int imaginpart; public Complex() { } public Complex(int r,int i) { realpart=r; imaginpart=i; } Complex addComplex (Complex com) { int real=realpart-com.realpart; int imagin=imaginpart-com.imaginpart; return new Complex(real,imagin); } public String toString(Complex c) { String temp; temp = c.realpart + "+" + c.imaginpart + "i"; return temp; } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |