-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocker.java
More file actions
101 lines (64 loc) · 1.95 KB
/
Locker.java
File metadata and controls
101 lines (64 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.Scanner;
import java.util.Random;
public class Locker {
public static void main(String [] args)
{ int count = 0;
int leave = 0;
int index = 0;
int full = 0;
int lockerbox[] = new int[3]; // set there are 50 lockers
int uselock;
Scanner input = new Scanner(System.in);
for(int i= 0; i < 3; i++)
{
lockerbox[i] = 1;
} // let all the locker element equal 1;
do
{
System.out.print("Do you want to use the lock;(Intput: 1)");
uselock = input.nextInt();
do
{
index = getLocker();
//System.out.println(index );
}while(lockerbox[index] == 0);
lockerbox[index] = 0;// make sure the last locker element is 0
if(full == 0)
{
System.out.println("The locker " + (index + 1) +" is free to use");
}
count = 0;
for(int j = 0; j < 3; j ++) // check whether lockerbox is full
{
if(lockerbox[j] == 0)
{
count++;
if(count == 3)
{
full = 1;
lockerbox[index] = 1;
}
}
}
if(full == 1) {
System.out.println("The locker is full, please wait...." );
}
System.out.println("Do you want use another locker: input 2 for yes");
uselock = input.nextInt();
}
while( uselock != 1);
System.out.println("There are " + (3-count)
+ " empty locker remand." );
System.out.println("Do you want to leave you locker right now. Input: 3 for yes." );
leave = input.nextInt();
if(leave == 3)
{count--;}
System.out.println("Now! There are " + (3-count) + " empty locker remand." );
}
public static int getLocker()
{
int a = 0;
a = (int) (Math.random()*3);
return a;
}
}