Romans (While)
The Romans began their invasions into Britain in 55 BCE. Here is a timeline of the occupation.
- In 55 BCE Caesar invades but is driven off by the Britons.
- In 56 BCE Caesar leads a successful invasion, but the Romans leave.
- In 43 CE the Romans invade and occupy Southeastern Britain.
- In 60 CE Boudicca leads a rebellion against the Romans.
- In 61 CE the Romans defeat Boudicca's rebellion.
- By 75 CE the Romans control all of Britain up to the Scottish frontier.
- Tribes of Picts and Scotts battle the Romans in the North.
The "While" method continues to loop through the "If Else" conditional statements as long as the "invnumber" variable is less than (<) 8.
This code illustrates the use of a While statement.
- import static java.lang.System.out;
- public class Romans {
- public static void main(String[] args) {
- String firstevent = "In 55 BCE Caesar invades but is driven off by the Britons.";
- String secondevent = "In 56 BCE Caesar leads a successful invasion, but the Romans leave.";
- String thirdevent = "In 43 CE the Romans invade and occupy Southeastern Britain.";
- String fourthevent = "In 60 CE Boudicca leads a rebellion against the Romans.";
- String fifthevent = "In 61 CE the Romans defeat Boudicca's rebellion.";
- String sixthevent = "By 75 CE the Romans control all of Britain up to the Scottish frontier.";
- String seventhevent = "Tribes of Picts and Scotts battle the the Romans in the North.";
- int invnumber = 0;
- while (invnumber < 8) {
- invnumber = invnumber + 1;
- if (invnumber == 1) {
- out.println();
- out.println(firstevent);
- }
- else if (invnumber == 2) {
- out.println();
- out.println(secondevent);
- }
- else if (invnumber == 3) {
- out.println();
- out.println(thirdevent);
- }
- else if (invnumber == 4) {
- out.println();
- out.println(fourthevent);
- }
- else if (invnumber == 5) {
- out.println();
- out.println(fifthevent);
- }
- else if (invnumber == 6) {
- out.println();
- out.println(sixthevent);
- }
- else if (invnumber == 7) {
- out.println();
- out.println(seventhevent);
- }
- else {
- out.println();
- out.println("End of list.");
- }
- }
- }
- }
Use Save As to save the code to your folder as: Romans.java. Then compile the code in the Command prompt window. Use javac Romans.java to create the .class file. Execute the program. Use java Romans to run the program.
Your output should look like this:
c:\javamike>java Romans
In 55 BCE Caesar invades but is driven off by the Britons.
In 56 BCE Caesar leads a successful invasion, but the Romans leave.
In 43 CE the Romans invade and occupy Southeastern Britain.
In 60 CE Boudicca leads a rebellion against the Romans.
In 61 CE the Romans defeat Boudicca's rebellion.
By 75 CE the Romans control all of Britain up to the Scottish frontier.
Tribes of Picts and Scotts battle the Romans in the North.
End of list.