package com.ww.yzpC; public class Find { public boolean searchNames(String[] names, String name) { boolean find = false; for (int i = 0; i < names.length; i++) { if (names[i].equals(name)) { find = true; } } return find; } } package com.ww.yzpC; import java.util.Scanner; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); Find find = new Find(); String names[] = new String[5]; System.out.println("請依次輸入5爲學生的姓名:"); for (int i = 0; i < names.length; i++) { names[i]=in.next(); } System.out.println("請輸入要查找的學生姓名:"); String findname=in.next(); if(find.searchNames(names,findname)){ System.out.println("有"); }else{ System.out.println("沒"); } } }