/home/caleb/ASDV-Java/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/MP5_CalebFontenot.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/$
package com.calebfontenot.mp5_calebfontenot;
import java.util.Scanner;
@author
public class MP5_CalebFontenot {
static final boolean debug = false;
public static void printArray(boolean[] array) {
final String ANSI_RED = "\u001B[31m";
final String ANSI_GREEN = "\u001B[32m";
final String ANSI_WHITE = "\u001B[37m";
for (int i = 0; i <= array.length - 1; i++) {
if (array[i]) {
System.out.print(ANSI_GREEN + array[i] + " ");
} else {
System.out.print(ANSI_RED + array[i] + " ");
}
if (i != 0) {
if ((i + 1) % 10 == 0) {
System.out.println(ANSI_WHITE);
}
}
}
}
public static void solveAndPrint(boolean[] array, int studentCount) {
for (int student = 1; student <= studentCount; student++) {
if (debug) {
System.out.println("Student " + student + " is interacting with the lockers!");
}
for (int i = 0; i <= array.length - 1; i++) {
if ((i + 1) % student == 0) {
array[i] = !array[i];
}
}
if (debug) {
printArray(array);
}
}
for (int i = 0; i < array.length; i++) {
if (array[i]) {
System.out.println("Locker " + (i + 1) + " is open.");
}
}
}
public static int menu() {
Scanner input = new Scanner(System.in);
int userInput;
do {
System.out.print("Enter 0 to quit, and 1 to enter the number of students: ");
userInput = input.nextInt();
if (userInput == 1) {
System.out.print("Enter the number of students: ");
userInput = input.nextInt();
break;
}
} while (userInput != 0);
return userInput;
}
public static void main(String[] args) {
final int N = menu();
boolean[] lockers = new boolean[N];
solveAndPrint(lockers, N);
}
}