单链表学生信息--添加

主方法

import java.util.Scanner;

public class Dome_main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//登陆
        Iand i = new Iand();
       
      
       Add a = new Add();
        int id = 0;
        String sex,name;
        int t = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("-----------------<<学生信息管理>>-------------");
        System.out.println("友情提示:你一共有三次机会");
        for(int j = 0;j < 3;j ++)
        {
        	 System.out.print("请输入用户名");
             String user = input.next();
             System.out.print("请输入密码:");
             String pass = input.next();
             if(i.getIand(user, pass) == 1)
             {
            	 t = 1;
            	 break;
             }
             else
             {
            	 t = 0;
            	 System.out.println("用户名或密码错误");
            	 System.out.println("你还有: " + (3 - j - 1) + "次机会");
             }
        }
        if(t == 1)
        {
        	System.out.println("登陆成功");
        	//添加
        	System.out.println("添加学生到单链表");
        	System.out.println("id\tsex\tname\n");
        	System.out.println("请输入第一个学生的基本信息");
        	id = input.nextInt();
        	sex = input.next();
        	name = input.next();
        	 JiBen jb1 = new JiBen(id,sex,name);
        	 
        	System.out.println("请输入第二个学生的基本信息");
        	id = input.nextInt();
        	sex = input.next();
        	name = input.next();
        	 JiBen jb2 = new JiBen(id,sex,name);
        	
        	System.out.println("请输入第三个学生的基本信息");
        	id = input.nextInt();
        	sex = input.next();
        	name = input.next();
        	JiBen jb3 = new JiBen(id,sex,name);
        	
        	System.out.println("请输入第四个学生的基本信息");
        	id = input.nextInt();
        	sex = input.next();
        	name = input.next();
        	 JiBen jb4 = new JiBen(id,sex,name);
        	
        	a.getAdd(jb1);
        	a.getAdd(jb2);
        	a.getAdd(jb3);
        	a.getAdd(jb4);
        	 //显示该链表中的信息
        	a.list();
        }
        else
        {
        	System.out.println("登陆失败");
        }
       
	}

}

Iand方法;登陆

class Iand {
	  //初始的密码和用户名
      private String UserName = "玉兔";
      private String PassWord = "1314";
      public int getIand(String user,String pass)
      {
    	  if(UserName.equals(user)  && PassWord.equals(pass))
    	  {
    		  return 1;
    	  }
    	  else
    	  {
    		  return 0;
    	  }
    	 
      }
      
      
	
}

JiBen方法:创建结点

public class JiBen {
     private int id;
     private String sex;
     private String name;
     public JiBen next;//指向下一个元素的指针
     public JiBen(int id,String sex,String name)
     {
    	 this.id = id;
    	 this.name = name;
    	 this.sex = sex;
     }
     public void setId(int id)
     {
    	 this.id = id;
     }
     public int getId()
     {
    	 return this.id;
     }
     public void setSex(String sex)
     {
    	 this.sex = sex;
     }
     public String getSex()
     {
    	 return this.sex;
     }
     public void setName(String name)
     {
    	 this.name = name;
     }
     public String getName()
     {
    	 return this.name;
     }
     //打印单个结点
     public void show()
     {
    	 System.out.printf("学号: %d\t性别:%s\t姓名:%s\n",id,sex,name);
     }
}

Add方法:添加

public class Add{
   JiBen jb = new JiBen(0,"","");//初始化
   public void getAdd(JiBen j)
   {
	   JiBen temp = jb;

	   while(true)
	   {
		   if(temp.next == null)
		   {
			   break;
		   }
		   if(temp.next.getId() >= j.getId())
		   {
			   break;
		   }
		   temp = temp.next;
	   }
	   j.next = temp.next;
	   temp.next = j;
   }
   public void list()
   {
	   if(jb.next == null)
	   {
		   return;
	   }
	   JiBen temp = jb.next;
	   while(true)
	   {
		   if(temp == null)
		   {
			   break;
		   }
		   temp.show();
		   temp = temp.next;
	   }
   }
   
  
}

你可能感兴趣的:(java数据结构)