[java]代码库
package gps.start;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import gps.car.Car;
/**
*
* @author 锋客
* 重点:
* 1.关于static的属性与static的块
* 当二者中都存在Car Mazda=new Car("123", "Mercedes_Benz", "RED")时,
* 二者 Mazda不同,不共用,快中的变量不会影响static属性的全局属性值;
* 当static快中调用static属性Mazda时,会共用这个变量;
* 2.执行顺序:根据书写顺序依次执行,不分块和属性;
*
*/
public class StartGame {
static ArrayList<Car> list_car = new ArrayList<Car>();
public static Car Mercedes_Benz=new Car("123", "Mercedes_Benz", "RED");
public static Car Mazda;
public static Car Porsche;
public static Car car_temp1;
public static Car car_temp2;
static {
//该方法不会重置class中的属性
// Car Mercedes_Benz = new Car("12345", "Benz", "blue");
// Car Mazda = new Car("23456", "Mazda", "red");
// Car Porsche = new Car("34567", "Porsche", "black");
// Car car_temp1 = new Car();
// Car car_temp2 = new Car();
// System.out.println("static块执行了!!!!");
// System.out.println(Mercedes_Benz);
Mercedes_Benz = new Car("12345", "Benz", "blue");
Mazda = new Car("23456", "Mazda", "red");
Porsche = new Car("34567", "Porsche", "black");
car_temp1 = new Car();
car_temp2 = new Car();
System.out.println("static块执行了!!!!");
System.out.println(Mercedes_Benz);
}
//主方法:设置主方法
public static void main(String[] args) {
StartGame game = new StartGame();
System.out.println("--------------------");
//生成自己车辆信息
Car userCar=game.ready();
//抢车位正式开始
while(true){
System.out.println("------!!!!!!!!!-------");
System.out.println("输入抢占的车位号:");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
//每次都调用start()方法重置list_car中车辆顺序
if(game.Start(num)){
System.out.println("抢占成功!!!");
}else{
System.out.println("抢占失败!!!");
}
}
}
/**
* 生成自己车辆信息
*
* @return
*/
public Car ready() {
System.out.println("请输入车辆信息:");
Scanner cursor = new Scanner(System.in);
System.out.println("车牌:");
String id = cursor.next();
System.out.println("类型:");
String type = cursor.next();
System.out.println("颜色:");
String colour = cursor.next();
Car userCar = new Car(id, type, colour);
list_car.add(userCar);
return userCar;
}
/**
* 抢占车位
*
* @param car
* @return
*/
public boolean Start(int num) {
Random random = new Random();
list_car.add(new Car());
list_car.add(new Car());
list_car.add(new Car());
list_car.add(new Car());
list_car.add(new Car());
// 检测list_car存的内容
// for(int i=0;i<list_car.size();i++){
// System.out.println(list_car.get(i));
// }
int index = random.nextInt(4);
list_car.set(index, Mercedes_Benz);
list_car.set((index + 1) % 5, car_temp2);
list_car.set((index + 2) % 5, StartGame.Mazda);
list_car.set((index + 3) % 5, car_temp1);
list_car.set((index + 4) % 5, Porsche);
if(list_car.get(num).getId()!=null){
return false;
}
return true;
}
}
[源代码打包下载]