重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
300行能干啥啊,你还不如自己做做练习题。
成都创新互联是专业的沈阳网站建设公司,沈阳接单;提供网站制作、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行沈阳网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
package suoha;
import java.util.ArrayList;import java.util.Random;
public class MainFunction {
private ArrayListMyCard cards = new ArrayListMyCard();
private int[] cardNumArr = new int[52];
private int[] mycards = new int[5];
private int[] p1cards = new int[5];
public static void main(String[] args) {
MainFunction mainfunc = new MainFunction();
mainfunc.makeCards();
mainfunc.cardNumArr = mainfunc.shuffleCards(mainfunc.cardNumArr);
mainfunc.catchCards();
mainfunc.showAllCardsOfHand();
//mainfunc.printNums();
}
/**
* 洗牌
* @param cardNums
* @return
*/
private int[] shuffleCards(int[] cardNums){
int size = cardNums.length;
int[] returnNums = new int[size];
//System.out.println(cardNums[0]+"=========================");
Random rand = new Random();
for(int i=size-1;i0;i--){
int nextInt = rand.nextInt(i);
returnNums[size-1-i] = cardNums[nextInt];
cardNums[nextInt] = cardNums[i];
cardNums[i] = returnNums[size-1-i];
//System.out.println("suijishu:"+nextInt+"||zhi:"+returnNums[size-1-i]);
}
returnNums[size-1] = cardNums[0];
return returnNums;
}
/**
* 抓牌
*/
private void catchCards(){
for(int i=0;i5;i++){
mycards[i] = cardNumArr[2*i];
p1cards[i] = cardNumArr[2*i+1];
}
}
private void showAllCardsOfHand(){
System.out.print("你手中的牌是:||");
for(int i=0;i5;i++){
MyCard myCard = cards.get(mycards[i]-1);
System.out.print(myCard.toString()+"||");
}
System.out.println();
System.out.print("对家手中的牌是:||");
for(int i=0;i5;i++){
MyCard myCard = cards.get(p1cards[i]-1);
System.out.print(myCard.toString()+"||");
}
}
/**
* 显示牌的顺序
*/
private void printNums(){
for(int i=0;icardNumArr.length;i++){
System.out.print(cardNumArr[i]+",");
}
}
/**
* 制作卡片
*/
private void makeCards(){
for(int i=1;i=13;i++){
for(int j=1;j=4;j++){
//System.out.println((i-1)*4+j);
MyCard card = new MyCard((i-1)*4+j, j, i);
cardNumArr[(i-1)*4+j-1] = (i-1)*4+j;
cards.add(card);
}
// MyCard card1 = new MyCard((i-1)*4+1, 1, i);
// MyCard card2 = new MyCard((i-1)*4+2, 2, i);
// MyCard card3 = new MyCard((i-1)*4+3, 3, i);
// MyCard card4 = new MyCard((i-1)*4+4, 4, i);
// cards.add(card1);
// cards.add(card2);
// cards.add(card3);
// cards.add(card4);
}
}
}
差不多300行了!
/* 本程序使用常见组件设计一个用于输入学生信息的用户界面 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Example0701_Components extends JFrame implements ActionListener
{
JTextField name;
JPasswordField password;
JRadioButton male;
JRadioButton female;
JCheckBox party;
JSpinner age;
JButton selectColor;
JPanel color;
JSlider addition;
JComboBox department;
JList course;
JButton confirm;
JButton save;
JTextArea result;
JProgressBar progbar;
JLabel time;
public Example0701_Components()
{
setTitle("Components usage in inputting the information of students");
setDefaultCloseOperation(EXIT_ON_CLOSE);
buildContentPane();
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
}
public void buildContentPane()
{
name = new JTextField();
password = new JPasswordField();
male = new JRadioButton("男", true);
female = new JRadioButton("女");
ButtonGroup group = new ButtonGroup();
group.add(male);
group.add(female);
party = new JCheckBox("", false);
age = new JSpinner();
age.setValue(new Integer(20));
age.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent evt)
{
Integer v = (Integer)age.getValue();
int value = v.intValue();
if (value = 15)
{
age.setValue(new Integer(15));
}
if (value = 25)
{
age.setValue(new Integer(25));
}
}
});
color = new JPanel();
color.setBorder(BorderFactory.createLineBorder(Color.BLUE));
addition = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
addition.setMajorTickSpacing(10);
addition.setMinorTickSpacing(5);
addition.setPaintTicks(true);
addition.setPaintLabels(true);
addition.setSnapToTicks(true);
String[] departmentNames = {
"计算机科学与技术系",
"电子信息与技术系",
"计算机工程系"
};
department = new JComboBox(departmentNames);
department.setEditable(false);
String[] coursesNames = {
"数据结构",
"操作系统",
"网络原理",
"Java程序设计",
"分布式系统开发技术",
"计算机导论",
"密码学",
"计算机组成原理",
"编译原理",
"图形学"
};
course = new JList(coursesNames);
course.setVisibleRowCount(5);
confirm = new JButton("确认");
confirm.addActionListener(this);
save = new JButton("保存");
save.addActionListener(this);
result = new JTextArea();
time = new JLabel("计时开始...");
progbar = new JProgressBar(JProgressBar.HORIZONTAL,0,100);
progbar.setStringPainted(true);
Timer timer = new Timer(1000, new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy 年 M 月 d 日 hh : mm : ss");
time.setText(sf.format(new java.util.Date()));
if (progbar.getValue() = 100) progbar.setValue(0);
progbar.setValue(progbar.getValue() + 5);
}
});
timer.start();
Box boxName = Box.createHorizontalBox();
boxName.add(new JLabel("姓名"));
boxName.add(name);
Box boxPassword = Box.createHorizontalBox();
boxPassword.add(new JLabel("密码"));
boxPassword.add(password);
Box boxSexPartyAgeColor = Box.createHorizontalBox();
boxSexPartyAgeColor.add(new JLabel("性别"));
boxSexPartyAgeColor.add(male);
boxSexPartyAgeColor.add(female);
boxSexPartyAgeColor.add(Box.createHorizontalGlue());
boxSexPartyAgeColor.add(new JLabel("党否"));
boxSexPartyAgeColor.add(party);
boxSexPartyAgeColor.add(Box.createHorizontalGlue());
boxSexPartyAgeColor.add(new JLabel("年龄"));
boxSexPartyAgeColor.add(age);
boxSexPartyAgeColor.add(Box.createHorizontalGlue());
selectColor = new JButton("选择颜色");
selectColor.addActionListener(this);
boxSexPartyAgeColor.add(selectColor);
boxSexPartyAgeColor.add(Box.createHorizontalStrut(10));
boxSexPartyAgeColor.add(color);
Box boxAge = Box.createHorizontalBox();
Box boxAddition = Box.createHorizontalBox();
boxAddition.add(new JLabel("加分"));
boxAddition.add(addition);
Box box1 = Box.createVerticalBox();
box1.add(Box.createVerticalStrut(20));
box1.add(boxName);
box1.add(Box.createVerticalStrut(20));
box1.add(boxPassword);
box1.add(Box.createVerticalStrut(20));
box1.add(boxSexPartyAgeColor);
box1.add(Box.createVerticalStrut(20));
box1.add(boxAddition);
box1.add(Box.createVerticalStrut(20));
Box butt = Box.createHorizontalBox();
butt.add(confirm);
butt.add(Box.createHorizontalStrut(10));
butt.add(save);
Box box2 = Box.createVerticalBox();
box2.add(new JLabel("系别"));
box2.add(department);
box2.add(Box.createVerticalStrut(10));
box2.add(new JLabel("选课"));
box2.add(new JScrollPane(course));
box2.add(Box.createVerticalStrut(10));
box2.add(butt);
Box box = Box.createHorizontalBox();
box.setBorder(BorderFactory.createTitledBorder("请输入学生信息"));
box.add(box1);
box.add(Box.createHorizontalStrut(20));
box.add(new JSeparator(JSeparator.VERTICAL));
box.add(Box.createHorizontalStrut(20));
box.add(box2);
JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, box, new JScrollPane(result));
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,3));
panel.add(new JLabel("作者:QSP 2005 年 10 月"));
panel.add(time);
panel.add(progbar);
JLabel title = new JLabel("学生信息录入系统", new ImageIcon("mouse.gif"), JLabel.CENTER);
title.setHorizontalTextPosition(JLabel.LEFT);
title.setForeground(Color.BLUE);
title.setFont(new Font("黑体", Font.BOLD, 36));
Container con = getContentPane();
con.add(title, "North");
con.add(split, "Center");
con.add(panel, "South");
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == selectColor)
{
Color c = JColorChooser.showDialog(this, "请选择你所喜欢的颜色", color.getBackground());
color.setBackground(c);
}
if (evt.getSource() == confirm)
{
String nameStr = name.getText();
if (nameStr.equals(""))
{
JOptionPane.showMessageDialog(this, "姓名不能为空!");
name.requestFocus();
return;
}
int ret = JOptionPane.showConfirmDialog(this, "确认信息是正确的吗?");
if (ret != JOptionPane.YES_OPTION) return;
StringBuffer courses = new StringBuffer();
Object[] selectedCourses = course.getSelectedValues();
for (int i = 0 ; i selectedCourses.length ; i++)
{
courses.append((String)selectedCourses[i]);
if (i selectedCourses.length - 1)
{
courses.append(",");
}
}
String student =
nameStr + "," +
"密码(" + new String(password.getPassword()) + ")," +
(male.isSelected() ? "男," : "女,") +
(party.isSelected() ? "党员," : "") +
"现年" + age.getValue() + "岁," +
"喜欢" + color.getBackground() + "颜色," +
"可得到加分" + addition.getValue() + "," +
"在" + department.getSelectedItem() + "学习," ;
+"选修的课程有:" + courses + "\n";
result.append(student);
}
if (evt.getSource() == save)
{
JFileChooser fc = new JFileChooser();
if (JFileChooser.APPROVE_OPTION == fc.showSaveDialog(this))
{
try
{
File f = fc.getSelectedFile();
FileWriter fw = new FileWriter(f);
fw.write(result.getText());
fw.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, "文件 IO 异常!");
}
}
}
}
public static void main(String[] args)
{
new Example0701_Components();
}
}
java项目的大小衡量标准:
微型:只是一个人,甚至是半日工作在几天内完成的软件;
小型:一个人半年内完成的 2000 行以内的程序;
中型: 5 个人在 1 年多的时间内完成的 5000-50000 行的程序;
大型: 5-10 人在两年内完成的 50000-100000 行的程序;
甚大型: 100-1000 人参加用 4-5 年完成的具有 100 , 0000 行的软件项目;
极大型: 2000-5000 人参加, 10 年内完成的 1000 万行以内的程序;
以上摘自:《软件工程概论》 郑人杰、殷人民编
这样的观点是以代码行作为计量标准的,认为代码行多的自然项目也就大了。
以1000行为准,超过千行就要考虑类拆分了。
类的代码行数没有特定的行数限制规范。根据实际情况决定。
对于经常使用的java类,代码行数应该尽可能的少,这样能减少java类的加载时间,减少内存频繁占用和回收。如果类过大,java类加载会耗时并且占用内存大。容易引起内存回收。
/**
* 文件名: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;
ArrayListStudentInfo 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 ArrayListStudentInfo();
}
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;
ListStuBean list = new ArrayListStu.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;
}
}
这个计算器可是我自己写了好几天才写好的耶,你能在网上找到?
计算器:
// Calculator.java
public class Calculator {
private static final char NO_OP = '\0';
private static final char PLUS = '+';
private static final char SUBTRACT = '-';
private static final char MULTIPLY = '*';
private static final char DIVIDE = '/';
private float number1 = 0.0F;
//private float number2 = Float.NaN;
private char operator = NO_OP;
//等于运算
public String opEquals(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = NO_OP;
number1 = result;
return Float.toString(result);
}
//加法运算
public String opAdd(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = PLUS;
number1 = result;
return Float.toString(result);
}
//减法运算
public String opSubtract(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = SUBTRACT;
number1 = result;
return Float.toString(result);
}
//乘法运算
public String opMultiply(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = MULTIPLY;
number1 = result;
return Float.toString(result);
}
//除法运算
public String opDivide(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = DIVIDE;
number1 = result;
return Float.toString(result);
}
//处理运算符
private float performOperation(float number2) {
float result = 0.0F;
switch ( operator ) {
case PLUS:
result = number1 + number2;
break;
case SUBTRACT:
result = number1 - number2;
break;
case MULTIPLY:
result = number1 * number2;
break;
case DIVIDE:
result = number1 / number2;
break;
}
return result;
}
//将字符串类型转换为浮点型
private static float parseNumber(String number) {
float real_number;
try {
real_number = Float.parseFloat(number);
} catch (NumberFormatException e) {
real_number = Float.NaN;
}
return real_number;
}
}
GUI程序:
// CalculatorGUI.java
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI {
// 定义按钮
private Button key0, key1, key2, key3, key4;
private Button key5, key6, key7, key8, key9;
private Button keyequal, keyplus, keyminus;
private Button keyperiod, keymult, keydiv;
// 定义存放按钮的区域
private Panel buttonArea;
// 定义存放运算结果的区域
private Label answer;
// 用来实现运算功能的类的对象实例化
private Calculator calculator;
private boolean readyForNextNumber;
public CalculatorGUI() {
calculator = new Calculator();
readyForNextNumber = true;
answer = new Label("0.0",Label.RIGHT);
key0 = new Button("0");
key1 = new Button("1");
key2 = new Button("2");
key3 = new Button("3");
key4 = new Button("4");
key5 = new Button("5");
key6 = new Button("6");
key7 = new Button("7");
key8 = new Button("8");
key9 = new Button("9");
keyequal = new Button("=");
keyplus = new Button("+");
keyminus = new Button("-");
keymult = new Button("*");
keydiv = new Button("/");
keyperiod = new Button(".");
buttonArea = new Panel();
}
public void launchFrame() {
buttonArea.setLayout(new GridLayout(4,4));
buttonArea.add(key7);
buttonArea.add(key8);
buttonArea.add(key9);
buttonArea.add(keyplus);
buttonArea.add(key4);
buttonArea.add(key5);
buttonArea.add(key6);
buttonArea.add(keyminus);
buttonArea.add(key1);
buttonArea.add(key2);
buttonArea.add(key3);
buttonArea.add(keymult);
buttonArea.add(key0);
buttonArea.add(keyperiod);
buttonArea.add(keyequal);
buttonArea.add(keydiv);
// 设置事件接收句柄
OpButtonHanlder op_handler = new OpButtonHanlder();
NumberButtonHanlder number_handler = new NumberButtonHanlder();
key0.addActionListener(number_handler);
key1.addActionListener(number_handler);
key2.addActionListener(number_handler);
key3.addActionListener(number_handler);
key4.addActionListener(number_handler);
key5.addActionListener(number_handler);
key6.addActionListener(number_handler);
key7.addActionListener(number_handler);
key8.addActionListener(number_handler);
key9.addActionListener(number_handler);
keyperiod.addActionListener(number_handler);
keyplus.addActionListener(op_handler);
keyminus.addActionListener(op_handler);
keymult.addActionListener(op_handler);
keydiv.addActionListener(op_handler);
keyequal.addActionListener(op_handler);
// 新建一个帧,并且加上消息监听
Frame f = new Frame("计算器");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(200, 200);
f.add(answer, BorderLayout.NORTH);
f.add(buttonArea, BorderLayout.CENTER);
f.setVisible (true);
}
//响应按钮事件,并根据不同的按钮事件进行不同的运算
private class OpButtonHanlder implements ActionListener {
public void actionPerformed(ActionEvent event) {
char operator = event.getActionCommand().charAt(0);
String result = "";
switch ( operator ) {
case '+':
result = calculator.opAdd(answer.getText());
break;
case '-':
result = calculator.opSubtract(answer.getText());
break;
case '*':
result = calculator.opMultiply(answer.getText());
break;
case '/':
result = calculator.opDivide(answer.getText());
break;
case '=':
result = calculator.opEquals(answer.getText());
break;
}
answer.setText(result);
readyForNextNumber = true;
}
}
//处理连续运算时主界面的显示
private class NumberButtonHanlder implements ActionListener {
public void actionPerformed(ActionEvent event) {
if ( readyForNextNumber ) {
answer.setText(event.getActionCommand());
readyForNextNumber = false;
} else {
answer.setText(answer.getText() + event.getActionCommand().charAt(0));
}
}
}
//主函数,也是该应用程序的执行入口处
public static void main(String[] args) {
CalculatorGUI calcGUI = new CalculatorGUI();
calcGUI.launchFrame();
}
}
注意:运行的是GUI程序