KileMax:
Quiero crear una matriz 2D cadena que almacena el nombre y la dirección que se da como entradas del teclado. por ejemplo: nombre de 1 dirección 1; nombrar 2 Dirección 2;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[][] array = new String[3][2];
for (int i = 0; i < 3; i++)
{
for(int j = 0; j < 2; j++)
{
array[i][j] = sc.nextLine();
}
System.out.println(array[0][0]);
}
}
aquí Quiero imprimir un comunicado pidiendo que introduzca el nombre por ejemplo. System.out.println ( "Introduce el nombre:"); después de eso quiero escribir otra declaración que solicita acceder a la eg.System.out.println dirección ( "Introduzca la dirección"); pero no puedo averiguar cómo hacer que en una matriz 2D
greenlamponatable:
¿Qué tal algo como esto?
public static void main(String[] args) {
int rows = 3;
int cols = 2;
Scanner sc = new Scanner(System.in);
String[][] array = new String[rows][cols];
for (int row = 0; row < rows; row++) {
System.out.println("Enter name:");
array[row][0] = sc.nextLine();
System.out.println("Enter the address:");
array[row][1] = sc.nextLine();
}
// Print out array
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
System.out.print(array[row][col] + ", ");
}
System.out.print(";");
}
}
Salida:
Person 1, Address 1, ; Person 2, Address 2, ; Person 3, Address 3, ;
Compruebe que viven aquí: https://onlinegdb.com/Byu4wc1vI
¡Espero que esto ayude!