Topic: 帮我改改,看看怎么写这个东西才更合理。

  Print this page

1.帮我改改,看看怎么写这个东西才更合理。 Copy to clipboard
Posted by: ruoruowudi
Posted on: 2005-09-26 20:36


import java.io.*;
class Cjxt{
  static int count=0;
  private String name;
  private int maths;
  private Cjxt next=null;
  public void setName(String x){
     name=x;
  }
  public String getName(){
    return name;
  }  
  public  void setMaths(int s){
     maths=s;
  }   
  public  int getMaths(){
    return maths;
  }
  public void setNext(Cjxt a){
     next=a;
    
  }
  public Cjxt getNext(){
    return next;
  }
  public void ini(Cjxt pp){
      maths=pp.maths;
      name=pp.name;
      next=pp.next;
  }
}
class TestInPut{
  static void inPut(Cjxt pp){
    while(true){
      input(pp);
      pp.setNext(new Cjxt());    
      pp=pp.getNext();
      Cjxt.count++;  
      String name=readl("停止输入请按n");
      if(name.length()==0)
        continue;
      if(name.charAt(name.length()-1)=='n')
        break;             
    }
  }
  static void input(Cjxt pp){
    BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));
    try{        
      pp.setName(readl("输入姓名"));             
      System.out.println("输入数学成绩");  
      pp.setMaths(Integer.parseInt(ch.readLine()));           
    }catch(Exception e){  System.out.println(e.getMessage());  }
  }  
  static String readl(String s){
    String name=null;
    BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));
    System.out.println(s); //这怎么变成月亮了???这应该是输出s啊
    try{
      name=ch.readLine();      
    }catch(Exception e){ System.out.println(e.getMessage());}
    return(name);
  }    
}        

class TestDelete{
  static void delete(Cjxt pp){
    Cjxt next=new Cjxt();
    String name=null;
    name=TestInPut.readl("输入你要删的名字");     
     for(int i=0;i<Cjxt.count;i++){
     if(name.equals(pp.getName())){
       if(i==0)
       {  
         pp.ini(pp.getNext()); Cjxt.count--;
       }
       else {
         pp.ini(pp.getNext());
         next.setNext(pp);
         Cjxt.count--; break;
       }
      }
      else{
        next=pp;
        pp=pp.getNext();
      }  
   }
  }
}          
class TestInsert {
  static void insert(Cjxt pp){
    Cjxt temp=new Cjxt();
    String name=null;
    name=TestInPut.readl("输入你要插入的地方(名字)");
    for(int i=0;i<Cjxt.count;i++){
       if(name.equals(pp.getName())){
       temp.ini(pp);   pp.setNext(temp);
       TestInPut.input(pp);  
           Cjxt.count++;  break;
      }
      else{
        pp=pp.getNext();
      }  
    }    
  }    
}  
class TestOut{
  static void out(Cjxt pp){
    for(int i=0;i<Cjxt.count;i++){
      System.out.println("name="+pp.getName()+ " "+"maths="+pp.getMaths());
      pp=pp.getNext();  
   }
  }
}  
public class Test{
  public static void main(String[] args){  
     Cjxt head=new Cjxt();  
   esc: while(true){
       System.out.println("1.输入");System.out.println("2.输出");
       System.out.println("3.插入");System.out.println("4.删除");
       System.out.println("5.排序");System.out.println("6.退出");
       System.out.print("请选择你要的操作");
       BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));
       String pd=null;
       try{
         pd=ch.readLine();
       }catch(Exception e){  System.out.println(e.getMessage()+pd);  }  
       switch(pd.charAt(pd.length()-1)){
         case '1':
           TestInPut.inPut(head);break;
         case '2':
           TestOut.out(head);break;
         case '3':
           TestInsert.insert(head);break;
         case '4':
           TestDelete.delete(head);break;
         case '5':
           System.out.println("5.还未完成");break;
         case '6':
           System.out.println("谢谢使用"); break esc;
         default:
           System.out.println("输入有误,从新来过");
       }
     }                           
   }    
}

儿童一定要回呀

2.Re:帮我改改,看看怎么写这个东西才更合理。 [Re: ruoruowudi] Copy to clipboard
Posted by: carter880
Posted on: 2005-09-27 00:38

不错,我刚刚学些这个东西,呵呵,又可以在里面学些东西了,希望多多,发表些东西出来让我看看,基础掌握的很好啊!

3.Re:帮我改改,看看怎么写这个东西才更合理。 [Re: ruoruowudi] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-09-27 09:28

从功能上讲,你完成了你的目标
而且比起上一次发帖,的确是完善了很多,不错哈。

我要提出以下几点:
1 这种基本的链表操作,java的库里面有哈,java.util下面有常用的几个类和接口。
2 如果不考虑代码的优雅和其他特性,你的代码在实现基本链表操作上基本正确;
3 考虑充分使用面向对象的思想,你应该将操作和对象分开,我的意思是说你对学生及其相关属性可封装成一个类,把链表的操作封装另外一个类.

以上言语,可供参考和交流Smile

4.Re:帮我改改,看看怎么写这个东西才更合理。 [Re: ruoruowudi] Copy to clipboard
Posted by: ruoruowudi
Posted on: 2005-09-27 11:13

谢谢指导,我可能受c的影响,把类写的和c中的函数似的,我会慢慢的改掉那些习惯的。。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923