阿新 發問時間: 電腦與網際網路程式設計 · 1 0 年前

java程式

有一個JAVA通訊錄程式,可以幫我看看哪邊有問題嗎?

import java.util.Scanner;

public class t {

String name;

String id;

String add;

String tel;

public t(String n, String i, String a, String t) {

name = n;

id = i;

add = a;

tel = t;

}

public static void show(t a[]){

for( int i = 0; i < a.length; i++ ){

System.out.printf( "姓名:%s,學號:%s,住址:%s,電話:%s\n", a[i].name, a[i].id, a[i].add, a[i].tel);

}

}

public static void q(t a[], String na){

for( int i = 0; i < a.length; i++ )

if( na.equals(a[i].name) )

System.out.printf( "姓名:%s,學號:%s,住址:%s,電話:%s\n", a[i].name, a[i].id, a[i].add, a[i].tel);

}

public static void main(String[] args) {

// TODO code application logic here

Scanner p = new Scanner(System.in);

System.out.println("請輸入同學人數:"");

int n = p.nextInt();

t[] a = new t[n];

String na, d, ad, te;

for( int i = 0; i < n; i++ ){

System.out.println("請輸入姓名:"");

na = p.next();

System.out.println("請輸入學號:"99501118");

d = p.next();

System.out.println("請輸入住址:"台北縣中和市興南路二段34號2F ");

}

show(a);

System.out.println("請輸入要查詢的姓名:"");

na = p.next();

q(a, na);

}

}

1 個解答

評分
  • 1 0 年前
    最佳解答

    請參考我的做法

    import java.util.Scanner;

    public class t {

    String name;

    String id;

    String add;

    String tel;

    public t(String n, String i, String a, String t) {

    name = n;

    id = i;

    add = a;

    tel = t;

    }

    public static void show(t a[]){

    for( int i = 0; i < a.length; i++ ){

    System.out.printf( "姓名:%s,學號:%s,住址:%s,電話:%s\n", a[i].name, a[i].id, a[i].add, a[i].tel);

    }

    }

    public static void q(t a[], String na){

    for( int i = 0; i < a.length; i++ )

    if( na.equals(a[i].name) )

    System.out.printf( "姓名:%s,學號:%s,住址:%s,電話:%s\n", a[i].name, a[i].id, a[i].add, a[i].tel);

    }

    public static void main(String[] args) {

    // TODO code application logic here

    Scanner p = new Scanner(System.in);

    System.out.println("請輸入同學人數:");

    int n = p.nextInt();

    t[] a = new t[n];

    String na, d, ad, te;

    for( int i = 0; i < n; i++ ){

    System.out.println("請輸入第" + (i+1) + "位同學");

    System.out.print("請輸入姓名:");

    na = p.next();

    System.out.print("請輸入學號:");

    d = p.next();

    System.out.print("請輸入住址:");

    ad = p.next();

    System.out.print("請輸入電話:");

    te = p.next();

    a[i] = new t(na, d, ad, te);

    System.out.println();

    }

    show(a);

    System.out.println("請輸入要查詢的姓名:");

    na = p.next();

    q(a, na);

    }

    }

還有問題?馬上發問,尋求解答。