Hello, I’ve just started studying Java (about 1 week ago). I’ve never studied coding before, and this is my first program. Feel free to give me any advice. Thanks!
P.S.: I know there are faster and simpler ways to do some parts of my code,
but I’m trying to use only what I’ve learned in the current chapter of my course.
package Desafios;
import java.util.Scanner;
public class Desafio2 {
public static void main(String[] args) {
boolean running = true;
String[] ninjasNames = new String[50];
int counter = 0; /*Number of registered ninjas*/
Scanner inputName = new Scanner(System.in);
while (running == true) {
String menu =
"===== Ninja Menu =====\n" +
"1. Register Ninja\n" +
"2. List Ninjas\n" +
"3. Exit\n";
System.out.print(menu + "Choose an option: ");
String ninja = inputName.nextLine();
switch (ninja) {
case "1":
boolean registerMore = true;
while (registerMore && counter < ninjasNames.length) {
System.out.print("What is the name of the ninja you want to register? ");
String register = ninjasNames[counter] = inputName.nextLine();
System.out.println("Ninja " + ninjasNames[counter] + " successfully registered!");
counter++;
System.out.println("Do you want to register another ninja?\n" +
"1. Register another ninja\n" +
"2. Return to main menu\n");
System.out.print("Choose an option: ");
String register2 = inputName.nextLine();
switch (register2) {
case "1":
break;
case "2":
registerMore = false;
break;
default:
System.out.println("Invalid option! Please choose again: " );
register2 = inputName.nextLine();
registerMore = false;
break;
}
}
break;
case "2":
System.out.println("List of registered ninjas: ");
for (int i = 0; i < counter; i++) {
System.out.println((i + 1) + ". " + ninjasNames[i]);
}
System.out.println("Press Enter to return to the main menu...\n");
inputName.nextLine();
break;
case "3":
System.out.println("Thank you for using the ninja registration system!");
running = false;
break;
default:
System.out.println("Invalid option! Please choose again: " + ninja);
break;
}
}
inputName.close();
}
}