日期:2014-05-20 浏览次数:20977 次
package shuzu1;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import xuesheng.StuInform;
public class Dui1 {
//选择功能,1为录入,2为查询,3为退出
//录入:输入姓名、学好,提示是否继续,并保存到一个动态的数组里
//查询,遍历对象数组
static List<StuInform> luru=new ArrayList<StuInform>();
public static void jiemian() throws IOException{
//定义一个字符数组接收123
//判断123不是则抱错,是则分别调用不同方法
System.out.println("按1为录入,按2为查询,按3为退出");
InputStream input=System.in;
int temp=0;
while((temp=input.read())!=-1){
char c=(char)temp;
if(c =='1'){
System.out.println("\n");
//inp.close();
luru();
}
else if(c=='2'){
System.out.println("\n");
inp.close();
chaxun();
}
else if(c=='3'){
System.out.println("\n");
inp.close();
tuichu();
}
else {
System.out.println("错误的指令,请重新输入");
jiemian();
}
}
}
public static void luru() throws IOException{
System.out.println("录入格式为:学生姓名+空格+学号+回车");
//创建一个新的学生对象
InputStream input=System.in;
StringBuffer buf=new StringBuffer();
//StringBuffer yn=new StringBuffer();
int num =1;
for (int i = 0; i < num; i++) {
int namelength = 0;
StuInform inform = new StuInform();
int temp=0;
while((temp=input.read())!=-1){
char c=(char) temp;
if(c==' '){
namelength=buf.length()-1;
inform.setXingming(buf.substring(0, namelength).toString());
//byte b[]=new byte[buf.length()-1];
//b=buf.substring(0, buf.length()-1);
}
if(c=='\n'){
inform.setXuehao(buf.substring(namelength +1, buf.length()-1).toString());
System.out.println("是否还要继续录入,是请按y,否请按n\n");
if(c=='y'){
buf.delete(0, buf.length());
num++;
break;
//再次
}
else if(c=='n'){
//返回界面
input.close();
jiemian();
}
else{
//
}
}
buf.append(c);
}
luru.add(inform);
}
}
public static void chaxun(){
}
public static void tuichu(){
int i=1;
Iterator itr = luru.iterator();
while (itr.hasNext()) {
StuInform nextObj = (StuInform)itr.next();
System.out.println("第"+i+"个学生的信息:\n");
System.out.println("姓名"+nextObj.getXingming()+"\n");
System.out.println("学号"+nextObj.getXuehao()+"\n");
}
}
public static void main (String[] args) throws IOException{
jiemian();
}
}