Мне нужно было создать код, определяющий, как выполнять основные математические операции с комплексными числами. Я сделал один класс, чтобы определить, как выполнять эти операции и тестовый класс для запуска кода.Компилятор печатает неправильный ответ - Java
Вот код для определения операции:
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
.
Этот вопрос был вызван проблемой, которая больше не может быть воспроизведена или простой типографской ошибкой. Хотя подобные вопросы могут быть по-теме здесь, этот вопрос был разрешен таким образом, который вряд ли поможет будущим читателям. Этого часто можно избежать путем выявления и тщательного изучения кратчайшей программы, необходимой для воспроизведения проблемы до публикации. –
Какова цель 'float a; float b; 'когда у вас уже есть' float real; float imaginary; '? – Pshemo
Итак, что вы сделали для его отладки? Вы уже печатали значения tempA, tempB, tempC, tempD? –