我必须编写一个代码来定义复杂数字的基本数学运算。我创建了一个类来定义如何执行这些操作,并创建一个测试类来运行代码。
以下是定义操作的代码:
import java.util.Scanner;
public class ComplexNumber{
float real;
float imaginary;
private float a;
private float b;
public ComplexNumber(float a, float b){
real = a;
imaginary = b;
}
public ComplexNumber add(ComplexNumber otherNumber){
ComplexNumber newComplex;
float newA = a + otherNumber.getA();
float newB = b + otherNumber.getB();
newComplex = new ComplexNumber(newA, newB);
return newComplex;
}
public ComplexNumber minus(ComplexNumber otherNumber){
ComplexNumber newComplex;
float newA = a - otherNumber.getA();
float newB = b - otherNumber.getB();
newComplex = new ComplexNumber(newA, newB);
return newComplex;
}
public ComplexNumber times(ComplexNumber otherNumber){
ComplexNumber newComplex;
float newA = (a * otherNumber.getA()) - (b * otherNumber.getB());
float newB = (b * otherNumber.getB()) - (a * otherNumber.getA());
newComplex = new ComplexNumber(newA, newB);
return newComplex;
}
public ComplexNumber divide(ComplexNumber otherNumber){
ComplexNumber newComplex;
float newA = (a * otherNumber.getA()) + (b*otherNumber.getB());
float newB = (otherNumber.getA() * otherNumber.getA()) + (otherNumber.getB() * otherNumber.getB());
float newC = (b * otherNumber.getA()) + (a * otherNumber.getB());
float newD = (otherNumber.getA() * otherNumber.getA()) + (otherNumber.getB() * otherNumber.getB());
float firstFraction = (newA / newB);
float secondFraction = (newC / newD);
newComplex = new ComplexNumber(firstFraction, secondFraction);
return newComplex;
}
public float getA(){
return a;
}
public float getB(){
return b;
}
public String toString(){
return a + " + " + b + "i";
}
}
这是我的测试课程:
import java.util.Scanner;
class ComplexCalculator{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
float tempA, tempB, tempC, tempD;
ComplexNumber first, second, result;
System.out.println("Choose an operation: \n1. Addition \n2. Subtraction \n3. Multiplication \n4. Division \n5. Quit");
int selection = myScanner.nextInt();
if (selection == 1) {
System.out.println("Enter A: ");
tempA = myScanner.nextFloat();
System.out.println("Enter B: ");
tempB = myScanner.nextFloat();
System.out.println("Enter C: ");
tempC = myScanner.nextFloat();
System.out.println("Enter D: ");
tempD = myScanner.nextFloat();
first = new ComplexNumber(tempA, tempB);
second = new ComplexNumber(tempC, tempD);
result = first.add(second);
System.out.println(result.toString());
}
if (selection == 2) {
System.out.println("Enter A: ");
tempA = myScanner.nextFloat();
System.out.println("Enter B: ");
tempB = myScanner.nextFloat();
System.out.println("Enter C: ");
tempC = myScanner.nextFloat();
System.out.println("Enter D: ");
tempD = myScanner.nextFloat();
first = new ComplexNumber(tempA, tempB);
second = new ComplexNumber(tempC, tempD);
result = first.minus(second);
System.out.println(result.toString());
}
if (selection == 3) {
System.out.println("Enter A: ");
tempA = myScanner.nextFloat();
System.out.println("Enter B: ");
tempB = myScanner.nextFloat();
System.out.println("Enter C: ");
tempC = myScanner.nextFloat();
System.out.println("Enter D: ");
tempD = myScanner.nextFloat();
first = new ComplexNumber(tempA, tempB);
second = new ComplexNumber(tempC, tempD);
result = first.times(second);
System.out.println(result.toString());
}
if (selection == 4) {
System.out.println("Enter A: ");
tempA = myScanner.nextFloat();
System.out.println("Enter B: ");
tempB = myScanner.nextFloat();
System.out.println("Enter C: ");
tempC = myScanner.nextFloat();
System.out.println("Enter D: ");
tempD = myScanner.nextFloat();
first = new ComplexNumber(tempA, tempB);
second = new ComplexNumber(tempC, tempD);
result = first.divide(second);
System.out.println(result.toString());
}
if (selection == 5){
System.out.println("Thank you!");
return;
}
}
}
每当我运行我的代码时,我会输入四个数字,但它总是打印出来0.0 + 0.0i
。
答案 0 :(得分:1)
当您指定了两个成员
时,会出现此问题private float a;
private float b;
,您的toString()
定义为
public String toString(){
return a + " + " + b + "i";
}
然而,您的代码中没有任何地方设置a
或b
。
尝试使用real
和imaginary
替换a和b。