while (true) {
if (Thread.currentThread() == yaru) { // q.add(); if (q.get() < 12) { q.add();
System.out.println(““加入一颗子弹,共有:”” + q.get() + ““颗””);
} else { try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
} else if (Thread.currentThread() == shechu) { if (q.get() > 0) { q.move();
System.out.println(““射出一颗子弹,共有”” + q.get() + ““颗””);
} else { try {
Thread.sleep(500);
} catch (InterruptedException e) {
} } }
附加题:
}
}
}
5、 某企业为了促销,搞抽奖宣传活动,奖品为新款手机一部,抽奖规则如下:
(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,??,n-1。手机随机放在其中一个盒子中。(n为自然数) (2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。(m为自
然数,m (3)直到重新摸到0号盒子为止。 例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。 请编写一个完整的程序,随机输入n,m(m import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class DrawOutAward { /** ? wanglong */ public static void main(String args[]) { new DrawOutAward(); } public DrawOutAward() { // BufferedReader br = new BufferedReader(new // InputStreamReader(System.in)); Scanner scan = new Scanner(System.in); int n = 5; int m = 3; boolean flag = true; while (flag) { System.out.print(““请输入n:””); n = scan.nextInt(); System.out.print(““请输入m:””); m = scan.nextInt(); if (m < n) flag = false; else { System.out.println(““m必须要小于n””); } } Set System.out.print(““摸奖过程:0,””); // 起点 for (int i = 0; (i + m) % n != 0;) { int j = (i + m) % n; System.out.print(j + ““,””); set.add(j); i = j; } System.out.println(0);// 终点 if (n == set.size()) System.out.println(““一定会被抽中””); else { float result = (n - set.size()) / (float) n; System.out.println(““不被抽中的概率为:”” + result); } } } 6、 采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两 部分。客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。(20分)(服务端程序和客户端程序分别命名为Server.java和Client.java) 7、 服务端: package sever; import java.io.*; import java.net.*; public class Server { public static void main(String args[]) { ServerSocket server = null; Socket you = null; String s = null; DataOutputStream out = null; DataInputStream in = null; try { server = new ServerSocket(4332); } catch (IOException e1) { System.out.println(e1); } try { System.out.println(““等待客户呼叫””); you = server.accept(); out = new DataOutputStream(you.getOutputStream()); in = new DataInputStream(you.getInputStream()); s = in.readUTF(); while (true) { System.out.println(““服务器收到:”” + s); out.writeUTF(““你好:我是服务器””); if (!(new File(““d:/”” + s).exists())) { out.writeUTF(““文件不存在””); System.out.println(““文件不存在””); return; } out.writeUTF(““文件存在””); System.out.println(““文件已发送””); FileInputStream fis = new FileInputStream(new File(““d:/02.jpg””)); byte[] buf = new byte[288072]; fis.read(buf); out.write(buf, 0, 288072); Thread.sleep(1000); } } catch (Exception e) { System.out.println(““客户已断开”” + e); } } } 客户端: package sever; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.*; import javax.swing.JFrame; public class showtu extends JFrame { /** * */ private static final long serialVersionUID = 9113050687899503448L; public static void main(String[] args) { try { String s = null; Socket mysocket; DataInputStream in = null; DataOutputStream out = null; mysocket = new Socket(““127.0.0.1””, 4332); in = new DataInputStream(mysocket.getInputStream()); out = new DataOutputStream(mysocket.getOutputStream()); out.writeUTF(““02.jpg””); s = in.readUTF(); System.out.println(““客户收到:”” + s); s = in.readUTF(); System.out.println(““客户收到:”” + s); if (s.equals(““文件不存在””)) { System.out.println(““文件不存在””); } else { byte[] buf = new byte[298072]; in.readFully(buf); FileOutputStream fos2 = new FileOutputStream(““9.jpg””); fos2.write(buf, 0, 298072); } } } catch (IOException ex) { } }