第十四章 上机练习2......

public class Student {
	String[] names = new String[30];

	public void addName(String name) {
		for (int i = 0; i < names.length; i++) {
			if (names[i] == null) {
				names[i] = name;
				break;
			}
		}
	}

	public void showName() {
		for (int i = 0; i < names.length; i++) {
			if (names[i] == null) {
				break;
			}
			System.out.print(names[i] + " ");
		}
	}

	public boolean editName(String oldName, String newName) {
		boolean find = false;
		for (int i = 0; i < names.length; i++) {
			if (names[i] != null) {
				// /////////////////
				if (names[i].equals(oldName)) {
					names[i] = newName;
					find = true;
					break;
				}
			}
		}
		return find;
	}

}
import java.util.Scanner;

public class TestStudent {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Student s = new Student();
		Scanner input = new Scanner(System.in);
		for (int i = 0; i < 5; i++) {
			System.out.println("请输入客户姓名:");
			String newName = input.next();
			s.addName(newName);
		}
		System.out.println("**********");
		System.out.println("客户姓名列表");
		System.out.println("***********");
		s.showName();
		System.out.println("\n请输入要修改的客户姓名:");
		String oldName = input.next();
		System.out.println("请输入新的客户姓名:");
		String newName = input.next();
		if (s.editName(oldName, newName)) {
			System.out.println("修改结果");
			System.out.println("找到修改成功");
		} else {
			System.out.println("没有找到");
		}
		System.out.println("**********");
		System.out.println("客户姓名列表");
		System.out.println("***********");
		s.showName();
	}

}


你可能感兴趣的:(第十四章 上机练习2......)