//It is just a simulation.None of your account information will be saved
//after you close this program.import java.io.Serializable;
import java.util.Date;import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;public class BankManager implements Serializable
{
//The maximum user accounts.
public static final int MAX_AMOUNT=100;
//Each element of this array stores a user's information.
private UserAccount[] users=new UserAccount[this.MAX_AMOUNT];
//The registered number of users.
protected int num=0;
//Construct a BankManager.
public BankManager()
{
start();
}
//Start this BankManager.
public void start()
{
System.out.println("BankManger is starting.");
System.out.println("If you want to register a new account.Please enter \"r\" .");
System.out.println("If you want to bank in money.Please enter \"i\" .");
System.out.println("If you want to bank out money.Please enter \"o\" .");
System.out.println("If you want to see about your remanent.Please enter \"s\" .");
System.out.println("If you want to check all your bank record.Please enter \"p\" .");
System.out.println("If you want to exit this BankManager.Please enter \"e\" .");
this.solve();
}
//Register a user account.
public boolean registerUser(int money,char[] password,char[] repassword)
{
if(num==99||!check(password,repassword))
{
return false;
}
else
{
Date date=new Date();
UserAccount ua=new UserAccount(money,date,password);
users[num]=ua;
num++;
return true;
}
}
//after you close this program.import java.io.Serializable;
import java.util.Date;import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;public class BankManager implements Serializable
{
//The maximum user accounts.
public static final int MAX_AMOUNT=100;
//Each element of this array stores a user's information.
private UserAccount[] users=new UserAccount[this.MAX_AMOUNT];
//The registered number of users.
protected int num=0;
//Construct a BankManager.
public BankManager()
{
start();
}
//Start this BankManager.
public void start()
{
System.out.println("BankManger is starting.");
System.out.println("If you want to register a new account.Please enter \"r\" .");
System.out.println("If you want to bank in money.Please enter \"i\" .");
System.out.println("If you want to bank out money.Please enter \"o\" .");
System.out.println("If you want to see about your remanent.Please enter \"s\" .");
System.out.println("If you want to check all your bank record.Please enter \"p\" .");
System.out.println("If you want to exit this BankManager.Please enter \"e\" .");
this.solve();
}
//Register a user account.
public boolean registerUser(int money,char[] password,char[] repassword)
{
if(num==99||!check(password,repassword))
{
return false;
}
else
{
Date date=new Date();
UserAccount ua=new UserAccount(money,date,password);
users[num]=ua;
num++;
return true;
}
}
解决方案 »
- 菜鸟两个关于number format的问题
- 无数遍地看了生产者消费者的代码,但自己编写总会有问题,大家看看我的半段代码如何思考下去??
- 关于泛型的一个问题
- 如何方便的发布JAVA的WEB程序?
- 请高手解决ant部署tomcat问题
- java 关于二进制数据的处理 急啊
- java中是否有传真的api可用。如果没有,怎样调用外部的fax app实现这个功能?
- tomcat里面有没有定时执行的服务?或者怎样自己编写?
- 谁有VisualCafe的光盘啊?
- 卖身求解难题!!!!!
- 我现在使用adsl上网,和几个同学通过交换机路由模式上的,我想在我的机器上建立ftp服务器,请问我如何建设,也就是没有固定的ip。希望能
- 开始动手研究布局管理,呵呵,大家进来看看,给点我意见。谢谢!
}
//Bank some money in the specified account.
public synchronized boolean bankIn(int money,int id)
{
if(id>this.num||users[0]==null)
{
return false;
}
else
{
return getUser(id).bank(money);
}
}
//Take out some money from the specified account.
public synchronized boolean bankOut(int money,int id,char[] password)
{
if(id>this.num||users[0]==null)
{
return false;
}
else
{
return getUser(id).takeout(money,password);
}
}
//Print the recorder of the specified account.
public void pintRecorder(int id)
{
if(id>this.num||users[0]==null)
{
System.out.println("Account not exit.");
}
else
{
String record=getUser(id).getRecord();
System.out.println(record);
}
}
//See about the user's account.
public int seeAbout(int id)
{
UserAccount u=(UserAccount)getUser(id);
if(u==null)
{
return -1;
}
else
return u.getRemanent();
}
//Get the specified user.
private UserAccount getUser(int id)
{
return (UserAccount)users[id];
}
//Solve the user's request.
protected void solve()
{
while(true)
{
String s=getInput();
char ins=s.charAt(0);
if(ins!='r'&&ins!='i'&&ins!='o'&&ins!='s'&&ins!='p'&&ins!='e')
{
System.out.println("Unsupported command.");
}
else{
switch(ins)
{
//Register a user account.
case 'r':
{
try
{
System.out.println("Registering new user account......");
System.out.println("Please enter your account intial money:");
Integer money=new Integer(getInput());
System.out.println("Please enter your password: ");
char[] p=getInput().toCharArray();
System.out.println("Please enter you password again:");
char[] rp=getInput().toCharArray();
boolean b=this.registerUser(money.intValue(),p,rp);
if(b)
{
System.out.println
("Register successfully. Your account is:"+(this.num-1));
break;
}
else
{
System.out.println
("Register failed.Please enter \"r\" and try again.");
break;
}
}
catch(Exception e)
{
System.out.println("Error input.Please try again.");
break;
}
}
//Bank in some money.
case 'i':
{
try
{
System.out.println("Banking in money......");
System.out.println("Please enter the account you want to bank in:");
Integer acc=new Integer(getInput());
System.out.println("Please enter the money you want to bank in:");
Integer money=new Integer(getInput());
boolean b=this.bankIn(money.intValue(),acc.intValue());
if(b)
{
System.out.println("Bank in successfully.");
break;
}
else
{
System.out.println
("Bank in failed. Please enter \"i\"and try again.");
break;
}
}
catch(Exception e)
{
System.out.println("Error input.Please try again.");
break;
}
}
//Bank out some money.
case 'o':
{
try
{
System.out.println("Banking out money......");
System.out.println("Please enter your account:");
Integer acc=new Integer(getInput());
System.out.println("Please enter your password:");
char[] p=getInput().toCharArray();
System.out.println("Please enter the money you want to bank out:");
Integer m=new Integer(getInput());
boolean b=this.bankOut(m.intValue(),acc.intValue(),p);
if(b)
{
System.out.println("Bank out successfully.");
break;
}
else
{
System.out.println
("Bank out failed.Please enter \"o\"and try again.");
break;
}
}
catch(Exception e)
{
System.out.println("Error input.Please try again.");
break;
}
}
//See about the user's account.
case 's':
{
try
{
System.out.println("Seeing about your bank recorder......");
System.out.println("Please enter your account.");
Integer acc=new Integer(getInput());
int money=this.seeAbout(acc.intValue());
if(money==-1)
{
System.out.println("Account not exit.");
break;
}
else
{
System.out.println("Your remanent money is:"+money);
break;
}
}
catch(Exception e)
{
System.out.println("Error input.Please try again.");
break;
}
}
//Print all the user's bank recorder.
case 'p':
{
System.out.println("Printing all your bank recorder......");
System.out.println("Please enter your account:");
Integer acc=new Integer(getInput());
this.pintRecorder(acc.intValue());
break;
}
//Exit this BankManager.
case 'e':
{
System.out.println("Exiting this BankManager......");
System.exit(0);
}
}
}
//Get the user's input.
private String getInput()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String ss=br.readLine();
return ss;
}
catch(IOException e)
{
System.out.println(e.toString());
return null;
}
}
//Check if the two password is the same.
public static boolean check(char[] p1,char[] p2)
{
int l1=p1.length;
int l2=p2.length;
if(l1!=l2)
{
return false;
}
else
{
boolean b=true;
for(int i=0;i<p1.length;i++)
{
if(p1[i]!=p2[i])
b=false;
}
return b;
}
}
//The main method.
public static void main(String args[])
{
BankManager bm=new BankManager();
bm.start();
}
//The inner class stores the user's information.
private class UserAccount
{
//The remanent money.
public int moneyrem=0;
//The Data of this userAcount upbuilded.
public Date date=null;
//This StringBuffer stores the bankout recorder.
public StringBuffer record=new StringBuffer();
//The password of this user.
private char[] password=null;
public UserAccount(int money,Date date,char[] password)
{
this.moneyrem=money;
this.date=date;
this.password=password;
}
//Bank some money.
public boolean bank(int money)
{
this.moneyrem+=money;
return true;
}
//Take out some money.
public boolean takeout(int money,char[] repassword)
{
if(money>this.moneyrem||!BankManager.check(password,repassword))
{
return false;
}
else
{
this.moneyrem-=money;
//Record this bankout recorder.
Date date=new Date();
String s=new String
("Date:"+date.toLocaleString()+" bankout:"+money+" remanent:"+moneyrem+"\n");
record.append(s);
return true;
}
}
//See about the remanent money.
public int getRemanent()
{
return this.moneyrem;
}
//See about the date this user banked money.
public Date getBankDate()
{
return this.date;
}
//Get the bankout recorder.
public String getRecord()
{
return record.toString();
}
}
}
楼主不如用jdk带的demo吧,那个才清楚呢
import java.io.*;public class myFileDialog extends Frame{
//初试化变量
String s = "This is TextArea.";
TextArea t =new TextArea (s ,5,5);
byte bu[] =new byte [5000];
String ss,sb, getstr,strbuf;
//布局控制
public myFileDialog(){
super ("File Dialog");
pack();
resize(400,250);
MenuBar mb =new MenuBar ();
Menu m1 = new Menu("File");
m1.add(new MenuItem("New Window"));
m1.add (new MenuItem("Open"));
m1.add (new MenuItem("Save"));
m1.add (new MenuItem("Close"));
m1.add(new MenuItem("Exit"));
mb.add(m1);
Menu m2 =new Menu ("Edit");
m2.add (new MenuItem ("Copy"));
m2.add (new MenuItem ("Reset"));
m2.add (new MenuItem("Delete"));
mb.add(m2);
setMenuBar(mb);
Panel p = new Panel();
p.add(new Button("Clear"));
p.add (new Button ("Reset"));
p.add ( new Button ("Exit"));
add ( "South", p);
add ("Center",t);
show();
}
//main函数
public static void main (String argd[]){
myFileDialog win1 =new myFileDialog();
}
//事件监听
public boolean handleEvent (Event e){
if (e.id ==Event.WINDOW_DESTROY){
System.exit(0);
return true;
}
if (e.id== Event.ACTION_EVENT){
if((e.arg).equals("Clear")){
s = t.getText();
t.setText("");
return true;
}
if((e.arg).equals("Reset")){
t.setText(s);
return true ;
}
if ((e.arg).equals("Exit")){
System.exit(0);
return true;
}
if ((e.arg).equals("New Window")){
new myFileDialog();
return true;
}
if ((e.arg).equals("Open")){
openFile();
return true;
}
if ((e.arg).equals("Save")){
saveFile();
return true;
}
if ((e.arg).equals("Close")){
dispose();
return true;
}
}
return false ;
}
//打开文件
public void openFile(){
FileDialog fd = new FileDialog(this,"Open File");
fd.show();
ss = fd.getFile();
File f = new File(ss);
FileInputStream fo;
try{
fo= new FileInputStream(f);
try{
fo.read(bu);
fo.close();
}
catch (IOException fioe){}
}
catch (FileNotFoundException fe){}
ByteArrayOutputStream st;
try{
st= new ByteArrayOutputStream();
st.write(bu);
sb= st.toString();
t.setText(sb);
}
catch(IOException fiose){}
}
//完成saveFile即保存文件的功能
public void saveFile(){
int SAVE =1 ;
getstr = t.getText();
StringBufferInputStream sst;
sst =new StringBufferInputStream(getstr);
sst.read(bu,0,5000);
FileDialog d;
d = new FileDialog(this, "Save File",SAVE);
d.show();
ss=d.getFile();
File sf = new File(ss);
FileOutputStream sfo;
try{
sfo =new FileOutputStream(sf);
try{
sfo.write(bu);
sfo.close();
}
catch(IOException sfioe){}
}
catch (IOException sfe){}
}
}
import java.util.*;
import java.awt.event.*;
public class frameClock extends Frame implements Runnable{
int x=100,y=100,r=60;
Thread timer=null;
public class WindowCloser extends WindowAdapter
{ public void windowClosing(WindowEvent e)
{System.exit(0);} }
frameClock()
{ super("Clock");
setBackground(Color.PINK);
setSize(200,200);
show();
addWindowListener(new WindowCloser());
}
public void paint(Graphics g)
{ Date date=new Date();
int h=date.getHours();
int m=date.getMinutes();
int s=date.getSeconds(); g.setColor(Color.yellow);
g.fillOval(x-r,y-r,2*r,2*r);
g.setColor(Color.red);
g.drawLine(x,y,(int)(x+0.8*r*Math.sin(s*3.14/30)),(int)(y-0.8*r*Math.cos(s*3.14/30)));
g.setColor(Color.blue);
g.drawLine(x,y,(int)(x+0.6*r*Math.sin(m*3.14/30)),(int)(y-0.6*r*Math.cos(m*3.14/30)));
g.setColor(Color.black);
g.drawLine(x,y,(int)(x+0.5*r*Math.sin(h*3.14/6)),(int)(y-0.5*r*Math.cos(h*3.14/6)));
}
public void start()
{ if(timer==null){Thread timer=new Thread(this);timer.start(); }
} public void run()
{while(true)
{repaint();
try{
Thread.sleep(1000); }catch(Exception e){}
}} public static void main(String arfs[])
{ frameClock f=new frameClock();
f.start();
}}
import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.awt.event.*;
import com.borland.jbcl.layout.*;
public class DispDate extends JFrame{
private JButton exitButton = new JButton();
private BorderLayout borderLayout1 = new BorderLayout();
private JLabel label = new JLabel();
DispDate() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void setLabel(String str){
label.setText(str);
}
private void jbInit() throws Exception {
label.setText(new Date().toString());
this.getContentPane().add(label, BorderLayout.NORTH);
this.getContentPane().add(exitButton, BorderLayout.SOUTH);
exitButton.setText("退出");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
exitButton_actionPerformed(e);
}
});
} void exitButton_actionPerformed(ActionEvent e) {
System.exit(21); }
public static void main(String[] args) throws Exception{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
DispDate ct=new DispDate();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = ct.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
ct.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
ct.setVisible(true);
ct.setTitle("时钟") ; ct.pack(); MyThread a=new MyThread();
a.start();
for(int i=0;true;i++){
a.sleep(1000);
System.out.println(new Date().toString());
ct.setLabel(new Date().toString());
}
}
}
class MyThread extends Thread{
public void run() {
}
}
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
}
}
//制作一个简单的计数器;
//用到五个按钮,分别为+,-,*,/和clear(表示清除);
//作者:尹立
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener{
private Button Pulsbutton; //加法按钮;
private Button Substractbutton;//减法按钮;
private Button Multiplybutton;//乘法按钮;
private Button Dividebutton; //除法按钮;
private Button Clearbutton; //清除按钮;
private Button Equalbutton; //等于按钮;
private Button button0;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private TextField InTextField;//输入框;
private TextField OutTextField;//输出框;
private Label InLabel; //输入标记;
private Label OutLabel; //输出标记;
public Calculator()
{
super("简单计算器");
this.setSize(500,400);
this.setLayout(null); //设置布局;
this.setLocation(300,200);
//实现按钮0-----9;
button0=new Button(" 0 ");
button0.addActionListener(this);
this.add(button0);
button0.setBounds(60,100,60,20);
button1=new Button(" 1 ");
button1.addActionListener(this);
this.add(button1);
button1.setBounds(200,100,60,20);
button2=new Button(" 2 ");
button2.addActionListener(this);
this.add(button2);
button2.setBounds(340,100,60,20);
button3=new Button(" 3 ");
button3.addActionListener(this);
this.add(button3);
button3.setBounds(60,140,60,20);
button4=new Button(" 4 ");
button4.addActionListener(this);
this.add(button4);
button4.setBounds(200,140,60,20);
button5=new Button(" 5 ");
button5.addActionListener(this);
this.add(button5);
button5.setBounds(340,140,60,20);
button6=new Button(" 6 ");
button6.addActionListener(this);
this.add(button6);
button6.setBounds(60,180,60,20);
button7=new Button(" 7 ");
button7.addActionListener(this);
this.add(button7);
button7.setBounds(200,180,60,20);
button8=new Button(" 8 ");
button8.addActionListener(this);
this.add(button8);
button8.setBounds(340,180,60,20);
button9=new Button(" 9 ");
button9.addActionListener(this);
this.add(button9);
button9.setBounds(60,220,60,20);
//实现+ - * / = clear按钮;
Pulsbutton=new Button(" + ");
Pulsbutton.addActionListener(this);
this.add(Pulsbutton);
Pulsbutton.setBounds(200,220,60,20);
/***********************/
Substractbutton=new Button(" - ");
Substractbutton.addActionListener(this);
this.add(Substractbutton);
Substractbutton.setBounds(340,220,60,20);
/********************/
Multiplybutton=new Button(" * ");
Multiplybutton.addActionListener(this);
this.add(Multiplybutton);
Multiplybutton.setBounds(60,260,60,20);
/****************************/
Dividebutton=new Button(" / ");
Dividebutton.addActionListener(this);
this.add(Dividebutton);
Dividebutton.setBounds(200,260,60,20);
/***************************/
Equalbutton=new Button(" = ");
Equalbutton.addActionListener(this);
this.add(Equalbutton);
Equalbutton.setBounds(340,260,60,20);
/**********************/
Clearbutton=new Button("清除");
Clearbutton.addActionListener(this);
this.add(Clearbutton);
Clearbutton.setBounds(60,300,100,20);
/***************************/
InTextField=new TextField(20);
this.add(InTextField);
InTextField.setBounds(100,60,80,20);
OutTextField=new TextField(20);
this.add(OutTextField);
OutTextField.setBounds(300,60,80,20);
/**************************/
InLabel=new Label("请输入表达式");
this.add(InLabel);
InLabel.setBounds(20,60,80,20);
OutLabel=new Label("结果为");
this.add(OutLabel);
OutLabel.setBounds(220,60,80,20);
/*************************/
this.show();
/******增加窗口监听器*******/
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
);
}//Calculator();
//按钮接口实现;
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button0)
{
InTextField.setText(InTextField.getText()+"0");
}
if(e.getSource()==button1)
{
InTextField.setText(InTextField.getText()+"1");
}
if(e.getSource()==button2)
{
InTextField.setText(InTextField.getText()+"2");
}
if(e.getSource()==button3)
{
InTextField.setText(InTextField.getText()+"3");
}
if(e.getSource()==button4)
{
InTextField.setText(InTextField.getText()+"4");
}
if(e.getSource()==button5)
{
InTextField.setText(InTextField.getText()+"5");
}
if(e.getSource()==button6)
{
InTextField.setText(InTextField.getText()+"6");
}
if(e.getSource()==button7)
{
InTextField.setText(InTextField.getText()+"7");
}
if(e.getSource()==button8)
{
InTextField.setText(InTextField.getText()+"8");
}
if(e.getSource()==button9)
{
InTextField.setText(InTextField.getText()+"9");
}
if(e.getSource()==Pulsbutton)
{
InTextField.setText(InTextField.getText()+"+");
}
if(e.getSource()==Substractbutton)
{
InTextField.setText(InTextField.getText()+"-");
}
if(e.getSource()==Multiplybutton)
{
InTextField.setText(InTextField.getText()+"*");
}
if(e.getSource()==Dividebutton)
{
InTextField.setText(InTextField.getText()+"/");
}
if(e.getSource()==Clearbutton)
{
InTextField.setText("");
OutTextField.setText("");
}
if(e.getSource()==Equalbutton)
{
float sum=0;
sum=this.calculate();
OutTextField.setText(""+sum);
InTextField.setText(""+(int)sum);
}
}
public float calculate()
{
String ptr=InTextField.getText();
int length=ptr.length();
char[] str=ptr.toCharArray();
float sum=0;
int op1=0,op2=0;
int i=0;
char ch='0';
try{
while(str[i]<='9'&&str[i]>='0'&&i<length)
{
op1=op1*10;
op1+=str[i]-'0';
i++;
}
if(i>=length)
return op1;
else
{
ch=str[i];
i++;
}
if(i>=length)
return op1;
while(str[i]<='9'&&str[i]>='0'&&i<length)
{
op2=op2*10;
op2+=str[i]-'0';
i++;
}
}
catch(Exception e)
{
System.out.println(e);
}
switch(ch)
{
case '+':
sum=(float)op1+(float)op2;
break;
case '-':
sum=(float)op1-(float)op2;
break;
case '*':
sum=(float)op1*(float)op2;
break;
case '/':
sum=(float)op1/(float)op2;
break;
}
return sum;
}
public static void main(String args[])
{
Calculator yl=new Calculator();
}//main
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;public class Gobang2 extends Applet implements MouseListener
{
int grid[][] = new int[14][14];
boolean playerAGo = false;//Player
boolean playerBGo = false;//CPU
boolean start = true;
int ACount = 0;
int BCount = 0;
int count = 0; boolean playerATable[][][] = new boolean[14][14][480];
boolean playerBTable[][][] = new boolean[14][14][480];
int playerAGrades[][] = new int[14][14];
int playerBGrades[][] = new int[14][14];
int playerAGrade;
int playerBGrade;
int win[][] = new int[2][480];
Image aImage;
Image bImage;
Image bg;
void initVarible()
{
for(int i = 0 ; i < 14 ; i++)
for(int j = 0 ; j < 14 ; j++)
grid[i][j] = 2;
for(int i = 0 ; i < 14 ; i++)
for(int j = 0 ; j < 10 ; j++)
{
for(int k = 0 ; k < 5 ; k++)
{
playerATable[j+k][i][count] = true;
playerBTable[j+k][i][count] = true;
}
count++;
}
for(int i = 0 ; i < 14 ; i++)
for(int j = 0 ; j < 10 ; j++)
{
for(int k = 0 ; k < 5 ; k++)
{
playerATable[i][j+k][count] = true;
playerBTable[i][j+k][count] = true;
}
count++;
}
for(int i = 0 ; i < 10 ; i++)
for(int j = 0 ; j < 10 ; j++)
{
for(int k = 0 ; k < 5 ; k++)
{
playerATable[i+k][j+k][count] = true;
playerBTable[i+k][j+k][count] = true;
}
count++;
}
for(int i = 0 ; i < 10 ; i++)
for(int j = 13 ; j >= 4 ; j--)
{
for(int k = 0 ; k < 5 ; k++)
{
playerATable[j-k][i+k][count] = true;
playerBTable[j-k][i+k][count] = true;
}
count++;
}
}
boolean isAWon()
{
for(int j = 0 ; j < 480 ; j++)
{
if(win[0][j] == 5)
return true;
}
return false;
}
boolean isBWon()
{
for(int j = 0 ; j < 480 ; j++)
{
if(win[1][j] == 5)
return true;
}
return false;
}
{
int m,n;
for(int i = 0 ; i < 14 ; i++)
for(int j = 0 ; j < 14 ; j++)
{
playerAGrades[i][j] = 0;
if(grid[i][j] == 2)
for(int k = 0 ; k < 480 ; k++)
if(playerATable[i][j][k])
{
switch(win[0][k])
{
case 1:
playerAGrades[i][j] += 5;
break;
case 2:
playerAGrades[i][j] += 50;
break;
case 3:
playerAGrades[i][j] += 100;
break;
case 4:
playerAGrades[i][j] += 400;
break;
}
}
}
for(int i = 0 ; i < 14 ; i++)
for(int j = 0 ; j < 14 ; j++)
{
playerBGrades[i][j] = 0;
if(grid[i][j] == 2)
for(int k = 0 ; k < 480 ; k++)
if(playerBTable[i][j][k])
{
switch(win[1][k])
{
case 1:
playerBGrades[i][j] += 5;
break;
case 2:
playerBGrades[i][j] += 50;
break;
case 3:
playerBGrades[i][j] += 100;
break;
case 4:
playerBGrades[i][j] += 400;
break;
}
}
}
if(start)
{
if(grid[5][5] == 2)
{
m = 5;
n = 5;
}
else
{
m = 6;
n = 6;
}
start = false;
}
else
{
int ma = 0,na = 0,md = 0,nd = 0;
for(int i = 0 ; i < 14 ; i++)
for(int j = 0 ; j < 14 ; j++)
if(grid[i][j] == 2)
{
if(playerBGrades[i][j] >= playerBGrade)
{
playerBGrade = playerBGrades[i][j];
ma = i;
na = j;
}
if(playerAGrades[i][j] >= playerAGrade)
{
playerAGrade = playerAGrades[i][j];
md = i ;
nd = j;
}
}
if(playerAGrade <= playerBGrade)
{
m = ma;
n = na;
}
else
{
m = md;
n = nd;
}
}
playerAGrade = 0;
playerBGrade = 0;
grid[m][n] = 1;
BCount++;
repaint();
for(int i = 0 ; i < 480 ; i++)
{
if(playerBTable[m][n][i] && win[1][i] != 7)
{
win[1][i]++;
}
if(playerATable[m][n][i])
{
playerATable[m][n][i] = false;
win[0][7] = 7;
}
}
playerBGo = false;
playerAGo = true;
}
public void init()
{
initVarible();
if((int)(Math.random()*2)==1)
{
ComAI();
}
else
{
playerAGo = true;
}
aImage = getImage(getCodeBase(), "images/A2.gif");
bImage = getImage(getCodeBase(), "images/B2.gif");
bg = getImage(getCodeBase(), "images/bg.jpg"); addMouseListener(this);
} public void destroy()
{
removeMouseListener(this);
}
public void paint(Graphics g)
{
Dimension d = getSize();
g.setColor(Color.black);
int xoff = d.width / 14;
int yoff = d.height / 14;
g.drawImage(bg, 0 , 0 , this);
for(int i = 0 ; i < 15 ; i++)
{
g.drawLine(i*xoff+xoff/2, yoff/2, i*xoff+xoff/2, d.height-yoff/2);
g.drawLine(xoff/2, i*yoff+yoff/2, d.width-xoff/2, i*yoff+yoff/2);
}
for(int i = 0 ; i < 14 ; i++)
for(int j = 0 ; j < 14 ; j++)
{
if(grid[i][j] == 0)
g.drawImage(aImage, i*xoff+1 , j*yoff+1 , this);
else if(grid[i][j] == 1)
g.drawImage(bImage, i*xoff+1 , j*yoff+1, this);
}
if(isAWon())
{
g.setColor(Color.RED);
g.setFont(new Font("ËÎÌå",Font.PLAIN,18));
g.drawString("Íæ¼Ò»ñʤ£¬Ë¢ÐÂÒ³ÃæÓÎÏ·ÖØпªÊ¼£¡",0,16);
play(getCodeBase(), "audio/WIN.wav");
}
if(isBWon())
{
g.setColor(Color.BLUE);
g.setFont(new Font("ËÎÌå",Font.PLAIN,18));
g.drawString("µçÄÔ»ñʤ£¬Ë¢ÐÂÒ³ÃæÓÎÏ·ÖØпªÊ¼£¡",0,16);
play(getCodeBase(), "audio/WIN.wav");
}
if(ACount == 98 && BCount == 98)
{
g.setColor(Color.BLACK);
g.setFont(new Font("ËÎÌå",Font.BOLD,20));
g.drawString("Ë«·½Õ½ÆÀ£¬Ë¢ÐÂÒ³ÃæÓÎÏ·ÖØпªÊ¼£¡",0,16);
play(getCodeBase(), "audio/WIN.wav");
}
}
public void mouseReleased(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
Dimension d = getSize();
int xoff = d.width / 14;
int yoff = d.height / 14;
int Px = x/xoff;
int Py = y/yoff;
if(playerAGo && grid[Px][Py] == 2 && !isAWon() && !isBWon())
{
grid[Px][Py] = 0;
ACount++;
play(getCodeBase(), "audio/STAB3.wav");
for(int i = 0 ; i < 480 ; i++)
{
if(playerATable[Px][Py][i] && win[0][i]!=7)
{
win[0][i]++;
}
if(playerBTable[Px][Py][i])
{
playerBTable[Px][Py][i] = false;
win[1][i] = 7;
}
}
playerAGo = false;
playerBGo = true;
repaint();
if(!isAWon())
{
ComAI();
}
} else
{
play(getCodeBase(), "audio/OHOH.wav");
}
}
public void mousePressed(MouseEvent e)
{
} public void mouseClicked(MouseEvent e)
{
} public void mouseEntered(MouseEvent e)
{
} public void mouseExited(MouseEvent e)
{
}
}
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Conversation2
{
JFrame Frame = new JFrame("智能对话");
JLabel Label1= new JLabel("M:");
JLabel Label2= new JLabel("W:");
JButton Button = new JButton("Conversation");
JTextField Text1= new JTextField("Hello!",30);
JTextField Text2= new JTextField("Hello!",30);
JPanel panel1= new JPanel();
JPanel panel2= new JPanel();
public Conversation2()
{ //建立中间容器面板panel1,panel1的布局管理器为FlowLayout
panel1.setLayout(new FlowLayout());
panel1.add(Label1);
panel1.add(Text1);
panel1.add(Label2);
panel1.add(Text2);
//建立中间容器面板panel2,panel2的布局管理器为FlowLayout
panel2.setLayout(new FlowLayout());
panel2.add(Button);
//建立顶层容器ContentPane,ContentPane布局管理器为BorderLayout
Frame.getContentPane().setLayout(new BorderLayout());
Frame.getContentPane().add(panel1,BorderLayout.NORTH);
Frame.getContentPane().add(panel2,BorderLayout.CENTER);
Frame.setSize(1024,600);
Frame.setVisible(true);
//button的事件处理
class ButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
FileReader f=new FileReader("d:\\a.txt");
BufferedReader buf= new BufferedReader(f);
String s;
int b=0;
while((s=buf.readLine())!=null)
{
if(s.equals(Text1.getText()))
{break;}
b++; }
if(b==0)
{ Text2.setText("不知道,你来说说!");}
if(b==1)
{ Text1.setText("分3类");
Text1.setText("J2SE,J2ME,J2EE");
Text1.setText("");
}
}catch(Exception ex)
{
System.out.println(e.toString());
}
}
}
Button.addActionListener(new ButtonActionListener()); //Window关闭事件处理
Frame.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[ ] args)
{
Conversation2 conversation= new Conversation2();
}}
在同一个文件夹里建个文本.TXT就行了
我这个很比较简单,是自己做的~~
晕晕晕晕晕晕csdn 成了写作业的机器了
原来搞vc,mfc的,不过看现在java比较火,所以想转型。哈哈,大家多多支持哦。