Java面向对象6(AA ~ AE)

AE  简单的复数运算(类和对象) (SDUT 4303)

import java.util.*;

class Complex {
	int a, b;

	Complex() {
	}

	Complex(int n, int m) {
		a = n;
		b = m;
	}

	void getAns(int x, int y, int z) {
		if (z == 1) {
			a += x;
			b += y;
		} else if (z == 2) {
			a -= x;
			b -= y;
		} else if (z == 3) {
			int temp = a;
			a = a * x - b * y;
			b = temp * y + b * x;
		}
	}

	void Print() {
		System.out.println(a + " " + b);
	}
}

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int x, y, z;
		Complex p = new Complex(sc.nextInt(), sc.nextInt());
		while (sc.hasNext()) {
			x = sc.nextInt();
			y = sc.nextInt();
			z = sc.nextInt();
			if (x == 0 && y == 0 && z == 0) {
				p.Print();
				break;
			} else
				p.getAns(x, y, z);
			// p.Print();
		}

	}
}
原文地址:https://www.cnblogs.com/lcchy/p/10139462.html