Colonies (nested if else)

Plantation image

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

  1. import static java.lang.System.out;
  2. import java.util.Scanner;
  3. public class Colonies {
  4. public static void main(String[] args) {
  5. int[] available = {2500, 4120, 6300, 1450};
  6. String[] colonyname = {"India", "Australia", "America", "Jamaica"};
  7. // The next line establishes "keyboard" as an instance of
  8. // the Scanner object with the "System.in" parameter.
  9. // The "System.in" parameter will hold the user's input.
  10. Scanner keyboard = new Scanner(System.in);
  11. out.println("Enter the number of the colony from which you are requesting soldiers: ");
  12. int colonyNum = keyboard.nextInt();
  13. //This is the outside "if else" statement.
  14. if (colonyNum < 4) {
  15. out.println("How many solders are you requesting?");
  16. int requested = keyboard.nextInt();
  17. out.println("Enter the number of the colony needing the soldiers:");
  18. int requestedto = keyboard.nextInt();
  19. //This is the nested "if else" statement.
  20. if (colonyNum == 0 && requested <= available[colonyNum] || colonyNum == 1 && requested <= available[colonyNum] || colonyNum == 2 && requested <= available[colonyNum]|| colonyNum == 3 && requested <= available[colonyNum]){
  21. out.println("Soldiers Available:" + available[colonyNum]);
  22. out.println("Requested soldiers: " + requested);
  23. out.println("From colony: " + colonyname[colonyNum]);
  24. out.println("To colony: " + colonyname[requestedto]);
  25. out.print("We are sending " + requested);
  26. out.println(" soldiers from the colony of " + colonyname[colonyNum] + " to the colony of " + colonyname[requestedto] + ".");
  27. } else {
  28. out.print("In the colony of " + colonyname[colonyNum]);
  29. out.print(" only " + available[colonyNum]);
  30. out.println(" soldiers are available. Please enter a lower number.");
  31. }
  32. //This is the end of the nested "if else" statement.
  33. //This is the outside "if else" statement.
  34. } else {
  35. out.println("Please enter a number from 0 to 3.");
  36. }
  37. keyboard.close();
  38. }
  39. }

Java code

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.

 

< Knights Transfer (Input) Advanced Java >