练习1:创建学员类和教员类
需求说明 编写学员类,输出学员相关信息 编写教员类,输出教员相关信息
package diyizhang;
/**
*学员类
*/
public class Student {
String name ;
int age ;
String bj ;
String aih;
public void student1() {
System.out.println(name);
System.out.println("年龄:"+age);
System.out.println("就读于:"+bj);
System.out.println("爱好:"+aih);
}
}
package diyizhang;
/**
*教师类
*/
public class Teacher {
String name ;
int age ;
String bj ;
String aih;
public void teacher1() {
System.out.println(name);
System.out.println("专业放向:"+bj);
System.out.println("教课课程:"+aih);
System.out.println("教龄:"+age);
}
}
package diyizhang;
public class Lianxi1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student me = new Student();
me.name = "张浩";
me.age = 10;
me.bj = "S1班";
me.aih = "篮球";
me.student1();
Teacher me1 = new Teacher();
me1.name = "王老师";
me1.age = 5;
me1.bj = "计算机";
me1.aih = "使用Java语言理解程序逻辑";
me1.teacher1();
}
}
运行:
张浩
年龄:10
就读于:S1班
爱好:篮球
王老师
专业放向:计算机
教课课程:使用Java语言理解程序逻辑
教龄:5
练习2:用户密码管理
需求说明 模拟实现用户密码管理:输入旧的用户名和密码,如果正确,方有权限更新;从键盘获取新的密码,进行更新
package diyizhang;
import java.util.Scanner;
public class Password {
Scanner sc = new Scanner(System.in);
public String name = "111" ;//账号
public int password = 0000 ;//密码
public void password1() {
System.out.println("请输入新密码:");
int so = sc.nextInt();
System.out.println("修改密码成功,您的新密码为:"+so);
}
}
package diyizhang;
import java.util.Scanner;
public class Lianxi2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Password user = new Password();
System.out.println("请输入用户名:");
String a = sc.next();
System.out.println("请输入密码:");
int i = sc.nextInt();
if(user.name.equals(a) && i==user.password) {
user.password1();
}else {
System.out.println("用户名和密码不匹配!您没有权限更新管理员信息。");
}
}
}
运行1:
请输入用户名:
121
请输入密码:
123
用户名和密码不匹配!您没有权限更新管理员信息。
运行2:
请输入用户名:
111
请输入密码:
000
请输入新密码:
1111
修改密码成功,您的新密码为:1111
1.猜数字游戏:一个类A有两个成员变量v、num,v有一个初值100。定义一个方法guess,对A类的成员变量v,用num进行猜。如果大了则提示大了,小了则提示小了。等于则提示猜测成功。
package diyizhang;
public class Mumber {
int v = 100;
int num;
public void guess() {
System.out.println(v>num?"小了":"大了");
}
}
package diyizhang;
import java.util.Scanner;
public class Lianxi3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(int i = 0 ;i>=0;i++) {
Mumber s = new Mumber();
s.num = sc.nextInt();
if(s.v==s.num) {
System.out.println("猜测成功");
break;
}else {
s.guess();
}
}
}
}
运行:
1
小了
50
小了
80
小了
111
大了
101
大了
100
猜测成功
2.类的成员变量 请定义一个交通工具(Vehicle)的类其中有:
属性速度(speed)体积(size)等等 方法移动(move())设置速度(setSpeed(int speed))加速speedUp(),
减速speedDown()等等. 最后在测试类Vehicle中的main()中实例化一个交通工具对象并通过方法给它初始化speed,
size的值并且通过打印出来。另外调用加速减速的方法对速度进行改变
public class Vehicle {
Scanner sc = new Scanner(System.in);
public int speed = 10;
public int size = 20;
public void move() {
setSpeed();
speedUp();
speedDown();
}
public void setSpeed() {
System.out.println("初始速度:"+speed);
System.out.println("体积:"+size);
}
public void speedUp() {
System.out.print("加速:");
int a = sc.nextInt();
System.out.println("加速后的速度:"+(speed+a));
}
public void speedDown() {
System.out.print("减速:");
int a = sc.nextInt();
System.out.println("减速后的速度:"+(speed-a));
}
}
package diyizhang;
public class Lianxi4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Vehicle m = new Vehicle();
m.move();
}
}
运行:
初始速度:10
体积:20
加速:12
加速后的速度:22
减速:2
减速后的速度:8
3.定义计算器类,成员属性num1和num2,定义行为加,减,乘,除进行计算,根据用户输入,返回计算结果。
package diyizhang;
import java.util.Scanner;
public class Counter {
Scanner sc = new Scanner(System.in);
public double num1;
public double num2;
public void result() {
num1 = sc.nextDouble();
String s = sc.next();
num2 = sc.nextDouble();
if(s.equals("+")) {
System.out.print("="+(num1+num2));
}else if(s.equals("-")) {
System.out.print("="+(num1-num2));
}else if(s.equals("*")) {
System.out.print("="+(num1*num2));
}else {
System.out.print("="+(num1/num2));
}
}
}
package diyizhang;
public class Lianxi5 {
public static void main(String[] args) {
Counter a = new Counter();
a.result();
}
}
运行:
1
+
1
=2.0
2
*
2
=4.0
4.定义Penguin企鹅类,属性health健康值,sex性别,love亲密度,创建两个企鹅并实现企鹅的自我介绍,
并实现喂食,投递不同的食物增加不同的亲密度。
package diyizhang;
public class Penguin {
public int health;
public char sex;
public int love;
public String name;
public void intimacy() {
System.out.println("企鹅姓名:"+name);
System.out.println("企鹅性别:"+sex);
System.out.println("企鹅健康值:"+health);
System.out.println("企鹅亲密度:"+love);
}
}
package diyizhang;
import java.util.Scanner;
public class Lianxi6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Penguin qq = new Penguin();
qq.name = "xxx";
qq.health = 100;
qq.love = 50;
qq.sex = '雄';
Penguin q = new Penguin();
q.name = "mmm";
q.health = 88;
q.love = 66;
q.sex = '雌';
for(int i = 0; i>=0;i++) {
System.out.print("第一只企鹅1,第二只企鹅2 ,否则结束");
int d = sc.nextInt();
if(d==1) {
qq.intimacy();
for(int j = 0;j>=0;j++) {
System.out.println("投喂1、2亲密度健康值会改变");
int w = sc.nextInt();
qq.health--;
if(w==1) {
qq.love++;
qq.intimacy();
}else if(w==2) {
qq.love=100;
qq.health = 100;
qq.intimacy();
break;
}
}
}
if(d==2) {
qq.intimacy();
for(int j = 0;j>=0;j++) {
System.out.println("投喂1、2亲密度健康值会改变");
int w = sc.nextInt();
q.health--;
if(w==1) {
q.love++;
q.intimacy();
}else if(w==2) {
q.love=100;
q.health = 100;
q.intimacy();
break;
}
}
}else {
break;
}
}
}
}
运行:
第一只企鹅1,第二只企鹅2 ,否则结束1
企鹅姓名:xxx
企鹅性别:雄
企鹅健康值:100
企鹅亲密度:50
投喂1、2亲密度健康值会改变
1
企鹅姓名:xxx
企鹅性别:雄
企鹅健康值:99
企鹅亲密度:51
投喂1、2亲密度健康值会改变
2
企鹅姓名:xxx
企鹅性别:雄
企鹅健康值:100
企鹅亲密度:10