创建复数类
public class K334 {
public static void main(String[] args) {
Complex co1 = new Complex(3, 5);
System.out.println(co1.GouZhaoFuShu());
Complex co2 = new Complex(3, -5);
System.out.println(co2.GouZhaoFuShu());
System.out.println(co1.FuShuJiaFa(co2));
System.out.println(co1.FuShuJianFa(co2));
System.out.println(co1.equals(co2));
System.out.println(co1.JiSuanMo());
}
}
class Complex {
int i;
int j;
Complex co3;
String fu;
public Complex(int i, int j) {
this.i = i;
this.j = j;
if (j > 0 && i > 0)
fu = i + "i" + "+" + j + "j";
else if (i != 0 && j < 0)
fu = i + "i" + j + "j";
else if (i != 0 && j == 0)
fu = i + "i";
else if (i == 0 && j != 0)
fu = j + "j";
else
fu = "0";
}
public String GouZhaoFuShu() {
return fu;
}
public String FuShuJiaFa(Complex co) {
this.co3 = new Complex(co.i + this.i, co.j + this.j);
return this.co3.fu;
}
public String FuShuJianFa(Complex co) {
this.co3 = new Complex(co.i - this.i, co.j - this.j);
return this.co3.fu;
}
@Override
public String toString() {
return "Complex [i=" + i + ", j=" + j + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Complex other = (Complex) obj;
if (i != other.i)
return false;
if (j != other.j)
return false;
return true;
}
public double JiSuanMo() {
return Math.sqrt(i * i + j * j);
}
}
声明银行账户类Account
import java.util.Scanner;
public class j_35 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请依次输入身份证号,姓名,账户号,密码,开户时间,余额");
Account acc = new Account(scan.next(),scan.next(),scan.nextInt(),scan.nextInt(),scan.next(),scan.nextDouble());
while (true) {
System.out.println("---欢迎进入银行账户操作系统---");
System.out.println("---------1银行账户信息--------");
System.out.println("---------2取款操作------------");
System.out.println("---------3存款操作------------");
System.out.println("---------4退出系统------------");
System.out.println("------------------------------");
int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("---银行账户信息---");
acc.Display();
break;
case 2:
System.out.println("---取款操作---");
acc.takeMoney();
break;
case 3:
System.out.println("---存款操作---");
acc.saveMoney(1000);
break;
case 4:
System.exit(0);
break;
default:
System.out.println("您的选择有误!");
break;
}
}
}
}
class Account {
int password, ID;
String nameId, name, time;
double money;
public Account(String nameId, String name, int ID, int password,
String time, double deposit) {
this.nameId = nameId;
this.name = name;
this.time = time;
this.ID = ID;
this.password = password;
money = deposit;
}
public void Display() {
System.out.println("账户:" + ID);
System.out.println("姓名:" + name);
System.out.println("开户时间" + time);
System.out.println("余额:" + money);
}
public void takeMoney() {
while (true) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入密码进行验证!");
int pass = scan.nextInt();
if (pass == password) {
System.out.println("请输入需要取款的金额:");
int withdrawals = scan.nextInt();
if (withdrawals <= money) {
money = money - withdrawals;
System.out.println("账户余额:" + money);
} else {
System.out.println("当前余额不足!");
}
break;
} else {
System.out.println("你输入的密码有误,请重新输入!");
}
scan.close();
}
}
public void saveMoney(int inmoney) {
money = money + inmoney;
System.out.println("此次存款为:" + inmoney);
System.out.println("账户余额:" + money);
}
}
增加功能的Student类
import java.util.ArrayList;
public class k340 {
public static void main(String[] args) {
ArrayList<Student> st = new ArrayList<Student>();
st.add(new Student("wangluo", 1, "du", "20180105", "tengzhou", 80, 90));
for (int i = 0; i < st.size(); i++) {
st.get(i).chazhaobirthiday(st, "20180105");
}
}
}
class Student {
static int wangluo1 = 20181001;
static int wangluo2 = 20182001;
static int xingong1 = 20183001;
static int xingong2 = 20184001;
String zhuanye;
int nianji;
int id;
String name;
String birthday;
String diqu;
int grade1;
int grade2;
public Student(String zhuanye, int nianji, String name, String birthday,
String diqu, int grade1, int grade2) {
this.grade1 = grade1;
this.grade2 = grade2;
this.zhuanye = zhuanye;
this.nianji = nianji;
this.name = name;
this.diqu = diqu;
this.birthday = birthday;
if ((zhuanye + nianji).equals("wangluo1")) {
id = wangluo1++;
}
if ((zhuanye + nianji).equals("wangluo2")) {
id = wangluo2++;
}
if ((zhuanye + nianji).equals("xingong1")) {
id = xingong1++;
}
if ((zhuanye + nianji).equals("xingong2")) {
id = xingong2++;
}
}
public void chazhaoname(ArrayList<Student> st, String name) {
for (int i = 0; i < st.size(); i++) {
if (st.get(i).name.substring(0, 1).equals(name)) {
System.out.println(st.get(i).name);
}
}
}
public void chazhaobirthiday(ArrayList<Student> st, String birthday) {
for (int i = 0; i < st.size(); i++) {
if (st.get(i).birthday.equals(birthday)) {
System.out.println(st.get(i).name + "," + st.get(i).birthday);
}
}
}
public void chazhaodiqu(ArrayList<Student> st, String diqu) {
for (int i = 0; i < st.size(); i++) {
if (st.get(i).diqu.equals(diqu)) {
System.out.println(st.get(i).name + "," + st.get(i).diqu);
}
}
}
public void tongji(ArrayList<Student> st) {
System.out.println("wangluo de grade");
for (int i = 0; i < st.size(); i++) {
if (st.get(i).zhuanye.equals("wangluo")) {
System.out.println(st.get(i).name + ":" + st.get(i).grade1
+ "," + st.get(i).grade2);
}
}
}
}