⑴ 求java的一些源代碼
這是一個實體類.
import java.util.Date;
/**
* UserInfo generated by MyEclipse Persistence Tools
*/
public class UserInfo implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private String password;
private String realname;
private String gender;
private String orgDept;
private Integer eDegree;
private Integer country;
private Integer district;
private Integer phone;
private String email;
private String userRight;
private Date registerTime;
private String registerStatus;
private Date admitTime;
private String adminOper;
private Integer loginNum;
private Date loginLasttime;
private String loginLastIp;
// Constructors
/** default constructor */
public UserInfo() {
}
/** minimal constructor */
public UserInfo(String name, String password, String realname,
String orgDept, Integer phone, String email) {
this.name = name;
this.password = password;
this.realname = realname;
this.orgDept = orgDept;
this.phone = phone;
this.email = email;
}
/** full constructor */
public UserInfo(String name, String password, String realname,
String gender, String orgDept, Integer eDegree, Integer country,
Integer district, Integer phone, String email, String userRight,
Date registerTime, String registerStatus, Date admitTime,
String adminOper, Integer loginNum, Date loginLasttime,
String loginLastIp) {
this.name = name;
this.password = password;
this.realname = realname;
this.gender = gender;
this.orgDept = orgDept;
this.eDegree = eDegree;
this.country = country;
this.district = district;
this.phone = phone;
this.email = email;
this.userRight = userRight;
this.registerTime = registerTime;
this.registerStatus = registerStatus;
this.admitTime = admitTime;
this.adminOper = adminOper;
this.loginNum = loginNum;
this.loginLasttime = loginLasttime;
this.loginLastIp = loginLastIp;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return this.realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getOrgDept() {
return this.orgDept;
}
public void setOrgDept(String orgDept) {
this.orgDept = orgDept;
}
public Integer getEDegree() {
return this.eDegree;
}
public void setEDegree(Integer eDegree) {
this.eDegree = eDegree;
}
public Integer getCountry() {
return this.country;
}
public void setCountry(Integer country) {
this.country = country;
}
public Integer getDistrict() {
return this.district;
}
public void setDistrict(Integer district) {
this.district = district;
}
public Integer getPhone() {
return this.phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserRight() {
return this.userRight;
}
public void setUserRight(String userRight) {
this.userRight = userRight;
}
public Date getRegisterTime() {
return this.registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
public String getRegisterStatus() {
return this.registerStatus;
}
public void setRegisterStatus(String registerStatus) {
this.registerStatus = registerStatus;
}
public Date getAdmitTime() {
return this.admitTime;
}
public void setAdmitTime(Date admitTime) {
this.admitTime = admitTime;
}
public String getAdminOper() {
return this.adminOper;
}
public void setAdminOper(String adminOper) {
this.adminOper = adminOper;
}
public Integer getLoginNum() {
return this.loginNum;
}
public void setLoginNum(Integer loginNum) {
this.loginNum = loginNum;
}
public Date getLoginLasttime() {
return this.loginLasttime;
}
public void setLoginLasttime(Date loginLasttime) {
this.loginLasttime = loginLasttime;
}
public String getLoginLastIp() {
return this.loginLastIp;
}
public void setLoginLastIp(String loginLastIp) {
this.loginLastIp = loginLastIp;
}
}
⑵ java 源代碼 基礎點的 謝謝
package com.regex;
import java.io.*;
import java.net.URLDecoder;
import java.util.regex.*;
public class Regex {
private int REMARK=0;
private int LOGIC=0;
private int PHYSIC=0;
boolean start=false;
/**
* @param args
*/
public static void main(String[] args) { //測試方法
// TODO Auto-generated method stub
Regex re=new Regex();
re.regCount("Regex.java");
System.out.println("remark Line: "+re.REMARK);
System.out.println("logic Line: "+re.LOGIC);
System.out.println("physic Line: "+re.PHYSIC);
}/**
* @author BlueDance
* @param s
* @deprecated count
*/
public void regCount(String s){
String url=null;
try {
url=URLDecoder.decode(this.getClass().getResource(s).getPath(),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
try {
BufferedReader br=new BufferedReader(new FileReader(new File(url)));
String s1=null;
while((s1=br.readLine())!=null){
PHYSIC++;
if(CheckChar(s1)==1){
REMARK++;
System.out.println("純注釋行:"+s1);
}
if(CheckChar(s1)==2){
LOGIC++;
REMARK++;
System.out.println("非純注釋行:"+s1);
}
if(CheckChar(s1)==3)
LOGIC++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
/**
*
* @param s
* @return int
* @version check s
*/
public int CheckChar(String s){
String s1=null;
if(s!=null)
s1=s.trim();
//System.out.println(regCheck(s1,re));
if(regCheck(s1,"(//.*)")) //判斷//開頭的為純注釋行
return 1;
if(regCheck(s1,"(.*[;{})] *//.*)")) //判斷不是//開頭的非純注釋行
return 2;
if(regCheck(s1,"(//*.*)")){ //判斷/*開頭的純注釋行
start=true;
return 1;
}
if(regCheck(s1,"(.*[;{})]//*.*)")){ //判斷不是/*開頭的非純注釋行
start=true;
return 2;
}
if(regCheck(s1,"(.* */*/)")){ //判斷*/結尾的純注釋行
start=false;
return 1;
}
if(regCheck(s1,"(.* */*/.*)")&&!strCheck(s1)){ //判斷不是*/結尾的非純注釋行
if(strCheck(s1)){
start=false;
return 2;
}
}
if(start==true) //狀態代碼,start即/*開始時start=true*/結束時為false
return 1;
return 3;//ssssllll
}//aeee
/**
*
* @param s
* @param re
* @return boolean
*/
public boolean regCheck(String s,String re){ //正則表達試判斷方法
return Pattern.matches(re,s);
}
public boolean strCheck(String s){ //中間有*/的字元判斷 此方法最關鍵
if(s.indexOf("*/")>0){
int count=0;
String y[]=s.split("/*/");
boolean boo[]=new boolean[y.length];
for (int i = 0; i < y.length-1; i++) {
char c[]=y[i].toCharArray();
for (int j = 0; j < c.length; j++) {
if(c[j]=='\\'&&c[j+1]=='"'){
count++;
}
}
if(count%2==0){
if(countNumber("\"",y[i])%2!=0){
boo[i]=true;
}else{
boo[i]=false;
}
}else{
if(countNumber("\"",y[i])%2==0){
boo[i]=true;
}else{
boo[i]=false;
}
}
}
for(int i=0;i<boo.length;i++){
if(!boo[i])
return false;
}
return true;
}
return false;
}
public int countNumber(String s,String y){ //此方法為我前面寫的字元串出現次數統計方法,不懂的可以看我前面的文章
int count=0;
String [] k=y.split(s);
if(y.lastIndexOf(s)==(y.length()-s.length()))
count=k.length;
else
count=k.length-1;
if(count==0)
System.out.println ("字元串\""+s+"\"在字元串\""+y+"\"沒有出現過");
else
return count;
return -1;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GoodLucky extends JFrame implements ActionListener{
JTextField tf = new JTextField(); //實例化一個文本域
//設置兩個按鈕
JButton b1 = new JButton("開始");
JButton b2 = new JButton("停止");
boolean isGo = false;
//構造函數
public GoodLucky(){
b1.setActionCommand("start");//在開始按鈕上設置一個動作監聽 start
JPanel p = new JPanel(); //實例化一個可視化容器
//將兩個按鈕添加到可視化容器上面,用add方法
p.add(b1);
p.add(b2);
//在兩個按鈕上增加監聽的屬性,自動調用下面的監聽處理方法actionPerformed(ActionEvent e),如果要代碼有更好的可讀性,可用內部類實現動作
//監聽處理。
b1.addActionListener(this);
b2.addActionListener(this);
//將停止按鈕設置為不可編輯(即不可按的狀態)
b2.setEnabled(false);
this.getContentPane().add(tf,"North"); //將上面的文本域放在面板的北方,也就是上面(上北下南左西右東)
this.getContentPane().add(p,"South"); //將可視化容器pannel放在南邊,也就是下面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置用戶在此窗體上發起 "close" 時默認執行的操作,參數EXIT_ON_CLOSE是使用 System exit 方法退出應用程序。僅在應用程序中使用
this.setSize(300,200); //設置面板大小,寬和高
this.setLocation(300,300); //設置面板剛開始的出現的位置
Cursor cu = new Cursor(Cursor.HAND_CURSOR); //用指定名稱創建一個新的定製游標對象,參數表示手狀游標類型
this.setCursor(cu); //為指定的游標設置游標圖像,即設置游標圖像為上面所創建的手狀游標類型
this.setVisible(true); //將面板可視化設置為true,即可視,如果為false,即程序運行時面板會隱藏
tf.setText("welcome you! "); //設置面板的標題為歡迎
this.go(); //調用go方法
}
public void go(){
while(true){ //這里是死循環,也就是說用戶不點擊停止按鈕的話他一直循環出現隨機數,直到用戶點擊停止按鈕循環才能推出,具體流程在actionPerformed方法中控制。
if(isGo == true){ //上面所定義的isGo的初始值為false,所以程序第一次到此會跳過
String s = ""; //設置空字元串
for(int j = 1; j <= 7;j++){ //產生7個隨機數
int i = (int)(Math.random() * 36) + 1;//每個隨機數產生方式,這里定義靈活,可以自由定義隨機數產生的方式
if(i < 10){
s = s + " 0" + i; //如果產生的隨機數小於10的話做處理:這里就牽扯到一個重要的概念,簡單敘述一下:
/*
當一個字元串與一個整型數項相加的意思是連接,上面的s = s + " 0" + i的意思是字元串s鏈接0再連接整型i值,而不會導致0和整型的i相加,
產生的效果為s0i,由於s為空字元串(上面定義過的),所以當i小於零時,在個位數前面加上0,比如產生的隨機數i為7的話,顯示效果為 07.
*/
}else{
s = s + " " + i; //如果產生的隨機數比10打的話,那麼加上空格顯示,即數字和數字之間有個空格
}
//以上循環循環七次,以保證能出現7個隨機數
}
tf.setText(s); //將產生的隨機數全部顯示在文本域上,用文本域對象tf調用它的設置文本的方法setText(String)實現。
}
//以下為線程延遲
try{
Thread.sleep(10); //線程類同步方法sleep,睡眠方法,括弧里的單位為ms。
}catch(java.lang.InterruptedException e){
e.printStackTrace(); //異常捕獲,不用多說。
}
}
}
//以下是上面設置的事件監聽的具體處理辦法,即監聽時間處理方法,自動調用
public void actionPerformed(ActionEvent e){ //傳入一個動作事件的參數e
String s = e.getActionCommand(); //設置字元串s來存儲獲得動作監聽,上面的start
/*
以下這個條件語句塊的作用為:用戶點擊開始後(捕獲start,用方法getActionCommand()),將命令觸發設置為true,從而執行上面的go方法中的循環體(因為循環體中要求isGo參數為true,而初始為false)。
執行循環快產生隨機數,並將開始按鈕不可編輯化,而用戶只可以使用停止按鈕去停止。如果用戶按下停止時,也就是沒有傳入參數「start」的時候,
執行else語句塊中的語句,isGo設置為false,將不執行上面go中的循環語句塊,從而停止產生隨機數,並顯示,並且把開始按鈕設置為可用,而把
停止按鈕設置為不可用,等待用戶按下開始再去開始新一輪循環產生隨機數。
*/
if(s.equals("start")){ //如果捕獲到start,也就是用戶觸發了動作監聽器,那麼下面處理
isGo = true; //設置isGo為true
b1.setEnabled(false); //將開始按鈕設置為不可用
b2.setEnabled(true); //將停止按鈕設置為可用
}else{
isGo = false; //將isGo設置為false,isGo為循環標志位
b2.setEnabled(false); //設置停止按鈕為不可用(注意看是b2,b2是停止按鈕)
b1.setEnabled(true); //設置開始按鈕為可用
}
}
public static void main(String[] args){
new GoodLucky(); //產生類的實例,執行方法
}
}
⑶ 急求一個關於UDP的JAVA實現多人聊天源代碼
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
class Server
{
public Server()
{
try
{
ServerSocket s=new ServerSocket(8888);
Socket ss=s.accept();
OutputStream out=ss.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
InputStream in=ss.getInputStream();
DataInputStream din=new DataInputStream(in);
System.out.print(din.readUTF()+"!");
dout.writeUTF("你已經連接到伺服器"+"\t"+"你的地址:"+ss.getInetAddress()+"\t"
+"你的鏈接埠:"+ss.getLocalPort()+"\n");
new ReadMessage(din).start();
new SendMessage(dout).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Server();
}
}
//接受客戶端信息
class ReadMessage extends Thread
{
private DataInputStream din;
public ReadMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while (true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"客戶端說:"+str);
if (str.equals("bye"))
{
System.out.println("客戶端下線!");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
// 發出伺服器信息
class SendMessage extends Thread
{
private DataOutputStream dout;
public SendMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
InputStreamReader inr=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inr);
String str;
try
{
while(true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("伺服器退出!");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
class Client
{
public Client()
{
try
{
Socket s=new Socket("192.168.1.2",8888);
InputStream in=s.getInputStream();
DataInputStream din=new DataInputStream(in);
OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
dout.writeUTF("伺服器你好!我是客戶端");
System.out.println(din.readUTF());
new Thread(new SenderMessage(dout)).start();
new Thread(new ReaderMessage(din)).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Client();
}
}
class ReaderMessage implements Runnable
{
private DataInputStream din;
public ReaderMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while(true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"伺服器說:"+str);
if (str.equals("bye"))
{
System.out.println("伺服器已經關閉,此程序自動退出!");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class SenderMessage implements Runnable
{
private DataOutputStream dout;
public SenderMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
String str;
InputStreamReader inf=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inf);
try
{
while (true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("客戶端自己退出!");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
⑷ 什麼是java源代碼 怎麼查看
不知道你說的是瀏覽器的還是什麼的,
如果是瀏覽器的那麼簡單找到工具-查看源代碼,你就能看見代碼了,
還有一個就是被編譯成class文件的java用反編譯工具可以看到源代碼,
如果以上都不是你想要的答案,那麼你所說的代碼就是程序員寫好的代碼文件
⑸ java計算器的源代碼
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public class Counter extends Frame
{
//聲明三個面板的布局
GridLayout gl1,gl2,gl3;
Panel p0,p1,p2,p3;
JTextField tf1;
TextField tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;
StringBuffer str;//顯示屏所顯示的字元串
double x,y;//x和y都是運算數
int z;//Z表示單擊了那一個運算符.0表示"+",1表示"-",2表示"*",3表示"/"
static double m;//記憶的數字
public Counter()
{
gl1=new GridLayout(1,4,10,0);//實例化三個面板的布局
gl2=new GridLayout(4,1,0,15);
gl3=new GridLayout(4,5,10,15);
tf1=new JTextField(27);//顯示屏
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setText("0");
tf2=new TextField(10);//顯示記憶的索引值
tf2.setEditable(false);
//實例化所有按鈕、設置其前景色並注冊監聽器
b0=new Button("Backspace");
b0.setForeground(Color.red);
b0.addActionListener(new Bt());
b1=new Button("CE");
b1.setForeground(Color.red);
b1.addActionListener(new Bt());
b2=new Button("C");
b2.setForeground(Color.red);
b2.addActionListener(new Bt());
b3=new Button("MC");
b3.setForeground(Color.red);
b3.addActionListener(new Bt());
b4=new Button("MR");
b4.setForeground(Color.red);
b4.addActionListener(new Bt());
b5=new Button("MS");
b5.setForeground(Color.red);
b5.addActionListener(new Bt());
b6=new Button("M+");
b6.setForeground(Color.red);
b6.addActionListener(new Bt());
b7=new Button("7");
b7.setForeground(Color.blue);
b7.addActionListener(new Bt());
b8=new Button("8");
b8.setForeground(Color.blue);
b8.addActionListener(new Bt());
b9=new Button("9");
b9.setForeground(Color.blue);
b9.addActionListener(new Bt());
b10=new Button("/");
b10.setForeground(Color.red);
b10.addActionListener(new Bt());
b11=new Button("sqrt");
b11.setForeground(Color.blue);
b11.addActionListener(new Bt());
b12=new Button("4");
b12.setForeground(Color.blue);
b12.addActionListener(new Bt());
b13=new Button("5");
b13.setForeground(Color.blue);
b13.addActionListener(new Bt());
b14=new Button("6");
b14.setForeground(Color.blue);
b14.addActionListener(new Bt());
b15=new Button("*");
b15.setForeground(Color.red);
b15.addActionListener(new Bt());
b16=new Button("%");
b16.setForeground(Color.blue);
b16.addActionListener(new Bt());
b17=new Button("1");
b17.setForeground(Color.blue);
b17.addActionListener(new Bt());
b18=new Button("2");
b18.setForeground(Color.blue);
b18.addActionListener(new Bt());
b19=new Button("3");
b19.setForeground(Color.blue);
b19.addActionListener(new Bt());
b20=new Button("-");
b20.setForeground(Color.red);
b20.addActionListener(new Bt());
b21=new Button("1/X");
b21.setForeground(Color.blue);
b21.addActionListener(new Bt());
b22=new Button("0");
b22.setForeground(Color.blue);
b22.addActionListener(new Bt());
b23=new Button("+/-");
b23.setForeground(Color.blue);
b23.addActionListener(new Bt());
b24=new Button(".");
b24.setForeground(Color.blue);
b24.addActionListener(new Bt());
b25=new Button("+");
b25.setForeground(Color.red);
b25.addActionListener(new Bt());
b26=new Button("=");
b26.setForeground(Color.red);
b26.addActionListener(new Bt());
//實例化四個面板
p0=new Panel();
p1=new Panel();
p2=new Panel();
p3=new Panel();
//創建一個空字元串緩沖區
str=new StringBuffer();
//添加面板p0中的組件和設置其在框架中的位置和大小
p0.add(tf1);
p0.setBounds(10,25,300,40);
//添加面板p1中的組件和設置其在框架中的位置和大小
p1.setLayout(gl1);
p1.add(tf2);
p1.add(b0);
p1.add(b1);
p1.add(b2);
p1.setBounds(10,65,300,25);
//添加面板p2中的組件並設置其的框架中的位置和大小
p2.setLayout(gl2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBounds(10,110,40,150);
//添加面板p3中的組件並設置其在框架中的位置和大小
p3.setLayout(gl3);//設置p3的布局
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b10);
p3.add(b11);
p3.add(b12);
p3.add(b13);
p3.add(b14);
p3.add(b15);
p3.add(b16);
p3.add(b17);
p3.add(b18);
p3.add(b19);
p3.add(b20);
p3.add(b21);
p3.add(b22);
p3.add(b23);
p3.add(b24);
p3.add(b25);
p3.add(b26);
p3.setBounds(60,110,250,150);
//設置框架中的布局為空布局並添加4個面板
setLayout(null);
add(p0);
add(p1);
add(p2);
add(p3);
setResizable(false);//禁止調整框架的大小
//匿名類關閉窗口
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1)
{
System.exit(0);
}
});
setBackground(Color.lightGray);
setBounds(100,100,320,280);
setVisible(true);
}
//構造監聽器
class Bt implements ActionListener
{
public void actionPerformed(ActionEvent e2)
{
try{
if(e2.getSource()==b1)//選擇"CE"清零
{
tf1.setText("0");//把顯示屏清零
str.setLength(0);//清空字元串緩沖區以准備接收新的輸入運算數
}
else if(e2.getSource()==b2)//選擇"C"清零
{
tf1.setText("0");//把顯示屏清零
str.setLength(0);
}
else if(e2.getSource()==b23)//單擊"+/-"選擇輸入的運算數是正數還是負數
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(-x));
}
else if(e2.getSource()==b25)//單擊加號按鈕獲得x的值和z的值並清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);//清空緩沖區以便接收新的另一個運算數
y=0d;
z=0;
}
else if(e2.getSource()==b20)//單擊減號按鈕獲得x的值和z的值並清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=1;
}
else if(e2.getSource()==b15)//單擊乘號按鈕獲得x的值和z的值並清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=2;
}
else if(e2.getSource()==b10)//單擊除號按鈕獲得x的值和z的值並空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=3;
}
else if(e2.getSource()==b26)//單擊等號按鈕輸出計算結果
{
str.setLength(0);
switch(z)
{
case 0 : tf1.setText(""+(x+y));break;
case 1 : tf1.setText(""+(x-y));break;
case 2 : tf1.setText(""+(x*y));break;
case 3 : tf1.setText(""+(x/y));break;
}
}
else if(e2.getSource()==b24)//單擊"."按鈕輸入小數
{
if(tf1.getText().trim().indexOf(′.′)!=-1)//判斷字元串中是否已經包含了小數點
{
}
else//如果沒數點有小
{
if(tf1.getText().trim().equals("0"))//如果初時顯示為0
{
str.setLength(0);
tf1.setText((str.append("0"+e2.getActionCommand())).toString());
}
else if(tf1.getText().trim().equals(""))//如果初時顯示為空則不做任何操作
{
}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
}
}
y=0d;
}
else if(e2.getSource()==b11)//求平方根
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText("數字格式異常");
if(x<0)
tf1.setText("負數沒有平方根");
else
tf1.setText(""+Math.sqrt(x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b16)//單擊了"%"按鈕
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(0.01*x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b21)//單擊了"1/X"按鈕
{
x=Double.parseDouble(tf1.getText().trim());
if(x==0)
{
tf1.setText("除數不能為零");
}
else
{
tf1.setText(""+(1/x));
}
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b3)//MC為清除內存
{
m=0d;
tf2.setText("");
str.setLength(0);
}
else if(e2.getSource()==b4)//MR為重新調用存儲的數據
{
if(tf2.getText().trim()!="")//有記憶數字
{
tf1.setText(""+m);
}
}
else if(e2.getSource()==b5)//MS為存儲顯示的數據
{
m=Double.parseDouble(tf1.getText().trim());
tf2.setText("M");
tf1.setText("0");
str.setLength(0);
}
else if(e2.getSource()==b6)//M+為將顯示的數字與已經存儲的數據相加要查看新的數字單擊MR
{
m=m+Double.parseDouble(tf1.getText().trim());
}
else//選擇的是其他的按鈕
{
if(e2.getSource()==b22)//如果選擇的是"0"這個數字鍵
{
if(tf1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作
{
}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
else if(e2.getSource()==b0)//選擇的是「BackSpace」按鈕
{
if(!tf1.getText().trim().equals("0"))//如果顯示屏顯示的不是零
{
if(str.length()!=1)
{
tf1.setText(str.delete(str.length()-1,str.length()).toString());//可能拋出字元串越界異常
}
else
{
tf1.setText("0");
str.setLength(0);
}
}
y=Double.parseDouble(tf1.getText().trim());
}
else//其他的數字鍵
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
}
catch(NumberFormatException e){
tf1.setText("數字格式異常");
}
catch( e){
tf1.setText("字元串索引越界");
}
}
}
public static void main(String args[])
{
new Counter();
}
}
你在JAVA的環境中運行一下。
這題目也是我的作業,我運行的了。
⑹ 求JAVA源代碼
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GradeStatistic {
public static void main(String[] args) {
GradeStatistic gs = new GradeStatistic();
List<Mark> list = new ArrayList<Mark>();
float sum = 0;
while(true){
Scanner sc = new Scanner(System.in);
System.out.print("Please input student name: ");
String name = sc.nextLine();
if(name.equals("end")){
break;
}
System.out.print("Please input student score: ");
float score = sc.nextFloat();
sum += score;
list.add(gs.new Mark(name, score));
}
float max = list.get(0).getScore();
float min = list.get(0).getScore();
for(Mark mark: list){
if(max < mark.getScore()){
max = mark.getScore();
}
if(min > mark.getScore()){
min = mark.getScore();
}
}
float average = sum / list.size();
System.out.println("Average is: " + average);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
private class Mark{
private String name;
private float score;
public Mark(String name, float score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public float getScore() {
return score;
}
}
}
----------------------
Please input student name: Zhang san
Please input student score: 100
Please input student name: Li Si
Please input student score: 91
Please input student name: Ec
Please input student score: 35
Please input student name: ma qi
Please input student score: 67
Please input student name: end
Average is: 73.25
Max is: 100.0
Min is: 35.0
⑺ 初學者請求java源代碼
class Car
{
// 車輛屬性
private String brand; // 品牌
private double engineDisplacement;// 排氣量
private double speed;// 當前速度
private boolean status;// 啟動狀態
private double maxSpeed;// 最大速度
public double getSpeed () {
return this.speed;
}
public Car(String brand, double engineDisplacement, double maxSpeed) {
this.brand = brand;
this.engineDisplacement = engineDisplacement;
this.maxSpeed = maxSpeed;
// 其他屬性也要有初始值,不然執行出錯。
this.speed = 0;
this.status = false;
}
/** 啟動 */
public void start() {
this.status = true;
printNowStatus ();
}
/** 關閉(熄火) */
public void stop() {
// 只在速度為0時關閉(貌似樓上兩位沒仔細看題…)
if (this.speed == 0)
{
this.status = false;
}
printNowStatus ();
}
/** 加速 */
public void speedUp() {
// 只在啟動時可以加速
if (this.status)
{
this.speed = this.speed + 20;
if (this.speed > this.maxSpeed)
{
this.speed = this.maxSpeed;
}
}
printNowStatus ();
}
/** 減速 */
public void slowDown() {
// 只在啟動時可以減速
if (this.status)
{
this.speed = this.speed - 10;
if (this.speed < 0)
{
this.speed = 0;
}
}
printNowStatus ();
}
/** 狀態列印,在每次啟動,加減速,關閉時調用 */
private void printNowStatus () {
System.out.println("轎車【" + this.brand + "】現在的啟動狀態是:" + this.status + "速度是:" + this.speed +"。");
}
}
public class TestCar
{
public static void main(String[] args)
{
Car myCar = new Car ("紅旗", 2, 120);
//啟動
myCar.start();
// 循環加速到120
while (myCar.getSpeed() < 120)
{
myCar.speedUp();
}
//循環減速
while (myCar.getSpeed() > 0)
{
myCar.slowDown();
}
//關閉
myCar.stop();
}
}
/* 直接拿文本寫的,我用的電腦沒裝jdk,樓主自己到Java開發環境下調試,應該沒什麼問題… */
⑻ java里一個簡單類的源代碼
SessionFactory sessionFactory = cfg.buildSessionFactory();
你沒有理解這行代碼的意義,這行代碼cfg.buildSessionFactory()返回的不是SessionFactory,而是實現了SessionFactory 這個介面的一個類,這個具體實現了SessionFactory介面的類是在hibernate的配置文件中配置的
⑼ 求一個用JAVA寫的簡單的記事本源代碼程序
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;
class MyMenuBar extends MenuBar{
public MyMenuBar(Frame parent){
parent.setMenuBar(this);
}
public void addMenus(String [] menus){
for(int i=0;i<menus.length;i++)
add(new Menu(menus[i]));
}
public void addMenuItems(int menuNumber,String[] items){
for(int i=0;i<items.length;i++){
if(items[i]!=null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else getMenu(menuNumber).addSeparator();
}
}
public void addActionListener(ActionListener al){
for(int i=0;i<getMenuCount();i++)
for(int j=0;j<getMenu(i).getItemCount();j++)
getMenu(i).getItem(j).addActionListener(al);
}
}
class MyFile{
private FileDialog fDlg;
public MyFile(Frame parent){
fDlg=new FileDialog(parent,"",FileDialog.LOAD);
}
private String getPath(){
return fDlg.getDirectory()+"\\"+fDlg.getFile();
}
public String getData() throws IOException{
fDlg.setTitle("打開");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br=new BufferedReader(new FileReader(getPath()));
StringBuffer sb=new StringBuffer();
String aline;
while((aline=br.readLine())!=null)
sb.append(aline+'\n');
br.close();
return sb.toString();
}
public void setData(String data) throws IOException{
fDlg.setTitle("保存");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw=new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}
class MyClipboard{
private Clipboard cb;
public MyClipboard(){
cb=Toolkit.getDefaultToolkit().getSystemClipboard();
}
public void setData(String data){
cb.setContents(new StringSelection(data),null);
}
public String getData(){
Transferable content=cb.getContents(null);
try{
return (String) content.getTransferData(DataFlavor.stringFlavor);
//DataFlavor.stringFlavor會將剪貼板中的字元串轉換成Unicode碼形式的String對象。
//DataFlavor類是與存儲在剪貼板上的數據的形式有關的類。
}catch(Exception ue){}
return null;
}
}
class MyFindDialog extends Dialog implements ActionListener{
private Label lFind=new Label("查找字元串");
private Label lReplace=new Label("替換字元串");
private TextField tFind=new TextField(10);
private TextField tReplace=new TextField(10);
private Button bFind=new Button("查找");
private Button bReplace=new Button("替換");
private TextArea ta;
public MyFindDialog(Frame owner,TextArea ta){
super(owner,"查找",false);
this.ta=ta;
setLayout(null);
lFind.setBounds(10,30,80,20);
lReplace.setBounds(10,70,80,20);
tFind.setBounds(90,30,90,20);
tReplace.setBounds(90,70,90,20);
bFind.setBounds(190,30,80,20);
bReplace.setBounds(190,70,80,20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyFindDialog.this.dispose();
}
});
}//構造函數結束
public void showFind(){
setTitle("查找");
setSize(280,60);
setVisible(true);
}
public void showReplace(){
setTitle("查找替換");
setSize(280,110);
setVisible(true);
}
private void find(){
String text=ta.getText();
String str=tFind.getText();
int end=text.length();
int len=str.length();
int start=ta.getSelectionEnd();
if(start==end) start=0;
for(;start<=end-len;start++){
if(text.substring(start,start+len).equals(str)){
ta.setSelectionStart(start);
ta.setSelectionEnd(start+len);
return;
}
}
//若找不到待查字元串,則將游標置於末尾
ta.setSelectionStart(end);
ta.setSelectionEnd(end);
}
public Button getBFind() {
return bFind;
}
private void replace(){
String str=tReplace.getText();
if(ta.getSelectedText().equals(tFind.getText()))
ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd());
else find();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bFind)
find();
else if(e.getSource()==bReplace)
replace();
}
}
public class MyMemo extends Frame implements ActionListener{
private TextArea editor=new TextArea(); //可編輯的TextArea
private MyFile mf=new MyFile(this);//MyFile對象
private MyClipboard cb=new MyClipboard();
private MyFindDialog findDlg=new MyFindDialog(this,editor);
public MyMemo(String title){ //構造函數
super(title);
MyMenuBar mb=new MyMenuBar(this);
//添加需要的菜單及菜單項
mb.addMenus(new String[]{"文件","編輯","查找","幫助"});
mb.addMenuItems(0,new String[]{"新建","打開","保存",null,"全選"});
mb.addMenuItems(1,new String[]{"剪貼","復制","粘貼","清除",null,"全選"});
mb.addMenuItems(2,new String[]{"查找",null,"查找替換"});
mb.addMenuItems(3,new String[]{"我的記事本信息"});
add(editor); //為菜單項注冊動作時間監聽器
mb.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyMemo.this.dispose();
}
}); //分號不能忘了
} //構造函數完
public void actionPerformed(ActionEvent e){
String selected=e.getActionCommand(); //獲取菜單項標題
if(selected.equals("新建"))
editor.setText("");
else if(selected.equals("打開")){
try{
editor.setText(mf.getData());
}catch(IOException ie){}
}
else if(selected.equals("保存")){
try{
mf.setData(editor.getText());
}catch(IOException ie){}
}
else if(selected.equals("退出")){
dispose();
}
else if(selected.equals("剪貼")){
//將選中的字元串復制到剪貼板中並清除字元串
cb.setData(editor.getSelectedText());
editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());
}
else if(selected.equals("復制")){
cb.setData(editor.getSelectedText());
}
else if(selected.equals("粘貼")){
String str=cb.getData();
editor.replaceRange(str,editor.getSelectionStart(),editor.getSelectionEnd());
//粘貼在游標位置
}
else if(selected.equals("清除")){
editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());
}
else if(selected.equals("全選")){
editor.setSelectionStart(0);
editor.setSelectionEnd(editor.getText().length());
}
else if(selected.equals("查找")){
findDlg.showFind();
}
else if(selected.equals("查找替換")){
findDlg.showReplace();
}
}
public static void main(String[] args){
MyMemo memo=new MyMemo("記事本");
memo.setSize(650,450);
memo.setVisible(true);
}
}