Colonies (nested if else)
William the Conqueror died in 1087 and his son William Rufus became the king of England. By 1090 England was experiencing frequent attacks from Scottland in the North. William was able to defeat the Scotts and establish a northern border. What happened next was a harbinger of what was to come in future centuries. William moved English settlers into the disputed lands that he had captured. English colonialism had its first introduction. Many centuries later England would establish colonies throughout the world. Just like William's use of transferring his military to put down insurrections, later English military commanders were able to transfer soldiers from colony to colony.
This code again uses arrays but also adds a nested if else conditional statement. Be careful with the curly braces. You can comment out one if else statement and then the other to help you debug the program. Notice also the && and || or comparison operators. You can see a listing of operators at this site: https://www.w3schools.com/java/java_operators.asp
- import static java.lang.System.out;
- import java.util.Scanner;
- public class Colonies {
- public static void main(String[] args) {
- int[] available = {2500, 4120, 6300, 1450};
- String[] colonyname = {"India", "Australia", "America", "Jamaica"};
- // The next line establishes "keyboard" as an instance of
- // the Scanner object with the "System.in" parameter.
- // The "System.in" parameter will hold the user's input.
- Scanner keyboard = new Scanner(System.in);
- out.println("Enter the number of the colony from which you are requesting soldiers: ");
- int colonyNum = keyboard.nextInt();
- //This is the outside "if else" statement.
- if (colonyNum < 4) {
- out.println("How many solders are you requesting?");
- int requested = keyboard.nextInt();
- out.println("Enter the number of the colony needing the soldiers:");
- int requestedto = keyboard.nextInt();
- //This is the nested "if else" statement.
- if (colonyNum == 0 && requested <= available[colonyNum] || colonyNum == 1 && requested <= available[colonyNum] || colonyNum == 2 && requested <= available[colonyNum]|| colonyNum == 3 && requested <= available[colonyNum]){
- out.println("Soldiers Available:" + available[colonyNum]);
- out.println("Requested soldiers: " + requested);
- out.println("From colony: " + colonyname[colonyNum]);
- out.println("To colony: " + colonyname[requestedto]);
- out.print("We are sending " + requested);
- out.println(" soldiers from the colony of " + colonyname[colonyNum] + " to the colony of " + colonyname[requestedto] + ".");
- } else {
- out.print("In the colony of " + colonyname[colonyNum]);
- out.print(" only " + available[colonyNum]);
- out.println(" soldiers are available. Please enter a lower number.");
- }
- //This is the end of the nested "if else" statement.
- //This is the outside "if else" statement.
- } else {
- out.println("Please enter a number from 0 to 3.");
- }
- keyboard.close();
- }
- }
Use Save As to save the code file to your folder. Then compile the code file in the Command prompt window. Run the Colonies.java file.
Your output should look like this:
C:\javamike>java Colonies
Enter the number of the colony from which you are requesting soldiers:
3
How many soldiers are you requesting: 400
Enter the number of the colony needing the soldiers:
1
Soldiers Available:1450
Requested soldiers: 400
From colony: Jamaica
To colony: Australia
We are sending 400 soldiers from the colony of Jamaica to the colony of Australia.