Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
¿Cómo puedo seleccionar un índice específico de una matriz para usarlo en caso de cambio?

hola, soy un principiante y quiero hacer un juego de matemáticas de prueba, ¿cómo puedo seleccionar un índice específico de la matriz aleatoria y usarlo como valor de caso?

 Random random=new Random(); int num1= random.nextInt(30); int num2= random.nextInt(30); System.out.println(num1+"+"+num2+"="); int result=num1+num2; System.out.println("What is the answer of this operation?"); int sugg1=random.nextInt(100); int sugg2=random.nextInt(100); int[] array = {sugg1,sugg2,result}; Random rand = new Random(); for (int i = 0; i < array.length; i++) { int randomIndexToSwap = rand.nextInt(array.length); int temp = array[randomIndexToSwap]; array[randomIndexToSwap] = array[i]; array[i] = temp; } System.out.println(Arrays.toString(array)); switch (Arrays.toString(array)) { //I NEED HELP HERE HOW CAN I SELECT A SPECIFY INDEX OF THE RANDOM ARRAY AND USE IT AS CASE VALUE case array: }
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Puede usar switch(array[yourIndex]){case element: ...}

over 4 years ago · Santiago Trujillo Relatório

0

No estoy seguro de entender lo que quieres hacer..

Versión de valor:

 // Print for example [14,21,30] if question is 10+11 System.out.println(Arrays.toString(array)); // You want the user to select the answer System.out.print("Please type the good answer : "); Scanner scan = new Scanner(System.in); switch(scan.nextInt()){ case result: System.out.println("Good answer!"); break; default: System.out.printtln("Bad answer!"); }

Versión de índice:

 // Print for example [14,21,30] if question is 10+11 System.out.println(Arrays.toString(array)); // You want the user to select the answer index System.out.print("Please type the good answer : "); Scanner scan = new Scanner(System.in); int index = scan.nextInt(); if(index >= 0 && index <= array.length) { switch(array[index]){ case result: System.out.println("Good answer!"); break; default: System.out.printtln("Bad answer!"); } } else { System.out.println("Selected index does not exist in the array"); }

Por cierto, podría ser bueno si sugg1 != sugg2 != answer , porque en el siguiente código no es imposible que los valores sean los mismos :

 int sugg1=random.nextInt(100); int sugg2=random.nextInt(100); int[] array = {sugg1,sugg2,result};
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda