'F' → Fullscreen
import java.util.Scanner;
public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in);
System.out.println("--- Demo 1 - 99 Bottles Song ---\n");
System.out.print("Enter your favorite soda: "); String soda = input.nextLine(); int bottles = 99;
// While there are bottles left to pass around... while (bottles > 0) { System.out.println(bottles + " bottles of " + soda + " on the wall,"); System.out.println(bottles + " bottles of " + soda + "!"); System.out.println("You take one down, pass it around,"); bottles--; System.out.println(bottles + " bottles of " + soda + " on the wall!");
// Uncomment if you want to pause between iterations // input.nextLine(); }
System.out.print("Press enter to continue..."); input.nextLine(); // ----------------------------------------------------------------------------- }}//import java.util.Scanner;
public class Program { public static void main(String[] args) {23 collapsed lines
Scanner input = new Scanner(System.in);
System.out.println("--- Demo 1 - 99 Bottles Song ---\n");
System.out.print("Enter your favorite soda: "); String soda = input.nextLine(); int bottles = 99;
// While there are bottles left to pass around... while (bottles > 0) { System.out.println(bottles + " bottles of " + soda + " on the wall,"); System.out.println(bottles + " bottles of " + soda + "!"); System.out.println("You take one down, pass it around,"); bottles--; System.out.println(bottles + " bottles of " + soda + " on the wall!");
// Uncomment if you want to pause between iterations input.nextLine(); }
System.out.print("Press enter to continue..."); input.nextLine(); // -----------------------------------------------------------------------------
System.out.println("--- Demo 2 - Turn-Based Battle ---\n");
int playerHealth = 10; int enemyHealth = 6; boolean fighting = true;
// Another suitable condition: playerHealth > 0 && enemyHealth == 0 while (fighting) { System.out.println("Health: " + playerHealth); System.out.println("Goblin: " + enemyHealth); System.out.println("\n-Menu-"); System.out.println("1) Attack"); System.out.println("2) Heal"); System.out.print("-> "); String choice = input.nextLine().toLowerCase();
if (choice.equals("1") || choice.equals("attack")) { System.out.println("Player attacks!"); int playerAttack = (int)(Math.random() * 6); // 0 to 5
if (playerAttack > 0) { System.out.println("You did " + playerAttack + " damage."); enemyHealth = enemyHealth - playerAttack; } else { System.out.println("Missed!"); } } else if (choice.equals("2") || choice.equals("heal")) { int potion = (int)(Math.random() * 3) + 1; // 1 to 3 System.out.println("Healed " + potion + " points."); playerHealth = playerHealth + potion; } else { System.out.println("Invalid option. You lose your turn!"); }
if (enemyHealth > 0) { System.out.println("Goblin attacks!"); int enemyAttack = (int)(Math.random() * 4); // 0 to 3
if (enemyAttack > 0) { System.out.println("Goblin did " + enemyAttack + " damage."); playerHealth = playerHealth - enemyAttack; } else { System.out.println("Missed!"); } } else { System.out.println("Goblin defeated!"); fighting = false; // Exit battle }
if (playerHealth <= 0) { System.out.println("You were defeated!"); fighting = false; // Exit battle } } }}//Your program output should something similar to the sample output below.
--- Demo 1 - 99 Bottles Song ---
Enter your favorite soda: Code Zero99 bottles of Code Zero on the wall,99 bottles of Code Zero!You take one down, pass it around,98 bottles of Code Zero on the wall!Press enter to continue... [Enter]--- Demo 2 - Turn-Based Battle ---
Health: 10Goblin: 6
-Menu-1) Attack2) Heal-> 1Player attacks!You did 3 damage.Goblin attacks!Missed!Health: 10Goblin: 3
-Menu-1) Attack2) Heal-> 1Player attacks!You did 4 damage.Goblin defeated!You may write your reflection answers as comments at the bottom of your code.
Submit your activity and reflection answers to the appropriate dropbox.