韩顺平java作业练习

import java.util.Scanner;


//韩顺平java-->https://www.bilibili.com/video/BV1fh411y7R8?p=440 的作业


/**
 * @author lnpbqc
 * @version 1.0.0
 * time 2022/5/11
 **/
public class home2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("创建的人物名字:");
        String name = in.nextLine();
        System.out.print("你要选择什么工具(船/马):");
        String vehicle = in.nextLine();
        Person p;
        p=switch (vehicle) {
            case "船" ->  new Person(name, CreateVehicle.getBoat());
            case "马" ->  new Person(name, CreateVehicle.getHorse());
            default -> null;
        };
        if(p!=null)p.move();
        else System.out.println("请输入正确字符");
    }
}
interface Vehicles{
    void work();
}
class Horse implements Vehicles{
    @Override
    public void work() {
        System.out.println("正在🏇......");
    }
}
class Boat implements Vehicles{
    @Override
    public void work() {
        System.out.println("正在坐船......");
    }
}
class CreateVehicle{
    public static Horse getHorse(){
        return new Horse();
    }
    public static Boat getBoat(){
        return new Boat();
    }
}
class Person{
    private String name;
    private Vehicles v;

    public Person(String name,Vehicles v) {
        this.name = name;
        this.v = v;
    }
    public void move(){
//        boolean loop = true;
        int count = 0;
        int choice;
        for(;count<81;){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count++;
//            System.out.print("赶路1还是过河2:");
            choice = (int)(Math.random()*10)%2;
            System.out.print(this.name);
            switch (choice) {
                case 0 -> {
                    System.out.println("前面是平地");
                    if (this.v.getClass().equals(new Horse().getClass())) {
                        System.out.println("有马");
                    } else {
                        System.out.println("拿到马了");
                        this.v = CreateVehicle.getHorse();
                    }
                    this.v.work();
                }
                case 1 -> {
                    System.out.println("前面是河流");
                    if (this.v.getClass().equals(new Boat().getClass())) {
                        System.out.println("有船");
                    } else {
                        System.out.println("拿到船了");
                        this.v = CreateVehicle.getBoat();
                    }
                    this.v.work();
                }
            }
        }
        System.out.println("你好"+this.name+",本次旅程已经结束");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值