Hello I need help with a code what I want is to enter a matrix in a multiple switch structure here I leave them if someone knows help me please
import java.util.scanner;
public class test {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int op;
System.out.println("1. To record match information");
System.out.println("2. To display match information");
System.out.println("3. To show the number of games played");
System.out.println("4. To modify or update the information of the matches");
System.out.println("5. To show the information of the matches where a red card has been shown for one of the two teams");
System.out.println("6. To show the average number of goals for both teams");
System.out.println("7. To show the team with more goals");
System.out.println("Enter a number");
op=sc.nextInt();
}
int op;
switch op {
case 1:
public static void fill_matrix(String a[][]) {
for(int i=0; i<a.length; i++) {
System.out.println("Enter match information "+(i+1));
System.out.println("Enter the name of the opponent");
a[i][0]=sc.next();
System.out.println("Enter the total goals of the opposing team.");
a[i][1]=sc.next();
System.out.println("Enter the opponent's yellow cards");
a[i][2]=sc.next();
System.out.println("Enter the opponent's red cards");
a[i][3]=sc.next();
System.out.println("Enter the total number of goals for the Colombian national team");
a[i][4]=sc.next();
System.out.println("Enter the yellow cards of the colombia national team");
a[i][5]=sc.next();
System.out.println("Enter the red cards of the Colombian national team");
a[i][6]=sc.next();
System.out.println("Enter the date of the game");
a[i][7]=sc.next();
System.out.println("Enter the place where the match was played");
a[i][8]=sc.next();
}
break;
}
public static void show_array(String a[][]) {
for(int i=0; i<a.length; i++) {
System.out.println("Seleccion colombia vs "+a[i][0]);
System.out.println("The opponent's goals were "+a[i][1]);
System.out.println("The yellow cards of the opponent were "+a[i][2]);
System.out.println("The opponent's red cards were "+a[i][3]);
System.out.println("The total goals of the Colombian National Team were "+a[i][4]);
System.out.println("The yellow cards of the Colombian National Team were "+a[i][5]);
System.out.println("The red cards of the Colombian team were "+a[i][6]);
System.out.println("The date of the match was "+a[i][7]);
System.out.println("The place where the match was played was on "+a[i][8]);
}}
}
Translated with www.DeepL.com/Translator (free version)
Well first of all the format of this code has issues. You are trying to define your switch outside of a class method. It needs to be inside a method. Second, you can define the method fill_matrix like you have there, but you probably want to call the function from the switch. Third, you probably want to define the matrix and pass it to the fill_matrix method so that it can fill it.
public static void main(String[] args) {
// TODO Auto-generated method stub
int op;
System.out.println("1. To record match information");
System.out.println("2. To display match information");
System.out.println("3. To show the number of games played");
System.out.println("4. To modify or update the information of the matches");
System.out.println("5. To show the information of the matches where a red card has been shown for one of the two teams");
System.out.println("6. To show the average number of goals for both teams");
System.out.println("7. To show the team with more goals");
System.out.println("Enter a number");
op=sc.nextInt();
int matrixToFill[][] = new int[5][]; //<--- Create 2d array (must specify first dimension)
switch(op) { //<--- Notice the parenthesis
case 1:
fill_matrix(matrixToFill); //<-- Call method with matrix you want to fill
break;
}
}
This should get you started, but it sounds like you may need to re-read how to define methods, where to define them and how to define 2d arrays to then pass them to methods.
haha I think I don’t understand in this case e.g. how would I get the code by adding everything so that I can compile?
import java.util.Scanner;
public class ensayo {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
String mat[][]=new String[18][9];
llenar_matriz(mat);
System.out.println("Matriz");
mostrar_matriz(mat);
int op;
boolean salir;
while (!salir) {
System.out.println("1. Para registrar la informacion de los partidos");
System.out.println("2. Para mostrar la informacion de los partidos");
System.out.println("3. Para mostrar la cantidad de partidos jugados");
System.out.println("4. Para modificar o actualizar la informacion de los partidos");
System.out.println("5. Para mostrar la informacion de los partidos donde se haya presentado tarjeta roja para alguno de los dos equipos");
System.out.println("6. Para mostrar el promedio de goles de ambos equipos");
System.out.println("7. Para mostrar el equipo con mas goles ");
op=sc.nextInt();
System.out.println("Ingrese un número");}
}
int op;
switch (op) {
case 1:
public static void llenar_matriz (String a[][]) {
for(int i=0; i<a.length; i++) {
System.out.println("Ingrese la informacion de los partidos"+(i+1));
System.out.println("Ingrese el nombre del contrincante");
a[i][0]=sc.next();
System.out.println("Ingrese el total de goles del equipo contrincante");
a[i][1]=sc.next();
System.out.println("Ingrese las tarjetas amarillas del contrincante");
a[i][2]=sc.next();
System.out.println("Ingrese las tarjetas rojas del contrincante");
a[i][3]=sc.next();
System.out.println("Ingrese el total de goles de la seleccion colombia");
a[i][4]=sc.next();
System.out.println("Ingrese las tarjetas amarillas de la seleccion colombia");
a[i][5]=sc.next();
System.out.println("Ingrese las tarjetas rojas de la seleccion colombia");
a[i][6]=sc.next();
System.out.println("Ingrese la fecha del partido");
a[i][7]=sc.next();
System.out.println("Ingrese el lugar donde se jugo el partido");
a[i][8]=sc.next();
break;
}}
case 2:
public static void mostrar_matriz(String a[][]) {
for(int i=0; i<a.length; i++) {
System.out.println("Seleccion colombia vs "+a[i][0]);
System.out.println("Los goles del contrincanten fueron "+a[i][1]);
System.out.println("Las tarjetas amarillas del contrincante fueron"+a[i][2]);
System.out.println("Las tarjetas rojas del contrincante fueron "+a[i][3]);
System.out.println("El total de goles de la Seleccion Colombia fueron "+a[i][4]);
System.out.println("Las tarjetas amarillas de la Seleccion Colombia fueron "+a[i][5]);
System.out.println("Las tarjetas rojas de la seleccion colombia fueron "+a[i][6]);
System.out.println("La fecha del partido fue el "+a[i][7]);
System.out.println("El lugar donde se jugo el partido fue en "+a[i][8]);
break;
} }}