1 package com.java7; 2 // A simple automated telephone directory. 3 class Phone { 4 public static void main(String[] args) { 5 String numbers[][] = { 6 { "Tom", "555-3322" }, 7 { "Mary", "555-8976" }, 8 { "Jon", "555-1037" }, 9 { " Rachel", "555-1400"} 10 }; 11 12 int i; 13 14 if(args.length != 1) // 要使用程序,必须提供一个命令行实参 15 System.out.println("Usage: java Phone <name>"); 16 else { 17 for(i = 0; i < numbers.length; i++){ 18 if(numbers[i][0].equals(args[0])) { 19 System.out.println(numbers[i][0] + ": " + numbers[i][1]); 20 break; 21 } 22 } 23 if(i == numbers.length) 24 System.out.println("Name not found."); 25 } 26 } 27 }
时间: 2024-12-28 08:19:19