exam01_daan2.htm

来自「清华JAVA教程。不用多说了吧」· HTM 代码 · 共 294 行

HTM
294
字号
<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../css/text.css" type="text/css"></head><body bgcolor="#FFFFFF" text="#000000" style="overflow-x:hidden" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"><!--czp-wenda-daan--><table width="100%" border="0" cellspacing="0" cellpadding="0">  <tr>    <td>      <table width="95%" border="0" cellspacing="0" cellpadding="0" align="left">        <tr>           <td class=text>             <p><b>四、 编程题答案</b></p>            <p>编程第1题<br>                import java.io.*;             <p>  public class SelectSort<br>                {<br>                 public static void main(String args[])<br>                 {<br>                  int a[]={20,10,50,40,30,70,60,80,90,100};<br>                  int temp;<br>              <br>                  for (int i=0; i&lt;a.length-1;i++)<br>                   for (int j=i+1; j&lt;a.length ; j++)<br>                   {<br>                    if (a[i]&lt;a[j]) {<br>                     temp=a[i];<br>                     a[i]=a[j]; <br>                     a[j]=temp;<br>                    } <br>                   }<br>              <br>                  for (int k=0;k&lt;a.length ;k++)<br>                  {<br>                   System.out.println(&quot;a[&quot;+k+&quot;]:&quot;+a[k]);<br>                  }<br>               <br>                 }<br>               <br>                }<br>            </p>            <p><br>              编程第2题<br>                import java.io.*;<br>                import java.net.*;             <p>  public class HelloServer<br>                {<br>                 public static void main(String args[]) throws IOException<br>                 {<br>                  ServerSocket server=null;<br>                  server = new ServerSocket(8888);<br>              <br>                  Socket ClientSocket = null;<br>              <br>                  ClientSocket = server.accept();<br>              <br>                  String line;<br>                  BufferedReader is = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));<br>                  PrintWriter os = new PrintWriter(ClientSocket.getOutputStream());<br>              <br>                  while (true)<br>                  {<br>              <br>                  line = is.readLine();<br>                  if (line.equals(&quot;hello&quot;)) { <br>                    os.println(&quot;hello&quot;);<br>                    os.flush();<br>                   } <br>                  }<br>              <br>                 } <br>              <br>                }<br>            </p>            <p><br>              编程第3题<br>                import java.util.*;<br>                import java.text.*;             <p><br>                class ThreeTimeThread extends Thread<br>                {<br>                 public ThreeTimeThread(String str) <br>                 {<br>                  super(str);<br>                 }<br>              <br>                 public void run() <br>                 {<br>              <br>                  while (true) {<br>              <br>                   SimpleDateFormat formatter = new SimpleDateFormat (&quot;yyyy.MM.dd               G 'at' hh:mm:ss z&quot;);<br>                   Date currentTime = new Date();<br>              <br>                   try {<br>                   sleep(1000);<br>                   }catch (Exception e) {}<br>              <br>                   String dateString = formatter.format(currentTime);<br>                   System.out.println(getName()+&quot;:&quot;+dateString);<br>                   }<br>                  } <br>            </p>            <p>     public static void main(String args[]) throws Exception<br>                  {<br>                   new ThreeTimeThread(&quot;first&quot;).start(); <br>                   new ThreeTimeThread(&quot;second&quot;).start();<br>                   new ThreeTimeThread(&quot;third&quot;).start(); <br>                  }<br>               <br>                }<br>            </p>            <p><br>              编程第4题<br>                Calculator.java:             <p>  import java.awt.*;<br>                import java.awt.event.*;<br>                import java.lang.*;<br>                import java.applet.*;</p>            <p>  public class Calculator extends Applet implements ActionListener{<br>                 Panel buttonPanel;<br>                 TextField tf; <font color="339900">//用于显示输入的数字的文本框</font><br>                 Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bDot,bPlus,bSub,bDiv,bMulti,bEqual;	              <font color="339900">//按键</font><br>                 String onDisplay=&quot;&quot;; <font color="339900">//显示在文本框中的字符串</font><br>                 boolean isDotPressed=false; <font color="339900">//按键'.'是否被按下</font><br>                 float operand; <font color="339900">//通过按键输入的数</font><br>                 float operand1,operand2;<font color="339900">//operand1和operand2为计算用的操作数               </font><br>                 char operator; <font color="339900">//计录所用的操作符</font><br>                 float result; <font color="339900">//计算结果</font><br>                 int times=1; <font color="339900">//小数点后有几位的10次方幂</font><br>               <br>                 public void init(){<br>                  setLayout(new BorderLayout(5,5));<br>                  tf=new TextField(30);<br>                  add(tf,BorderLayout.NORTH);<br>                  buttonPanel=new Panel();<br>                  buttonPanel.setLayout(new GridLayout(4,4,5,5));<br>                  add(buttonPanel,BorderLayout.CENTER);</p>            <p>    <font color="339900">//下面依次把按钮添加上去</font><br>               <br>                  b1=new Button(&quot;1&quot;);<br>                  buttonPanel.add(b1);<br>                  b1.addActionListener(this);<br>                  b1.setActionCommand(&quot;1&quot;);<br>              <br>                  b2=new Button(&quot;2&quot;);<br>                  buttonPanel.add(b2);<br>                  b2.addActionListener(this);<br>                  b2.setActionCommand(&quot;2&quot;);<br>              <br>                  b3=new Button(&quot;3&quot;);<br>                  buttonPanel.add(b3);<br>                  b3.addActionListener(this);<br>                  b3.setActionCommand(&quot;3&quot;);<br>              <br>                  bPlus=new Button(&quot;+&quot;);<br>                  buttonPanel.add(bPlus);<br>                  bPlus.addActionListener(this);<br>                  bPlus.setActionCommand(&quot;+&quot;);<br>              <br>                  b4=new Button(&quot;4&quot;);<br>                  buttonPanel.add(b4);<br>                  b4.addActionListener(this);<br>                  b4.setActionCommand(&quot;4&quot;);<br>              <br>                  b5=new Button(&quot;5&quot;);<br>                  buttonPanel.add(b5);<br>                  b5.addActionListener(this);<br>                  b5.setActionCommand(&quot;5&quot;);</p>            <p>     b6=new Button(&quot;6&quot;);<br>                  buttonPanel.add(b6);<br>                  b6.addActionListener(this);<br>                  b6.setActionCommand(&quot;6&quot;);<br>              <br>                  bSub=new Button(&quot;-&quot;);<br>                  buttonPanel.add(bSub);<br>                  bSub.addActionListener(this);<br>                  bSub.setActionCommand(&quot;-&quot;);<br>              <br>                  b7=new Button(&quot;7&quot;);<br>                  buttonPanel.add(b7);<br>                  b7.addActionListener(this);<br>                  b7.setActionCommand(&quot;7&quot;);<br>              <br>                  b8=new Button(&quot;8&quot;);<br>                  buttonPanel.add(b8);<br>                  b8.addActionListener(this);<br>                  b8.setActionCommand(&quot;8&quot;);<br>              <br>                  b9=new Button(&quot;9&quot;);<br>                  buttonPanel.add(b9);<br>                  b9.addActionListener(this);<br>                  b9.setActionCommand(&quot;9&quot;);<br>                <br>                  bMulti=new Button(&quot;*&quot;);<br>                  buttonPanel.add(bMulti);<br>                  bMulti.addActionListener(this);<br>                  bMulti.setActionCommand(&quot;*&quot;);<br>              <br>                  b0=new Button(&quot;0&quot;);<br>                  buttonPanel.add(b0);<br>                  b0.addActionListener(this);<br>                  b0.setActionCommand(&quot;0&quot;);<br>              <br>                  bDot=new Button(&quot;.&quot;);<br>                  buttonPanel.add(bDot);<br>                  bDot.addActionListener(this);<br>                  bDot.setActionCommand(&quot;.&quot;);<br>              <br>                  bEqual=new Button(&quot;=&quot;);<br>                  buttonPanel.add(bEqual);<br>                  bEqual.addActionListener(this);<br>                  bEqual.setActionCommand(&quot;=&quot;);<br>              <br>                  bDiv=new Button(&quot;/&quot;);<br>                  buttonPanel.add(bDiv);<br>                  bDiv.addActionListener(this);<br>                  bDiv.setActionCommand(&quot;/&quot;);<br>              <br>                  }<br>              <br>              <br>                 public void actionPerformed(ActionEvent e){<br>                  String str=e.getActionCommand();<br>                  char b=str.charAt(0);<br>                  switch(b){<br>                    case '0':<br>                    case '1':<br>                    case '2':<br>                    case '3':<br>                    case '4':<br>                    case '5':<br>                    case '6':<br>                    case '7':<br>                    case '8':<br>                    case '9': onDisplay+=b;<br>                     operand=operand*10+Integer.parseInt(str);<br>                     if(isDotPressed) times*=10;<br>                     tf.setText(onDisplay);<br>                     break; <br>                    case '.': onDisplay+=b;<br>                     isDotPressed=true;<br>                     tf.setText(onDisplay);<br>                     break; <br>                    case '+': <br>                    case '-':<br>                    case '*':<br>                    case '/': operator=b;<br>                     operand1=operand/times;<br>                     System.out.println(operand1);<br>                     onDisplay=&quot;&quot;;<br>                     times=1;<br>                     isDotPressed=false;<br>                     operand=0;<br>                     break;<br>                    case '=': operand2=operand/times;<br>                     System.out.println(operand2);<br>                     switch(operator){<br>                      case '+': result=operand1+operand2;break;<br>                      case '-': result=operand1-operand2;break;<br>                      case '*': result=operand1*operand2;break;<br>                      case '/': result=operand1/operand2;break;<br>                     }<br>                     tf.setText(float.toString(result));<br>                     onDisplay=&quot;&quot;;<br>                     times=1;<br>                     isDotPressed=false;<br>                     operand=0;<br>                     break;<br>                    }<br>                  }<br>                }</p>            <p><br>                Calculator.html:</p>            <p>  &lt;html&gt;<br>                &lt;applet code=&quot;Calculator.class&quot; width=250 height=250&gt;<br>                &lt;/applet&gt;   <br>                &lt;/html&gt;</p>      </table>    </td>  </tr></table>  <p></p> </body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?