//本代码用Java语言实现了复数类,并且可对你刚刚输入的复数(实部和虚部)进行修改(可以循环的输入修改), //之后实现基本的加减乘除运算,这个功能是对你输入的(+,-,*,/)符号做出相应的判断,可以循环的进行。 //(初学者仅供参考) import java.io.BufferedReader; import java.io.InputStreamReader; public class ComplexNumber { private double real; private double virtual; //构造函数 ComplexNumber() { real = getValue(); virtual = getValue(); } //得到数值 public double getValue() { return inputValue(); } //输入数值 public double inputValue() { double value = 0.0; try { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String s=in.readLine(); value = Double.parseDouble(s); }catch (Exception e){} return value; } //显示复数 public void display() { System.out.println("The complex number is " + real + " + " + virtual + " i/n" + "It's real part is " + real + "/nIt's virtual part is " + virtual); } //修改属性 public void change() { String string =""; System.out.println("The complex number is " + real + " + " + virtual + " i"); System.out.println("Do you want to change the complex number?" + " y is yes and n is no."); try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); string = s; }catch (Exception e){} while(string.equalsIgnoreCase("y"))//如果是string和y相等则循环输入 //注意这里不能直接让string == y 要用字符串的equal函数 { System.out.println("Enter the complex number again:"); real = getValue(); virtual = getValue(); System.out.println("Do you want to change the complex number?" + " y is yes and n is no."); try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); string = s; }catch (Exception e){} } display(); System.out.println("Continue! Enter another complex number."); } //加法 public void addition(ComplexNumber n1, ComplexNumber n2) { double newReal = 0.0; double newVirtual = 0.0; newReal = n1.real + n2.real; newVirtual = n1.virtual + n2.virtual; System.out.println("number1 + number2 = " + newReal + " + " + newVirtual + " i " ); } //减法 public void subtraction(ComplexNumber n1, ComplexNumber n2) { double newReal = 0.0; double newVirtual = 0.0; newReal = n1.real - n2.real; newVirtual = n1.virtual - n2.virtual; System.out.println("number1 - number2 = " + newReal + " + " + newVirtual + " i " ); } //乘法 public void multiplication(ComplexNumber n1, ComplexNumber n2) { double newReal = 0.0; double newVirtual = 0.0; newReal = n1.real * n2.real - n1.virtual * n2.virtual; newVirtual = n1.real * n2.virtual + n1.virtual * n2.real; System.out.println("number1 * number2 = " + newReal + " + " + newVirtual + " i " ); } //除法 public void division(ComplexNumber n1, ComplexNumber n2) { double newReal = 0.0; double newVirtual = 0.0; newReal = (n1.real * n2.real + n1.virtual * n2.virtual) /(n2.real * n2.real + n2.virtual * n2.virtual ); newVirtual = (n1.virtual * n2.real-n1.real * n2.virtual) /(n2.real * n2.real + n2.virtual * n2.virtual ) ; System.out.println("number1 / number2 = " + newReal + " + " + newVirtual + " i " ); } public static void main(String[] args) { System.out.println("Please enter the value of the real and the virtual"); ComplexNumber number1 = new ComplexNumber(); number1.change(); ComplexNumber number2 = new ComplexNumber(); number2.change(); String operator = "";//记录所输入的运算符 String string = "";//记录是否要进行相应的运算(+,-,*,/) System.out.println("Do you want to calculate these two " + "complex numbers? y is yes and n is no"); try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); string = in.readLine(); }catch( Exception e){} //如果同意进行计算,则输入计算符号 if(string.equalsIgnoreCase("y")) { System.out.println("Input the operator(+,-,*,/) of you want to calculate:"); try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); operator = in.readLine(); }catch( Exception e){} } //按照所输入的符号进行计算,并且可行实现循环不同的运算 while(string.equalsIgnoreCase("y")) { if(operator.equalsIgnoreCase("+")) number1.addition(number1, number2); else if(operator.equalsIgnoreCase("-")) number1.subtraction(number1, number2); else if(operator.equalsIgnoreCase("*")) number1.multiplication(number1, number2); else if(operator.equalsIgnoreCase("/")) number1.division(number1, number2); System.out.println("Do you want to calculate these two " + "complex numbers again? y is yes and n is no"); try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); string = in.readLine(); }catch( Exception e){} if(string.equalsIgnoreCase("y")) { System.out.println("Input the operator(+,-,*,/) of you want to calculate:"); try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); operator = in.readLine(); }catch( Exception e){} } } System.out.println("Goodbye!"); } }