急求大神发几个java 300行代码的小程序 简单点的

最好要有代码注释的 初学者供学习

第1个回答  2014-05-30
/**
* 文件名:Operation.java 2014-1-2 下午3:06:37
* @author Administrator
*/
package cc.icoc.javaxu.action;

import java.util.ArrayList;
import java.util.Scanner;
import cc.icoc.javaxu.datas.StudentInfo;

/**
* @author 许仕永
* 创建时间: 2014 2014-1-2 下午3:06:37
*/
public class Operation
{
int stuNo,stuAge;
String stuName,stuClass,stuSex;
ArrayList<StudentInfo> list;
/**程序入口
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
new Operation().operation();
}

private int inputInt()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}

private String inputStr()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}

private void operation()
{
// TODO Auto-generated method stub
if(list == null)
{
list = new ArrayList<StudentInfo>();
}
while(true)
{
System.out.println("\t\t\t学生信息管理系统\n");
System.out.println("1.信息查阅");
System.out.println("2.信息查询");
System.out.println("3.信息录入");
System.out.println("4.退出系统");

int what = inputInt();

if ( what == 1 )
{
getInfo();
} else if ( what == 2 )
{
allSerach();
} else if ( what == 3 )
{
inputInfo();
} else if( what == 4 )
{
System.out.println("已经退出系统");
System.exit(0);
}
}
}

/**
* 获取全部学生的信息列表
*/
private void getInfo()
{
if(!list.isEmpty())
{
for ( int i = 0; i < list.size(); i++ )
{
System.out.println(list.get(i));
}
}
}

/**
* 执行查找
*/
private void allSerach()
{
System.out.println("1.输入学号进行查询");
System.out.println("2.输入姓名进行查询");
int what = inputInt();
if(what == 1)
{
System.out.println("请输入学号:");
search(inputInt());
}
else
{
System.out.println("请输入姓名:");
search(inputStr());
}
}

/**
* 按学生姓名查询学生信息
* @param name 学生姓名
*/
private StudentInfo search(String name)
{
for ( int i = 0; i < list.size(); i++ )
{
if(list.get(i).getStuName().equals(name))
{
System.out.println(list.get(i));
return list.get(i);
}
}
System.out.println("未查询到该名字的学生");
return null;
}

/**
* 按学号查询学生信息
* @param id 学号
*/
private StudentInfo search(int id)
{
for ( int i = 0; i < list.size(); i++ )
{
if(list.get(i).getStuNo()==id)
{
System.out.println(list.get(i));
return list.get(i);
}
}
System.out.println("未查询到该学号的学生");
return null;
}

/**
* 录入学生信息
*/
private void inputInfo()
{
try
{
System.out.print("请输入学生学号:\n");
stuNo = inputInt();
System.out.print("请输入学生姓名:\n");
stuName = inputStr();
System.out.print("请输入学生性别:\n");
stuSex = inputStr();
System.out.print("请输入学生年龄:\n");
stuAge = inputInt();
System.out.print("请输入学生班级:\n");
stuClass = inputStr();
list.add(new StudentInfo(stuNo, stuAge, stuName, stuSex, stuClass));
} catch (Exception e)
{
// TODO: handle exception
System.out.println("不允许为空,请重新输入");
}
}
}

package cc.icoc.javaxu.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Stu
{
int countStu = 0;
int sum = 0;
int avg = 0;
List<StuBean> list = new ArrayList<Stu.StuBean>();

public static void main(String[] args)
{
new Stu().go();
}

private void go()
{
A:while(true)
{
//构造实体Bean来存储学生成绩
StuBean bean = new StuBean();

System.out.println("请输入成绩:");
System.out.println("语文:");
bean.setYuwen(scanner());

System.out.println("数学:");
bean.setMath(scanner());

//获取英语成绩
System.out.println("英语:");
bean.setEnglish(scanner());

//存入容器
list.add(bean);

for ( int i = 0; i < list.size(); i++ )
{
sum += list.get(i).english+list.get(i).math+list.get(i).yuwen;
}
avg = sum / (3*list.size());
System.out.println("当前有"+list.size()+"条学生记录,所有学生总分="+sum+"平均分为"+avg);

//输入y继续录入学生信息,输入x退出
System.out.println("\n已成功录入一个学生的数据,是否继续/退出(y/x)");
if("x".equals(scannerString()))
break A;
}
}

/**输入*/
private int scanner()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}

/**输入*/
private String scannerString()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}

class StuBean
{
int math;
int yuwen;
int english;
public int getMath()
{
return math;
}
public void setMath(int math)
{
this.math = math;
}
public int getYuwen()
{
return yuwen;
}
public void setYuwen(int yuwen)
{
this.yuwen = yuwen;
}
public int getEnglish()
{
return english;
}
public void setEnglish(int english)
{
this.english = english;
}
}
}

/**
* 文件名:StudentInfo.java 2014-1-2 下午3:07:29
* @author Administrator
*/
package cc.icoc.javaxu.datas;

/**
* @author 许仕永
* 创建时间: 2014 2014-1-2 下午3:07:29
*/
public class StudentInfo
{
int stuNo,stuAge;
String stuName,stuSex,stuClass;

public StudentInfo(int stuNo, int stuAge, String stuName, String stuSex, String stuClass)
{
super();
this.stuNo = stuNo;
this.stuAge = stuAge;
this.stuName = stuName;
this.stuSex = stuSex;
this.stuClass = stuClass;
}

public int getStuNo()
{
return stuNo;
}

public void setStuNo(int stuNo)
{
this.stuNo = stuNo;
}

public int getStuAge()
{
return stuAge;
}

public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}

public String getStuName()
{
return stuName;
}

public void setStuName(String stuName)
{
this.stuName = stuName;
}

public String getStuSex()
{
return stuSex;
}

public void setStuSex(String stuSex)
{
this.stuSex = stuSex;
}

public String getStuClass()
{
return stuClass;
}

public void setStuClass(String stuClass)
{
this.stuClass = stuClass;
}

@Override
public String toString()
{
// TODO Auto-generated method stub
String s = "学号:"+getStuNo()+"\t姓名:"+getStuName()+" \t性别:"+getStuSex()+"\t年龄:"+getStuAge()+"\t班级:"+getStuClass();
return s;
}

}本回答被提问者和网友采纳
第2个回答  2014-05-30
《Java就业培训教程》P34源码
程序清单:Promote.java
class Promote
{
public static void main(String args[])
{
byte b = 50;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}

《Java就业培训教程》P35源码
程序清单:TestScope.java
public class TestScope
{
public static void main(String[] args)
{
int x = 12;
{
int q = 96; // x和q都可用
System.out.println("x is "+x);
System.out.println("q is "+q);
}
q = x; /* 错误的行,只有x可用, q 超出了作用域范围 */
System.out.println("x is "+x);
}
}

《Java就业培训教程》P37源码
程序清单:TestVar.java
public class TestVar
{
public static void main(String [] args)
{
int x;//应改为int x=0;
x=x+1; //这个x由于没有初始化,编译会报错。
System.out.println("x is "+x);
}
}
程序清单:Func1.java
public class Func1
{
public static void main(String [] args)
{
/* 下面是打印出第一个矩形的程序代码*/
for(int i=0;i<3;i++)
{
for(int j=0;j<5;j++)
{
System.out.print("*");
}
System.out.println(); //换行
}
System.out.println();
//下面是打印出第二个矩形的程序代码
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
//下面是打印出第三个矩形的程序代码
for(int i=0;i<6;i++)
{
for(int j=0;j<10;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();

}
}

《Java就业培训教程》P39源码
程序清单:Func2.java
public class Func2
{
public static void drawRectangle(int x,int y)
{
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
System.out.print(" * ");
}
System.out.println(); //换行
}
System.out.println();
}

public static void main(String [] args)
{
drawRectangle(3,5);
drawRectangle(2,4);
drawRectangle(6,10);
}
}
《Java就业培训教程》P40源码
程序清单:Func3.java
public class Func3
{
public static int getArea(int x,int y)
{
return x*y;
}

public static void main(String [] args)
{
int area = getArea(3,5);
System.out.println("first Acreage is " + area);
System.out.println("second Acreage is "+ getArea(2,4));
getArea(6,10);
}

《Java就业培训教程》P41源码
程序清单:Func4.java
public class Func4
{
public static int getArea(int x,int y)
{
if(x<=0 || y<=0)
{
return -1;
}
return x*y;
}

public static void main(String [] args)
{
int area = getArea(3,5);
System.out.println("first Acreage is " + area);
System.out.println("second Acreage is "+ getArea(2,4));
getArea(6,10);
}
}
《Java就业培训教程》P43源码
程序清单:Test.java
public class Test
{
public static void main(String [] args)
{
int isum;
double fsum;
isum=add(3,5);
isum=add(3,5,6);
fsum=add(3.2,6.5);
}
public static int add(int x,int y)
{
reutrn x+y;
}
public static int add(int x,int y,int z)
{
return x+y+z;
}
public static double add(double x,double y)
{
return x+y;
}
}
《Java就业培训教程》P47源码
public class TestAnd
{
public static void main(String[] args)
{
int x=0;
int y=0;
if(x!=0 && y==y/x)
System.out.println("y = "+y);
}
}
《Java就业培训教程》P48源码
程序清单:ShiftTest.java
public class ShiftTest
{
public static void main(String [] args)
{
int x=0x80000000;
int y=0x80000000;
x=x>>1;
y=y>>>1;
System.out.println("0x80000000>>1 = " + Integer.toHexString(x));
System.out.println("0x80000000>>>1 = " + Integer.toHexString(y));
}
}
《Java就业培训教程》P61源码
程序清单:TestDo.java
public class TestDo
{
public static void main(String[] args)
{
int x=3;
while(x==0)
{
System.out.println("ok1");
x++;
}
int y=3;
do
{
System.out.println("ok2");
y++;
}
while(y==0);
}
}
《Java就业培训教程》P64源码
程序清单:PrintOddNum.java
public class PrintOddNum
{
public static void main(String [] args)
{
for(int i=0;i<10;i++)
{
if(i%2==0)
continue;
System.out.println(i);
}
}
}
《Java就业培训教程》P67源码
程序清单:TestArray.java
public class TestArray
{
public static void main(String [] args)
{
int x[];
x=new int[100];
for(int i=0;i<100;i++)
{
System.out.println("x"+i+" is "+x[i]);
}
}
}
《Java就业培训教程》P73源码
程序清单:TestArrayCopy.java
public class TestArrayCopy
{
public static void main(String [] args)
{
int ia[]=new int[]{1,2,3,4,5};
int ib[]=new int[]{9,8,7,6,5,4,3};

System.arraycopy(ia,0,ib,0,3);
// 复制源数组中从下标0开始的3个元素到目的数组,从下标0的位置开始存储。
for(int i=0;i<ia.length;i++)
System.out.print(ia[i]);
System.out.println();

for(int j=0;j<ib.length;j++)
System.out.print(ib[j]);
System.out.println();
}
}
相似回答