123
package com.ezzd;
import java.util.Scanner;
public class Book {
static String[]cars=new String[5];
static Scanner sc=new Scanner(System.in);
private static Object carOut;
private static int num;
public static void main (String[]args) {
init();
while(true) {
System.out.println("進(jìn)入車庫");
System.out.println("離開車庫");
System.out.println("退出程序");
System.out.println("請輸入選擇的菜單編號");
int op = sc.nextInt();
switch(op) {
case 1:
carIn();
break;
case 2:
carOut();
break;
case 3:
return;
? ? default:
break;
}
}
}
private static void carOut() {
for (int i=0;i<cars.length;i ?) {
if(cars[i] !=null) {
}
}
System.out.println("請輸入即將離開車庫的車輛");
String carld=sc.next();
for(int i=0;i<cars.length;i ?) {
if(carld.equals(cars[i])) {
cars[i]=null;
System.out.println("車輛" carld "已成功離庫");
return;
}
}
}
private static void carIn() {
int num=0;
for(int i=0;i<cars.length;i ?) {
if(cars[i]==null) {
?num ?;
}
}
if(num>0) {
System.out.println("尚有空車位" num "個(gè),可以進(jìn)入車庫");
System.out.println("請輸入進(jìn)入的車牌號");
String carld=sc.next();//獲得車牌號
for(int i=0;i<cars.length;i ?) {
if(cars[i]==null) {
cars[i]=carld;
System.out.println("車輛" carld "已成功進(jìn)入車庫");
return;
}
}
}else {
System.out.println("車庫已滿,不能進(jìn)入");
}
}
private static void init() {
cars[0]="鄂G-12345";
cars[1]="鄂G-12346";
cars[2]="鄂G-12347";
cars[3]="鄂G-12348";
}
}