/**
創(chuàng)新互聯(lián)2013年至今,先為西湖等服務(wù)建站,西湖等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為西湖企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
* 文件名: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 許仕永
* 創(chuàng)建時(shí)間: 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學(xué)生信息管理系統(tǒng)\n");
System.out.println("1.信息查閱");
System.out.println("2.信息查詢");
System.out.println("3.信息錄入");
System.out.println("4.退出系統(tǒng)");
int what = inputInt();
if ( what == 1 )
{
getInfo();
} else if ( what == 2 )
{
allSerach();
} else if ( what == 3 )
{
inputInfo();
} else if( what == 4 )
{
System.out.println("已經(jīng)退出系統(tǒng)");
System.exit(0);
}
}
}
/**
* 獲取全部學(xué)生的信息列表
*/
private void getInfo()
{
if(!list.isEmpty())
{
for ( int i = 0; i list.size(); i++ )
{
System.out.println(list.get(i));
}
}
}
/**
* 執(zhí)行查找
*/
private void allSerach()
{
System.out.println("1.輸入學(xué)號(hào)進(jìn)行查詢");
System.out.println("2.輸入姓名進(jìn)行查詢");
int what = inputInt();
if(what == 1)
{
System.out.println("請(qǐng)輸入學(xué)號(hào):");
search(inputInt());
}
else
{
System.out.println("請(qǐng)輸入姓名:");
search(inputStr());
}
}
/**
* 按學(xué)生姓名查詢學(xué)生信息
* @param name 學(xué)生姓名
*/
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("未查詢到該名字的學(xué)生");
return null;
}
/**
* 按學(xué)號(hào)查詢學(xué)生信息
* @param id 學(xué)號(hào)
*/
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("未查詢到該學(xué)號(hào)的學(xué)生");
return null;
}
/**
* 錄入學(xué)生信息
*/
private void inputInfo()
{
try
{
System.out.print("請(qǐng)輸入學(xué)生學(xué)號(hào):\n");
stuNo = inputInt();
System.out.print("請(qǐng)輸入學(xué)生姓名:\n");
stuName = inputStr();
System.out.print("請(qǐng)輸入學(xué)生性別:\n");
stuSex = inputStr();
System.out.print("請(qǐng)輸入學(xué)生年齡:\n");
stuAge = inputInt();
System.out.print("請(qǐng)輸入學(xué)生班級(jí):\n");
stuClass = inputStr();
list.add(new StudentInfo(stuNo, stuAge, stuName, stuSex, stuClass));
} catch (Exception e)
{
// TODO: handle exception
System.out.println("不允許為空,請(qǐng)重新輸入");
}
}
}
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)
{
//構(gòu)造實(shí)體Bean來(lái)存儲(chǔ)學(xué)生成績(jī)
StuBean bean = new StuBean();
System.out.println("請(qǐng)輸入成績(jī):");
System.out.println("語(yǔ)文:");
bean.setYuwen(scanner());
System.out.println("數(shù)學(xué):");
bean.setMath(scanner());
//獲取英語(yǔ)成績(jī)
System.out.println("英語(yǔ):");
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("當(dāng)前有"+list.size()+"條學(xué)生記錄,所有學(xué)生總分="+sum+"平均分為"+avg);
//輸入y繼續(xù)錄入學(xué)生信息,輸入x退出
System.out.println("\n已成功錄入一個(gè)學(xué)生的數(shù)據(jù),是否繼續(xù)/退出(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 許仕永
* 創(chuàng)建時(shí)間: 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 = "學(xué)號(hào):"+getStuNo()+"\t姓名:"+getStuName()+" \t性別:"+getStuSex()+"\t年齡:"+getStuAge()+"\t班級(jí):"+getStuClass();
return s;
}
}
代碼如下:
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
?System.out.println("Hello World!");
}
public static int f(int a, int b){
return a*a + a*b + b*b;
}
}
結(jié)果如下:
這是我曉得的最簡(jiǎn)單的java小程序代碼了你可以看看:
package com.kenki.emp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
import java.sql.*;
public class emp extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String code = request.getParameter("code");
String name = request.getParameter("name");
String pay = request.getParameter("pay");
System.out.println("empcode:" + code);
System.out.println("name:" + name);
System.out.println("pay:" + pay);
//創(chuàng)建驅(qū)動(dòng)
new com.microsoft.jdbc.sqlserver.SQLServerDriver();
String strd =
"jdbc:microsoft:sqlserver://localhost:1433;databasename=emp_dates";
String username = "sa";
String pws = "";
try {
java.sql.Connection conn = java.sql.DriverManager.getConnection(
strd, username, pws);
String strs = "insert into emp values(?,?,?)";
java.sql.PreparedStatement pre = conn.prepareStatement(strs);
pre.setString(1, code);
pre.setString(2, name);
pre.setString(3, pay);
pre.execute();
pre.close();
conn.close();
//重定向至查詢頁(yè)面
out.println("成功保存?。?);
response.sendRedirect("emp.html");
} catch (SQLException ss) {
ss.printStackTrace();
response.sendRedirect("/WebModule1/error.html");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
package?com.demo;
import?java.awt.BorderLayout;
import?java.awt.Dimension;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.util.Timer;
import?java.util.TimerTask;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JTextField;
public?class?TimerTest?{
private?Timer?timer;
private?JTextField?field;
private?JButton?button;
private?boolean?flag?=?true;
public?TimerTest()?{
timer?=?new?Timer();
addview();
}
private?void?addview()?{
JFrame?frame?=?new?JFrame("Timer?test");
field?=?new?JTextField();
field.setPreferredSize(new?Dimension(0,?30));
button?=?new?JButton("start");
button.setPreferredSize(new?Dimension(100,?30));
button.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
if?(flag)?{
auto();
flag?=?false;
button.setText("stop");
}?else?{
timer.cancel();
flag?=?true;
button.setText("start");
}
}
});
frame.add(field,?BorderLayout.CENTER);
frame.add(button,?BorderLayout.EAST);
frame.setBounds(200,?200,?300,?60);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public?static?void?main(String[]?args)?{
new?TimerTest();
}
private?void?auto()?{
timer.schedule(new?TimerTask()?{
@Override
public?void?run()?{
int?num?=?Integer.parseInt(field.getText().trim());
num?+=?1;
field.setText(num?+?"");
auto();
}
},?1000);
}
}