-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBackAndForth2.java
More file actions
52 lines (44 loc) · 1.22 KB
/
Copy pathBackAndForth2.java
File metadata and controls
52 lines (44 loc) · 1.22 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
import java.util.TimerTask;
import java.util.Timer;
public class BackAndForth2{
static enum Direction{
FORWARDS,BACKWORDS
}
public static void main(String[] args){
TimerTask task = new TimerTask(){
final static int MAXSTEPS = 20;
Direction direction = Direction.FORWARDS;
int steps = -1; // need print one more time for the first round.
boolean isInitial = true;
@Override
public void run(){
switch(direction){
case FORWARDS:
if(!isInitial)
System.out.print("\b "); // \b stands for backspace, just move the edit place back, no replacement
System.out.print("*");
break;
case BACKWORDS:
System.out.print("\b "); // replace current asterisk by space: back the index, use new character
System.out.print("\b\b*");
break;
}
if(++steps == MAXSTEPS){
direction = (direction == Direction.FORWARDS) ? Direction.BACKWORDS : Direction.FORWARDS;
steps = 0;
}
isInitial = false;
}
};
Timer timer = new Timer();
int interval = 1000;
if(args.length!=0){
try{
interval = Integer.parseInt(args[0]);
}catch(NumberFormatException nfe){
nfe.printStackTrace();
}
}
timer.schedule(task, 1000, interval);
}
}