Use a two-dimensional array in the following problem. Your task is to create a simple Java program which would solve this problem.
You MUST use a 2D array of Strings to solve this problem.
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[][] array = new String[6][4];
for (String[] strings : array) {
Arrays.fill(strings, "Empty");
}
do {
System.out.println("Which cart of laptops(1-6):");
int cart = Integer.parseInt(in.nextLine());
System.out.println("Which period(1-4):");
int period = Integer.parseInt(in.nextLine());
System.out.println("The name of the teacher:");
array[cart - 1][period - 1] = in.nextLine();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("Continue (Y|N):");
} while (in.nextLine().equalsIgnoreCase("y"));
}
}
Comments
Leave a comment