Download DrJava and start the program. To run a program with DrJava, first click “Compile”, then click “Run”. To make sure DrJava was installed correctly, copy and paste the following program. When you save the program the name must match the first line. So save this program as CoinFlip.java.
public class CoinFlip {
public static void main(String[] args) {
// Math.random() returns a value between 0.0 and 1.0
// so it is heads or tails 50% of the time
if (Math.random() < 0.5) {
System.out.println("Heads");
}
else {
System.out.println("Tails");
}
}
}
Next we will try out a while loop.
public class CountdownWhile {
public static void main(String[] args) {
int i = 10
while (i >= 0) {
System.out.println(i);
i = i-1;
}
}
}
Practice it yourself
There are other ways to write a loop in java too! This does the same as CountdownWhile, but look different.
public class CountdownFor {
public static void main(String[] args) {
for (int i=0;i >= 0;i=i-1) {
System.out.println(i);
}
}
}
Practice it yourself