日期:2014-05-20 浏览次数:20761 次
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class guiTest implements ActionListener{ private JFrame frame; private JButton[] buttons; private boolean flag; public guiTest(){ flag=true; frame=new JFrame(); frame.setLayout(new GridLayout(2,3)); buttons=new JButton[6]; for(int i=0;i<6;i++){ buttons[i]=new JButton(i+"");buttons[i].addActionListener(this); frame.add(buttons[i]); } frame.setSize(400,300); frame.setLocation(200,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new guiTest(); } @Override public void actionPerformed(ActionEvent e) { if(flag){ frame.repaint(); frame.setLayout(new GridLayout(3,2)); for(int i=0;i<6;i++){ frame.add(buttons[i]); } frame.setVisible(true); flag=false; }else{ frame.repaint(); frame.setLayout(new GridLayout(2,3)); for(int i=0;i<6;i++){ frame.add(buttons[i]); } frame.setVisible(true); flag=true; } } }
------解决方案--------------------
import java.util.*;
/*5、 某企业为了促销,搞抽奖宣传活动,奖品为新款手机一部,抽奖规则如下:
(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,……,n-1。手机随机放在其中一个盒子中。(n为自然数)
(2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。(m为自然数,m <n)
(3)直到重新摸到0号盒子为止。
例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。
请编写一个完整的程序,随机输入n,m(m <n),程序分析手机有没有不被抽中的机会?如果有,概率是多少? (概率=不被抽中的可能数/n)(30分)*/
public class Test3{
public static void main(String[] args) {
System.out.println("不被摸中的概率是:"+guess(5,3));
}
public static double guess(int n,int m){
boolean[] isHit=new boolean[n];
int count=0;//不被摸中的盒子数。
int currentBox=0;
int times=0;
System.out.println("被摸到的盒子编号为");
do{
System.out.println(currentBox);
isHit[currentBox]=true;
times++;
currentBox=(m*times)%n;
}while(currentBox!=0);
for(boolean b:isHit){
if(!b){
count++;
}
}
return (double)count/n;
}
}
------解决方案--------------------
import java.net.*; import java.io.*; public class SampleServer extends Thread { static BufferedReader in; static PrintWriter out; public static void main(String[] arges) { try { int port = 5678; //使用5678端口创建一个ServerSocket ServerSocket mySocket = new ServerSocket(port); //等待监听是否有客户端连接 Socket sk = mySocket.accept(); //接受从客户端过来的数据流 BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream ())); //发送服务器端给客户端的数据流 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream ())),true); // BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); while(true) { System.out.println ("客户端信息:"+in.readLine()); out.println ("receive:I have accept your information"); } } catch(Exception e) { System.out.println("客户端已经退出!"); } } } import java.io.*; import java.net.*; public class SampleClient { public static void main(String[] arges) { try { //获取一个IP。null表示本机 InetAddress addr = InetAddress.getByName(null); //打开5678端口,与服务器建立连接 Socket sk = new Socket ("172.20.9.79", 5678); //缓存输入 BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream ())); //缓存输出 PrintWriter out = new PrintWriter (new BufferedWriter(new OutputStreamWriter(sk.getOutputStream ())),true); //向服务器发送信息 BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); String str = ""; while(true) { System.out.print("客户端:"); str = buf.readLine(); if(str.equals("end"))break; out.println (str); //接收服务器信息 System.out.println ("服务器端信息:"+in.readLine ()); } } catch(Exception e) { System.out.println(e); } } }