zoukankan      html  css  js  c++  java
  • 模拟道路交通简单练习(类)

    package lianxi;

    public class Car {  //Car类
    private String name;
    private int pos;
    private int line;
    private int speed;

    public Car() {
    // TODO Auto-generated constructor stub
    }

    public Car(String name, int line, int speed) {
    this.name = name;
    this.line = line;
    this.speed = speed;
    this.pos = 0;
    }

    public void run() {
    pos += speed;
    }

    public void stop() {
    this.speed = 0;
    }

    public void setSpeed(int speed) {
    this.speed = speed;
    }

    public String getName() {
    return name;
    }

    public int getPos() {
    return pos;
    }

    public int getLine() {
    return line;
    }

    public int getSpeed() {
    return speed;
    }

    }

    //Road类

    package lianxi;

    import java.util.ArrayList;
    import java.util.List;

    public class Road {
    private int lines;
    private int length;
    private List<TrafficLight> tra = new ArrayList<TrafficLight>();
    private List<Car> cars = new ArrayList<Car>();

    public Road(int lines, int length, String hld) {
    this.lines = lines;
    this.length = length;
    // this.red = hld;
    }

    public void enterCar(String name, int line, int speed) {
    Car car = new Car(name, line, speed);
    cars.add(car);
    }

    public void setLight(int line, int index) {
    TrafficLight tcl = new TrafficLight(line, index);
    tra.add(tcl);
    }

    private void printLine(int line) {
    // int r = (int) (Math.random() * length);
    List<Integer> carPoses = new ArrayList<Integer>();
    for (TrafficLight t : this.tra) {
    if (t.getLine() == line) {
    carPoses.add(t.getIndex());
    }
    }
    for (Car car : this.cars) {
    if (car.getLine() == line) {
    carPoses.add(car.getPos());
    }
    }
    for (int i = 0; i < this.length; i++) {
    if (carPoses.contains(i)) {
    System.out.print("X");
    } else if (carPoses.contains(i)) {
    System.out.print("口");
    } else {
    System.out.print("=");
    }
    }
    System.out.println();
    }

    public void print() {
    for (int i = 0; i < lines; i++) {
    printLine(i);
    }
    }

    public void run() {
    // List<Integer> carPoses = new ArrayList<Integer>();
    // 让所有的车自动开 手动开的话run(int i);
    for (int i = cars.size() - 1; i >= 0; i--) {
    Car car = this.cars.get(i);
    car.run();
    // 如果车的位置大于路长度 移除该车辆信息;
    // if (car.getPos() > this.length - 1) {
    // this.cars.remove(i);
    // }
    }
    }

    public void stopEnd() {
    for (int i = 0; i < cars.size(); i++) {
    if (cars.get(i).getPos() + cars.get(i).getSpeed() > length - 1) {
    cars.get(i).setSpeed(1);
    }
    if (cars.get(i).getPos() == length - 1)
    cars.get(i).stop();
    }
    }

    public List<Car> getCars() {
    return cars;
    }

    }

    //红绿灯

    package lianxi;

    public class TrafficLight {
    private int index;
    private int line;

    public TrafficLight(int line, int index) {
    this.index = index;
    this.line = line;
    }

    public int getIndex() {
    return index;
    }

    public int getLine() {
    return line;
    }

    public void setIndex(int index) {
    this.index = index;
    }

    public void setLine(int line) {
    this.line = line;
    }

    }

    //测试

    package lianxi;

    import java.io.IOException;
    import java.security.NoSuchAlgorithmException;

    public class MainProg {
    public static void main(String[] args) throws IOException,
    NoSuchAlgorithmException, InterruptedException {
    Road road = new Road(3, 50, "@");
    road.enterCar("1号", 1, 1);
    road.enterCar("2号", 2, 3);
    road.enterCar("3号", 2, 13);
    road.setLight(0, 10);
    road.setLight(1, 21);
    road.setLight(2, 5);
    while (true) {
    road.print();
    System.out.println(" ");
    road.run();
    road.stopEnd();
    Thread.sleep(100);
    }
    }

    }

  • 相关阅读:
    Python命名空间的本质
    Jetty架构解析及应用示例
    PRML读书会第一章 Introduction(机器学习基本概念、学习理论、模型选择、维灾等)
    PRML读书会第三章 Linear Models for Regression(线性基函数模型、正则化方法、贝叶斯线性回归等)
    PRML读书会第二章 Probability Distributions(贝塔二项式、狄利克雷多项式共轭、高斯分布、指数族等)
    PRML读书会第四章 Linear Models for Classification(贝叶斯marginalization、Fisher线性判别、感知机、概率生成和判别模型、逻辑回归)
    Mac技巧合集第二期
    Mac技巧合集第三期
    C/Java/Python/ObjectiveC在OS X上的性能实验
    程序猿崛起——Growth Hacker
  • 原文地址:https://www.cnblogs.com/Lovemeifyoudare/p/10302453.html
Copyright © 2011-2022 走看看