Hello everyone,
I am just beginning to learn Java and have started reading "Java Programming for the absolute beginner". It is quite a old book but seems to be doing the trick, mainly.
There is a code example which I follow mostly but the use of the modulus operator in the switch condition doesn't make sense to me, why is it used? Could someone shed some light on this for me?
import java.util.Random;
public class FortuneTeller {
public static void main(String args[]) {
Random randini = new Random();
int fortuneIndex;
String day;
String[] fortunes = { "The world is going to end :-(.",
"You will have a HORRIBLE day!",
"You will stub your toe.",
"You will find a shiny new nickel.",
"You will talk to someone who has bad breath.",
"You will get a hug from someone you love.",
"You will remember that day for the rest of your life!",
"You will get an unexpected phone call.",
"Nothing significant will happen.",
"You will bump into someone you haven't seen in a while.",
"You will be publicly humiliated.",
"You will find forty dollars.",
"The stars will appear in the sky.",
"The proper authorities will discover your secret.",
"You will be mistaken for a god by a small country.",
"You will win the lottery!",
"You will change your name to \"Bob\" and move to Alaska.",
"You will discover first hand that Bigfoot is real.",
"You will succeed at everything you do.",
"You will learn something new.",
"Your friends will treat you to lunch.",
"You will meet someone famous.",
"You will be very bored.",
"You will hear your new favorite song.",
"Tomorrow... is too difficult to predict" };
System.out.println("\nYou have awakened the Great Randini...");
fortuneIndex = randini.nextInt(fortunes.length);
switch (randini.nextInt(7) % 7) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
default:
day = "Tomorrow";
}
System.out.println("I, the Great Randini, know all!");
System.out.println("I see that on " + day);
System.out.println("\n" + fortunes[fortuneIndex]);
System.out.println("\nNow, I must sleep...");
}
}