【发布时间】:2020-11-19 07:05:13
【问题描述】:
问题来自posted in Spanish, on es.stackoverflow.com,来自Fernando Méndez:
询问要搜索的名称,从键盘上读取该名称并开始 通过数组来验证是否存在,如果找到,显示一个 指示如果找到名称 x 的消息,否则显示 传说中,x这个名字没有找到。
这是我的代码,但是在执行和比较元素时它只 取数组的最后一个元素,这是比较的元素 其他省略。
package arrreglo_practicas; import java.util.Arrays; import java.util.Scanner; import javax.swing.JOptionPane; public class Arrreglo_Practicas { public static void main(String[] args) { Scanner input = new Scanner(System.in); String busqueda = ""; int elements = 0 ; String aux = null ; //tomando el valor e insertarlo en el arreglo elements = Integer.parseInt(JOptionPane.showInputDialog( "digite la cantidad de elementos del areglo ")); //arreglo de "n" elementos String [] arreglo = new String [elements]; //recorriendo el arreglo para que tome los valores for (int x = 0; x <arreglo.length; x++){ System.out.print("|ingresa los nombres| "); aux = input.nextLine(); arreglo[x] = aux; } //búsqueda inicial busqueda = JOptionPane.showInputDialog(null," busca un nombre:"); //parte que se ejecuta mal if (busqueda.equals(aux)){ for (int a = 0; a < aux.length(); a++) System.out.println("si se encuentra el nombre:"); }else { JOptionPane.showMessageDialog(null,"dicho nombre no existe: "); } } }
【问题讨论】: