java训练11-20
新手练习,望大家多多指教
再小的帆也能远航
练习11
三个类分别是银行卡 类 ,功能类 ,测试类 。属性 用户名 密码 余额功能 类 登录 显示余额, 存 取关系 功能类中成员变量是银行卡类。
package lianxi;
import java.util.Scanner;
public class demo11 {
/*
三个类分别是银行卡 类 ,功能类 ,测试类 。
银行卡属性 用户名 密码
余额功能类: 登录 显示余额, 存和取关系,功能类中成员变量是银行卡类。
*/
public static void main(String[] args) {
Function function = new Function();
function.login();
}
public static class Card {
String username;
String password;
Double money;
public Card() {
}
public Card(String username, String password, Double money) {
this.username = username;
this.password = password;
this.money = money;
}
public Card(String username, String password) {
this.username = username;
this.password = password;
}
}
//功能类
public static class Function {
Card card = new Card("张三", "111111", 500.0);
Scanner scanner = new Scanner(System.in);
//登录
public void login() {
System.out.println("请输入用户名");
String usernameIn = scanner.next();
System.out.println("请输入密码");
String passwordIn = scanner.next();
//进行判断,如果正确登陆成功
if (card.username.equals(usernameIn) && card.password.equals(passwordIn)) {
mainMenu();
} else {
//登陆失败,用户名或密码错误
System.out.println("登陆失败,用户名或密码错误 ");
}
}
//主菜单
public void mainMenu() {
System.out.println("欢迎使用建行ATM");
System.out.println("1.查看余额");
System.out.println("2.存钱");
System.out.println("3.取钱");
System.out.println("请输入数字 1.查看余额 2.存钱 3.取钱 0.会返回登录");
System.out.println("-------------");
int i = scanner.nextInt();
//判断
if (i == 1) {
selectMoney();
} else if (i == 2) {
addMoney();
} else if (i == 3) {
quMoney();
} else if(i == 0){
login();//输入0登录
}else {
System.out.println("输入错误");
}
}
//查看余额
public void selectMoney() {
System.out.println(card.username + "余额为:" + card.money);
System.out.println("请输入数字,如果输入0返回主菜单,其他数字都是结束程序");
int i = scanner.nextInt();
//判断为0返回 mainMenu
if (i == 0) {
mainMenu();
} else {
System.out.println("输入错误");
}
}
//存钱
public void addMoney() {
System.out.println("请输入要存多少钱");
int cunMoney = scanner.nextInt();
card.money += cunMoney;
selectMoney();//存过都去查询一次
}
//取钱
public void quMoney() {
int quMoney;
System.out.println("请输入要取多少钱");
while(true){
quMoney = scanner.nextInt();
if (quMoney<=card.money){//如果取的钱小于余额则可以
break;
}else {//如果取得钱大于余额则一直循环
System.out.println("请再次输入要取多少钱");
}
}
card.money -= quMoney;
selectMoney();
}
}
}
练习12
自定义方法 :给两个 int参数 , 返回随机数int。
package lianxi;
import java.util.Random;
import java.util.Scanner;
public class demo12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个参数");
int a = scanner.nextInt();
System.out.println("请输入第二个参数");
int b = scanner.nextInt();
Random random = new Random();
System.out.println(random.getNum(a,b));
}
public static class Random {
public int getNum(int a,int b){
int randomNum = (int) (Math.random() * (b-a) + a);
return randomNum;
}
}
}
练习13
自定义方法 ,两个参数1 String[] 2String name 返回 找到的下标。
package lianxi;
import java.awt.datatransfer.StringSelection;
import java.util.Scanner;
/*
自定义方法 ,两个参数1 String[] 2String name 返回 找到的下标。
*/
public class demo13 {
public static void main(String[] args) {
String[] names = new String[]{"张飞", "刘备", "关羽", "黄忠", "马超", "赵云"};
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个名字");
String nameIn = scanner.next();
getIndex(names, nameIn);
}
public static void getIndex(String[] names, String name) {
for (int i = 0; i < names.length; i++) {
if (names[i].equals(name)) {
System.out.println(names[i] + "在" + i + "号索引");
break;
}
if (i == names.length - 1 && !names[names.length - 1].equals(name)) {//最后一个索引也不是这个名字
System.out.println("没有这个名字");
break;
}
}
}
}
练习14
ATM系统, 登录,显示余额、存钱、取钱、退出功能。
package lianxi;
import java.util.Scanner;
public class demo14 {
public static void main(String[] args) {
ATM atm = new ATM();
atm.login();
}
/*
ATM系统,登录,显示余额、存钱、取钱、退出功能。
*/
//账户类
public static class Account {
String username;
String password;
double money;
public Account() {
}
public Account(String username, String password, double money) {
this.username = username;
this.password = password;
this.money = money;
}
}
//ATM
public static class ATM {
String username;
String password;
double money;
Account account = new Account("张三", "111111", 1000.0);
Scanner scanner = new Scanner(System.in);
//登录功能
public void login() {
System.out.println("请输入用户名");
String usernameIn = scanner.next();
System.out.println("请输入密码");
String passwordIn = scanner.next();
if (account.username.equals(usernameIn) && account.password.equals(passwordIn)) {
System.out.println("进入主菜单");
mainMenu();
} else {
System.out.println("用户名或密码错误");
}
}
//主菜单
public void mainMenu() {
System.out.println("1.查询余额");
System.out.println("2.存钱");
System.out.println("3.取钱");
System.out.println("4.退出");
System.out.println("----------");
while (true) {
System.out.println("请输入一个数字");
int i = scanner.nextInt();
if (i == 1) {
selectMoney();
} else if (i == 2) {
System.out.println("请输入存的钱");
addMoney(scanner.nextDouble());
} else if (i == 3) {
System.out.println("请输入取的钱");
quMoney(scanner.nextDouble());
} else if (i == 4) {
System.out.println("谢谢使用");//退出则不再循环
break;
} else {
System.out.println("没有此选项");
}
}
}
//显示余额
public void selectMoney() {
System.out.println("余额为" + account.money);
}
//存钱
public void addMoney(double qian) {
account.money += qian;
System.out.println("存钱成功,余额为" + account.money);
}
//取钱
public void quMoney(double qian) {
//取得钱大于余额
if (qian > account.money) {
System.out.println("余额不足");
} else {
account.money -= qian;
System.out.println("取钱成功,余额为" + account.money);
}
}
}
}
练习15
自定义数组 ,参数 1 数组 2 字符串 返回数组 int
参数1 无序数组 , 根据参数2 升序或降序,
方法中 判断 返回升序或降序后的数组
package lianxi;
import java.util.Random;
import java.util.Scanner;
import static lianxi.demo15.sort;
public class demo15 {
public static void main(String[] args) {
//创建一个随机int数组
int[] arr = new int[10];
Random random = new Random();
//随机生成
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 100 + 1);
// arr[i] = random.nextInt(99)+1;
}
System.out.print("生成的随机数组为:");
for (int i = 0; i < arr.length; i++) {
if (i == 0) {
System.out.print("[" + arr[i] + ",");
} else if (i == arr.length - 1) {
System.out.println(arr[i] + "]");
} else {
System.out.print(arr[i] + ",");
}
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入排序方式");
while (true) {
String paiXu = scanner.next();
if ("升序".equals(paiXu) || "降序".equals(paiXu)) {
//调用方法排序
sort(arr, paiXu);
System.out.print("排序后数组为:");
for (int i = 0; i < arr.length; i++) {
if (i == 0) {
System.out.print("[" + arr[i] + ",");
} else if (i == arr.length - 1) {
System.out.println(arr[i] + "]");
} else {
System.out.print(arr[i] + ",");
}
}
//排序完毕后询问是否继续使用
System.out.println("请输入 1-继续 或 2-退出");
while(true) {
int c = scanner.nextInt();
if (c == 1) {
//继续使用
System.out.println("请输入排序方式");
break;//跳出这个循环,到外层去输入排序方式
} else if (c == 2) {
//退出
System.out.println("谢谢使用");
return;//直接结束程序
} else {
System.out.println("输入有误,请重新输入1或2");//接着循环输入正确的1或2
}
}
} else {
System.out.println("输入排序方式有误,请重新输入");
}
}
}
public static void sort(int[] arr, String s) {
int temp;
//升序
if ("升序".equals(s)) {
//升序排列
for (int i = 1; i < arr.length; i++) {//排几趟
for (int j = 0; j < arr.length - i; j++) {//排几次序,每走一趟就会有个最大或最小的, 就可以减1少一次排序,涉及到索引
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//降序
else {
//降序排列
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
}
练习16

package lianxi;
public class demo16 {
public static void main(String[] args) {
Regal regal = new Regal("君威", "1");
Seller seller = new Seller("张飞");
seller.sell(regal,100);
}
//Excelle类 汽车种类
public static class Excelle {
String type;
String id;
public Excelle() {
}
public Excelle(String type, String id) {
this.type = type;
this.id = id;
}
public String getType() {
return type;
}
public String getId() {
return id;
}
}
//Regal类 汽车种类
public static class Regal {
String type;
String id;
public Regal() {
}
public Regal(String type, String id) {
this.type = type;
this.id = id;
}
public String getType() {
return type;
}
public String getId() {
return id;
}
}
//Seller类 销售员
public static class Seller {
String name;
public Seller() {
}
public Seller(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sell(Excelle car) {
System.out.println("编号为" + car.getId() + "的" + car.getType());
}
public void sell(Regal car) {
System.out.println("编号为" + car.getId() + "的" + car.getType());
}
public void sell(Regal car, int num) {
System.out.println(name + "卖了" + num + "辆" + "编号为" + car.id + "的" + car.type);
}
}
}
练习17

package lianxi;
import java.util.Scanner;
public class demo17 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要领养什么宠物 1-狗 2-企鹅");
while (true) {
int i = scanner.nextInt();
if (i == 1) {
System.out.println("领养狗");
//领养狗
Dog dog = new Dog();
System.out.println("请输入要领养什么品种狗 1-金毛 2-阿拉斯加");
while (true) {
int type = scanner.nextInt();
if (type == 1) {
System.out.println("领养金毛");
//领养金毛
dog.setType("金毛");
break;
} else if (type == 2) {
System.out.println("领养阿拉斯加");
dog.setType("阿拉斯加");
break;
} else {
System.out.println("没有此品种,请重新输入: 1-金毛 2-阿拉斯加");
}
}
//默认亲密度为0
dog.setLove(0);
//输入健康值
System.out.println("请输入健康值");
dog.setHealthValue(scanner.nextInt());
//输入名字
System.out.println("请输入要起的名字");
dog.setName(scanner.next());
//打印输出宠物信息
System.out.println("打印输出宠物信息");
dog.aa();
break;
} else if (i == 2) {
System.out.println("领养企鹅");
//领养企鹅
Penguin penguin = new Penguin();
System.out.println("请输入要领养什么性别 1-q仔 2-q妹");
while (true) {
int sex = scanner.nextInt();
if (sex == 1) {
System.out.println("选择了q仔");
penguin.setSex("q仔");
break;
} else if (sex == 2) {
System.out.println("选择了q妹");
penguin.setSex("q妹");
break;
} else {
System.out.println("没有此性别,请重新输入: 1-q仔 2-q妹");
}
}
//默认亲密度为0
penguin.setLove(0);
//输入健康值
System.out.println("请输入健康值");
penguin.setHealthValue(scanner.nextInt());
//输入名字
System.out.println("请输入要起的名字");
penguin.setName(scanner.next());
//打印输出宠物信息
System.out.println("打印输出宠物信息");
penguin.aa();
break;
} else {
System.out.println("没有此宠物,请重新输入宠物品种 1-狗 2-企鹅");
}
}
}
//dog类
public static class Dog {
String name;
String type;
int healthValue;
int love;
public Dog() {
}
public Dog(String name, String type, int healthValue, int love) {
this.name = name;
this.type = type;
this.healthValue = healthValue;
this.love = love;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getHealthValue() {
return healthValue;
}
public void setHealthValue(int healthValue) {
if (healthValue >= 0 && healthValue <= 100) {
this.healthValue = healthValue;
} else {
healthValue = 0;
}
}
public void setLove(int love) {
this.love = love;
}
public int getLove() {
return love;
}
//打印输出
public void aa() {
System.out.println("我的名字是" + name + ",健康值是" + healthValue + ",是一只" + type + ",和主人亲密度为" + love);
}
}
//企鹅类
public static class Penguin {
public Penguin() {
}
String name;
String sex;
int healthValue;
int love;
public Penguin(String name, String sex, int healthValue, int love) {
this.name = name;
this.sex = sex;
this.healthValue = healthValue;
this.love = love;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getHealthValue() {
return healthValue;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public void setHealthValue(int healthValue) {
if (healthValue >= 0 && healthValue <= 100) {
this.healthValue = healthValue;
} else {
healthValue = 0;
}
}
//打印输出
public void aa() {
System.out.println("我的名字是" + name + ",健康值是" + healthValue + ",是一只" + sex + ",和主人亲密度为" + love);
}
}
}
练习18
要求 : 1 父类水果类 没有属性, 有一个抽象方法 (结出水果,无返回,无参数) 2 香蕉 子类 重写结出水果 输出结出一个香蕉 3 苹果子类 重写结出水果 输出结出一个苹果 4 测试类 new 两个子类 调用结出的方法
package lianxi;
public class demo18 {
public static void main(String[] args) {
Fruit.Banana banana = new Fruit.Banana();
banana.result();
Fruit.Apple apple = new Fruit.Apple();
apple.result();
}
//水果类
public static abstract class Fruit{
public abstract void result();
//香蕉类
public static class Banana extends Fruit{
public Banana() {
}
@Override
public void result() {
System.out.println("长出香蕉");
}
}
//苹果类
public static class Apple extends Fruit{
public Apple() {
}
@Override
public void result() {
System.out.println("长出苹果");
}
}
}
}
练习19
要求 : 1 父类披萨类 没有属性, 有一个生成披萨的方法 (生成披萨,String返回,无参数) 2 芝士披萨 子类 重写生成披萨的方法 输出结出一个芝士披萨 3 牛肉披萨 子类 重写生成披萨的方法 输出结出一个牛肉披萨 4 测试类 new 两个子类 调用结出的方法 注意有返回类型
package lianxi;
/*
要求
1 父类披萨类 没有属性, 有一个生成披萨的方法 (生成披萨,String返回,无参数)
2 芝士披萨 子类 重写生成披萨的方法 输出结出一个芝士披萨
3 牛肉披萨 子类 重写生成披萨的方法 输出结出一个牛肉披萨
4 测试类 new 两个子类 调用结出的方法 注意有返回类型
*/
public class demo19 {
public static void main(String[] args) {
pisa.ZhiShi zhiShi = new pisa.ZhiShi();
pisa.NiuRou niuRou = new pisa.NiuRou();
System.out.println(zhiShi.result());
System.out.println(niuRou.result());
}
//披萨类
public static class pisa{
public String result() {
return "披萨";
}
//芝士披萨
public static class ZhiShi extends pisa{
@Override
public String result() {
return "结出芝士披萨";
}
}
//牛肉披萨
public static class NiuRou extends pisa{
@Override
public String result() {
return "结出牛肉披萨";
}
}
}
}
练习20

package lianxi;
import java.util.Scanner;
public class demo20 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要租什么车 1-轿车,2-客车 (只可以输入数字哦)");
while (true) {
int i = scanner.nextInt();
if (i == 1) {
//轿车
System.out.println("请输入要租的车型 别克商务舱GL8 宝马550i 别克林荫大道");
while (true) {
String type = scanner.next();
//如果输入的不是这三个
if (!"别克商务舱GL8".equals(type) && !"宝马550i".equals(type) && !"别克林荫大道".equals(type)) {
System.out.println("无此车型,请重新输入");
} else {
MotoVehicle.Car car = new MotoVehicle.Car(type);
System.out.println("请问租几天");
int days = scanner.nextInt();
car.calcRent(days);
break;
}
}
break;
} else if (i == 2) {
//客车
System.out.println("请输入要租几座");
while (true) {
int seat = scanner.nextInt();
if (seat <= 0 || seat > 26) { //如果输入<0,不合法
System.out.println("输入的座数不合法,请重新输入");
} else {
MotoVehicle.Bus bus = new MotoVehicle.Bus(seat);
System.out.println("请问租几天");
int days = scanner.nextInt();
bus.calcRent(days);
break;
}
}
break;
} else {
System.out.println("没有这个车型,请重新输入");
}
}
}
//机动车类
public static abstract class MotoVehicle {
String color;
String Brand;
int number;
int Mileage;
//计算多少钱的方法
public abstract void calcRent(int days);
//轿车类
public static class Car extends MotoVehicle {
String type;
public Car(String type) {
this.type = type;
}
@Override
public void calcRent(int days) {
// System.out.println("请输入要租的车型 1-别克商务舱GL8 2-宝马550i 3-别克林荫大道");
if ("别克商务舱GL8".equals(type)) {
System.out.println("租的" + type + ",租了" + days + "天," + "需要" + 600 * days + "元");
} else if ("宝马550i".equals(type)) {
System.out.println("租的" + type + ",租了" + days + "天," + "需要" + 500 * days + "元");
} else {
System.out.println("租的" + type + ",租了" + days + "天," + "需要" + 300 * days + "元");
}
}
}
//客车类
public static class Bus extends MotoVehicle {
int seatCount;
public Bus(int seatCount) {
this.seatCount = seatCount;
}
@Override
public void calcRent(int days) {
if (seatCount <= 16 && seatCount > 0) {
System.out.println("租的" + seatCount + "座的车" +",租了"+ days + "天," + "需要" + 800 * days + "元");
} else if (seatCount > 16 && seatCount <= 26) {
System.out.println("租的" + seatCount + "座的车" +",租了"+ days + "天," + "需要" + 1500 * days + "元");
}
}
}
}
}
浙公网安备 33010602011771号