CustomerBiz方法运用
package FacePro; import java.util.Scanner; public class CustomerBizTesr { public static void main(String[] args) { CustomerBiz a = new CustomerBiz(); Scanner scanner = new Scanner(System.in); for (int i = 0; i < 5; i++) { System.out.println("请输入客户的姓名:"); String name = scanner.next(); a.addName(i,name); System.out.println("继续输入吗?(y/n)"); String s = scanner.next(); if(!s.equals("y")){ break; } } a.showName(); System.out.println(""); System.out.print("请输入开始查找的位置:"); int star = scanner.nextInt(); System.out.print("请输入结束查找的位置:"); int end = scanner.nextInt(); System.out.print("请输入查找的姓名:"); String name = scanner.next(); System.out.println("*****查找结果******"); boolean b = a.search(star, name, end); if(b){ System.out.println("找到该学生!"); } else{ System.out.println("没有找到该学生!"); } scanner.close(); } }
package FacePro; public class CustomerBiz { //声明属性 String [] names = new String[5]; //方案一:数组拼接为字符串 //StringBuffer sb = new StringBuffer(); //声明方法 public void addName(int i,String name){ names[i] = name; // sb.append(names[i]); //方案一 } public void showName(){ System.out.print("输入的全部姓名为:"); for(String na:names){ System.out.print(na+"\t"); } } public boolean search(int star ,String name,int end){ //方案一:根据索引范围查找 /*boolean b = true; int i = sb.indexOf(name); if (i < star || i >end){ b = false; } return b;*/ //方案二:根据name查找 boolean y = false; for(int i=star; i <= end ; i++){ if(names[i].equals(name)){ y = true; break; } } return y; } }