一道JAVA的数组题目,求解

dreamnong 2010-11-14 10:26:24
用字符串输入的方式编写一个简单的通讯录。此通讯录由多条记录构成。每条记录包括一个联系人的姓名、性别、电话、通信地址,并具有查询、增加、修改、删除等功能,且每执行一次功能操作后,可以选择用类似以下的格式,输出通讯录的所有信息记录。

可考虑用数组分别存储姓名,性别,电话,通信地址,那么查询,增加、修改、删除操作就可转换为对数组元素的操作。通讯录中的所有记录的每一个字段可以依次可以使用一维数组的各个元素来存放,并进行各项功能操作处理。也可以使用二维数组。
...全文
286 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
绝情小舞 2010-11-22
  • 打赏
  • 举报
回复
一看就是作业 自己写更好····
绝情小舞 2010-11-22
  • 打赏
  • 举报
回复
用什么数组啊 这么麻烦~
霜之哀伤 2010-11-22
  • 打赏
  • 举报
回复
吃撑了吧,作业自己做去
bird_sky 2010-11-22
  • 打赏
  • 举报
回复
太牛了上面的那位!
huasong09 2010-11-21
  • 打赏
  • 举报
回复
被某位仁兄吓到了……
Zac33201 2010-11-20
  • 打赏
  • 举报
回复
楼上某位强大,UP
jrlb0416 2010-11-19
  • 打赏
  • 举报
回复
好强~~~~
binsonX 2010-11-18
  • 打赏
  • 举报
回复
yetaodiao,好强
ztenv 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yetaodiao 的回复:]
Java code
import java.util.Scanner;
public class StudentArray{
static int Scount = 0;

public static void main(String []args){
String [][] studentInfo;
studentInfo = new String[10][5];
……
[/Quote]
简直是教坏新人,代码一坨。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Guo110 2010-11-18
  • 打赏
  • 举报
回复
yetaodiao,好强
UP!nb
活在岸上的魚 2010-11-15
  • 打赏
  • 举报
回复
分别写几个方法,然后就调用吧!我想这个最简单了
  • 打赏
  • 举报
回复
import java.util.Scanner; 
public class StudentArray{
static int Scount = 0;

public static void main(String []args){
String [][] studentInfo;
studentInfo = new String[10][5];

displayMenu(studentInfo);
System.out.println();
}/* main */


public static void displayMenu(String [][] student)
{
boolean quit = false;
String [] menu = {"add","dis","find","rm","update","quit","read","write"};
do{
System.out.println("+--------------------------------------------+");
System.out.println("|please choose the function you need: |");
System.out.println("+--------------------------------------------+");
System.out.println("| [ add ]:add |");
System.out.println("| [ read ]:read studentInfo from file |");
System.out.println("| [ dis ]:display |");
System.out.println("| [ find ]:find |");
System.out.println("| [ rm ]:remove |");
System.out.println("| [ update ]:update |");
System.out.println("| [ write ]:write studentInfo to file |");
System.out.println("| [ quit ]:quit |");
System.out.println("+--------------------------------------------+");
Scanner scanner = new Scanner(System.in );
String select = scanner.next();

if(select.equals(menu[0])){
add(student);
display(student);
}/* if add */
if(select.equals(menu[6])){
try{
readFromFile(student);
}catch(Exception e){

e.printStackTrace();
}/* catch */
display(student);
}/* if read */
else if(select.equals(menu[1])){
display(student);
}/* if display */
else if(select.equals(menu[2])){
find(student);
/* display(student); */
}/* if find */
else if(select.equals(menu[3])){
remove(student);
display(student);
}/* if remove */
else if(select.equals(menu[4])){
update(student);
display(student);
}/* if update */
else if(select.equals(menu[7])){
try{
writeToFile(student);
}catch(Exception e){
e.printStackTrace();
}/* catch */
display(student);
}/* if write */
else if(select.equals(menu[5])){
quit = true;
}/* if quit */
else
System.out.println("Input Error! Please Try again!");
}while(!quit); /* do{ } while */

}/* displayMenu() */
public static boolean add(String [][] student){
System.out.println("Please Input StudentInfomation like this:.\n\tNumber\tName\tGender\tPhoneNumber\tAddress");
Scanner scanner = new Scanner(System.in);
scanner.reset();
String [] info = new String [5];
for(int i = 0; i< 5;i++){
info[i] = scanner.next();
}/* for */
student[Scount] = info;
++Scount;

System.out.println("Add finished!");
return true;
}/* add() */
public static boolean display(String [][] student){
if(Scount < 1) {
System.out.println(" No Student In Here!");
return false;
}/* no student in */
System.out.println("All the Student Information As Follows:");
System.out.println("\tNumber\tName \tGender\tPhoneNumber \tAddress");
System.out.print("\t");
for(int j = 0; j < Scount;j++){
for(int i = 0; i< 5; i++){
System.out.print(student[j][i]+"\t");
if(i==3)System.out.print(" \t");
}/* for */
System.out.println();
System.out.print("\t");
}/* outer for */
System.out.println();
return true;
}/* display() */

public static boolean find(String [][] student){
int index;
System.out.println("+-----------------------------------------------------+");
System.out.println("|Please choose the which look up approach You need : |");
System.out.println("+-----------------------------------------------------+");
System.out.println("|[ 0 ]:ByNumber |");
System.out.println("|[ 1 ]:ByName |");
System.out.println("|[ 2 ]:ByGender |");
System.out.println("|[ 3 ]:ByPhoneNumber | ");
System.out.println("|[ 4 ]:ByAddress |");
System.out.println("+-----------------------------------------------------+");

Scanner scanner = new Scanner(System.in);
scanner.reset();
index = scanner.nextInt();
switch(index){
case 0: findByTarget(student,index);
break;
case 1: findByTarget(student,index);
break;
case 2: findByTarget(student,index);
break;
case 3: findByTarget(student,index);
break;
case 4: findByTarget(student,index);
break;
default:
System.out.println("Input Error:Please Try again!");
}/* switch */
return true;
}/* find() */
public static boolean findByTarget(String [][] student,int index){
boolean find = false;
String [] menu ={"StudentNumber","StudentNAME","StudentSex","StudentPhoneNumber","StudentHostel"};
System.out.println("Please Input The "+menu[index] + " You want Look Out:");
Scanner scanner = new Scanner(System.in);
String target=scanner.next();
for(int i = 0; i < Scount; i++)
{
if(student[i][index].equals(target))
{
displayIndex(student,i);
find = true;
/* not break then could find another same person ,ie find by sex*/
}/* if */
}/* for */
if(find ==false) System.out.println("The Student You want to find Isn't Exist.");
return true;
}/* findByTarget */

/** print the student that have finded ,only one for each time! */
public static void displayIndex(String[][] student,int index){
System.out.println("+-----------------------------------------------------+");
System.out.println("|The Student You find Are As Follows: |");
System.out.println("+-----------------------------------------------------+");
System.out.println("|\tNumber\tName \tGender\tPhoneNumber \tAddress |");
System.out.print("\t");
for(int i = 0; i< 5; i++){
System.out.print(student[index][i]+"\t");
if(i==3)System.out.print(" \t");
}/* for */
System.out.println();
System.out.println("+-----------------------------------------------------+");

}/* displayIndex */

public static void remove(String [][] student){
System.out.println(" Please Input The StudentNumber of the student you want to remove:");
Scanner scanner = new Scanner(System.in);
String target = scanner.next();
int i;
int start; /* store the position the first object need to remove */
for( i=0; i < Scount;i++)
if(student[i][0].equals(target)){
break;
}/* for and if */

/* Object at the back .remove forward */
for( start = i ;start < Scount-1;start++){
student[start] =student[start+1];
}/* another for */
student[--Scount] = null;
/* assigned null to last Object */

/** since student[--Scount] = null,ie cannot directly add student to
student[--Scount] now!
then ,if add directly ,throws NullPointerException
So we should try to come up with anoter approach!
*/

}/* remove() */

public static void update(String [][] student){
System.out.println("choose the Number Of The Student Which You Want to update:");
Scanner scanner = new Scanner(System.in);
String target = scanner.next();
int index =0;
for( int i = 0; i < Scount; i++)
if(target.equals(student[i][0]))
{
index = i;
break; /* only find the first occured student */
}/* if for */
System.out.println("*--------------------------------------------*");
System.out.println("The Student You Want to find is:");
displayIndex(student,index);
System.out.println("please Input new Information for this student!");
for(int i = 0; i< 5;i++){
student[index][i] = scanner.next();
}/* for */

}/* update */

public static boolean readFromFile(String [][] student) throws Exception {
/* Create a File instance */
java.io.File file = new java.io.File("student.txt");

/* Create a Scanner for the file */
java.util.Scanner input = new java.util.Scanner(file);

/* Read data from file */

while(input.hasNext()) {
for(int i = 0; i< 5;i++){
student[Scount][i] = input.next();
}/* for */
++Scount;
}/* while */

/* Close the file */
input.close();
return true;
}/* readFromFile() */

public static boolean writeToFile(String [][] student) throws Exception {
java.io.File file = new java.io.File("student.txt");

if(file.exists()) {
/* System.out.println(" File already exists!");
System.exit(0); */
if(!file.delete()){
System.out.println(" File already exists ! and cannot be delete!");
System.out.println("Now Create a anther file named \"new_student.txt\" to store you data!");
file = new java.io.File("new_student.txt");
}/* inner if */
else file = new java.io.File("student.txt");
}/* if */

/** Create a file */
java.io.PrintWriter output = new java.io.PrintWriter(file);

/** Write formatted output to the file */
for(int i = 0; i < Scount; i++){
for(int j = 0; j < 5; j++){
output.printf("%s\t",student[i][j]);
}/* inner for */
output.printf("\n");
}/* for */

/** Close the file */
output.close();
return true;
}/* writeToFile */

}
Ade子夜 2010-11-14
  • 打赏
  • 举报
回复
Ding!!!
solver212 2010-11-14
  • 打赏
  • 举报
回复
有种很想写的冲动。。。

23,406

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧