前几天做的程序今天改了改,以前没有仔细测验,今天测验发现了几个问题今天把它改正了程序如下
import java.util.*;
class Student //定义一个类
{
String xing;
String xingming;
void setStu(String xing1,String xingming1)
{
xing = xing1;
xingming = xingming1;
}
void printStudent()
{
System.out.println(xingming);
}
public String toString()
{
return xing+" "+xingming;
}
}
class YongHu {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
ArrayList a = new ArrayList();
Student stu = new Student();
/*将有的对象添加到数组里面*/
Student stu1 = new Student();
stu1.setStu("张","张三");
a.add(stu1);
Student stu2 = new Student();
stu2.setStu("李","李四");
a.add(stu2);
Student stu3 = new Student();
stu3.setStu("王","王五");
a.add(stu3);
Student stu4 = new Student();
stu4.setStu("李","李旺");
a.add(stu4);
Student stu5 = new Student();
stu5.setStu("张","张磊");
a.add(stu5);
Student stu6 = new Student();
stu6.setStu("张","张武");
a.add(stu6);
Student stu7 = new Student();
stu7.setStu("张","张杰");
a.add(stu7);
Student stu8 = new Student();
stu8.setStu("王","王石");
a.add(stu8);
while(true) {
System.out.println("a:加法 b:输出当前日期 c:查找姓名 q是退出");
System.out.print("请输入字母: ");
String s = sc.next();
if(s.equals("a"))
{
System.out.print("请输入第一个数:");//读取下面一样
int m=sc.nextInt();
System.out.print("请输入第二个数:");
int n=sc.nextInt();
int he=m+n;
System.out.println("它们的和为:"+he);
}
if(s.equals("b"))
{
int Y,M,D,H,m,S;
Y = Calendar.getInstance().get(Calendar.YEAR);//调用年份方法
M = Calendar.getInstance().get(Calendar.MONTH);//调用月份方法
D = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);//调用天份方法
H = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);//调用方法
m = Calendar.getInstance().get(Calendar.MINUTE);//调用方法
S = Calendar.getInstance().get(Calendar.SECOND);
System.out.println("当前系统时间为:" + Y + "年" + (M+1) + "月" + D +"日" + H+"点"+m + "分"+S+ "秒");
System.out.println();
}
if(s.equals("c"))
{
System.out.print("请输入姓名: ");
String name = sc.next();
//System.out.println(name.length());这是看看它的长度
if(name.length()==2) //字节来限制
{
Iterator it = a.iterator(); // 迭代导引
while(it.hasNext())
{
Student h = (Student)it.next();//转化为Student对象
if(h.xingming.equals(name))
{
h.printStudent();
}
}
}
if(name.length()==1) //字节来限制
{
Iterator it = a.iterator();
while(it.hasNext())
{
Student h = (Student)it.next();//转化为Student对象
if(h.xing.equals(name))
{
h.printStudent();
}
}
}
}
if(s.equals("q"))
{
break;
}
}
}
}
这是程序的全部,有点简陋请大家不见笑,请多多指教。
posted on 2010-01-09 10:45
ForMeBlog 阅读(120)
评论(0) 编辑 收藏 所属分类:
Java