电话簿 .问题出在哪儿? [英] phonebook . where is the problem?
本文介绍了电话簿 .问题出在哪儿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道为什么它不运行?它没有任何错误!
i dont know why it doesnt run? it doesnt have any error!
package importcontact;
import java.util.Scanner;
class PhoneEntry
{
String firstName;
String lastName;
String phone;
PhoneEntry( String f, String l, String p ){
firstName = f; lastName = l; phone = p;
}
}
class PhoneBook{
PhoneEntry[] phoneBook;
PhoneBook()
{
phoneBook = new PhoneEntry[7 ] ;
phoneBook[0] = new PhoneEntry("James ", "Barclay ", "(418)665-1223");
phoneBook[1] = new PhoneEntry("Grace" , "Dunbar", "(860)399-3044");
phoneBook[2] = new PhoneEntry("Paul", "Kratides", "(815)439-9271");
phoneBook[3] = new PhoneEntry("Violet" , "Smith", "(312)223-1937");
phoneBook[4] = new PhoneEntry("John", "Smith", "(913)883-2874");
phoneBook[5] = new PhoneEntry("Suleman", "Smith", "(913)883-2874");
phoneBook[6] = new PhoneEntry("Violet", "Dunbar", "(913)883-2874");
}
PhoneEntry search( String lastName, String firstName){
for (int j=0; j<phoneBook.length; j++){
if ( phoneBook[ j ].lastName.equals( lastName) && phoneBook[ j ].firstName.equals( firstName))
return phoneBook[ j ];
}
<pre lang="cs">return null;
}
public PhoneEntry[] search1( String lName){
int count=0;
for(int j=0; j<phoneBook.length; j++) {
if ( phoneBook[ j ].lastName.equals(lName)) count++;
}
if(count>0){
int k=0;
PhoneEntry pe[]=new PhoneEntry[count];
for(int j=0; j<phoneBook.length; j++) {
if ( phoneBook[ j ].lastName.equals(lName))
pe[k++]=phoneBook[j];
}
return pe;
}
return null;
}
class PhoneBookTester{
public static void main (String[] args){
String firstName="";
String lastName="";
PhoneBook pb = new PhoneBook();
Scanner scan = new Scanner(System.in);
do {
System.out.println("Enter the last name or enter quit to terminate" );
lastName = scan.nextLine();
if(lastName.equals("quit")) break;
System.out.println("Enter the first name ");
firstName = scan.nextLine();
// search for "Violet Smith"
PhoneEntry entry =pb.search( lastName, firstName );
PhoneEntry[] entry1 = pb.search1(lastName);
if(entry1.length==0){
System.out.println("No Match Found for :-both the entries -of -first & last names..");
}else{
for(int i=0;i<entry1.length;i++){
System.out.println( entry1[i].firstName + " "+entry1[i].phone);
}
}
if ( entry != null ) System.out.println( "The number is (of the entry matching both first & last name) " +entry.phone );
else
System.out.println("Name not found");
}while(true);
System.out.println("Good Bye ");
}
}
推荐答案
如果您不愿意匹配,请大括号,情况会大不相同:
If you bothered to match you''re braces, things would be rather different:
if ( phoneBook[ j ].lastName.equals(lName))
count++;
}
与以下内容不同:
is not the same as:
if ( phoneBook[ j ].lastName.equals(lName))
{
count++;
}
这篇关于电话簿 .问题出在哪儿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文