1、第一章实验一package ch01;import java.text.SimpleDateFormat;import java.util.Date;class Timer extends Thread private SimpleDateFormat sdf = new SimpleDateFormat(“yyyy 年 MM 月 dd 日 HH:mm:ss“);public void run() while (true) System.out.print(“r 现在时间是:“);Date now = new Date();System.out.print(sdf.format(now);tr
2、y sleep(1000); catch (InterruptedException e) e.printStackTrace();public class Clock public static void main(String args) Timer timer = new Timer();timer.start();实验二package ch01;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.Random;import javax.swing.JButton;imp
3、ort javax.swing.JFrame;public class MagicButton extends MouseAdapter JFrame win;JButton button = new JButton(“你点不到我“);Random rand = new Random();void initUI() win = new JFrame();win.setLayout(null);button.setSize(100, 40);button.addMouseListener(this);win.add(button);win.setSize(400, 300);win.setRes
4、izable(false);win.setLocationRelativeTo(null);win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);win.setVisible(true);public static void main(String args) MagicButton demo = new MagicButton();demo.initUI();public void mouseEntered(MouseEvent e) int mouseX = button.getX() + e.getX();int mouseY = butt
5、on.getY() + e.getY();while (true) int buttonX = rand.nextInt(win.getWidth() - button.getWidth();int buttonY = rand.nextInt(win.getHeight() - button.getHeight();button.setLocation(buttonX, buttonY);if (!button.getBounds().contains(mouseX, mouseY) break;第二章实验一/*2. 交换两个变量的值(不允许使用中间变量) 。*/package ch03;p
6、ublic class Exp2_2 public static void main(String args) int a = 2, b = 3;int s = a * b;a = s / a;b = s / a;System.out.println(“a=“ + a + “, b=“ + b);实验二/*3. 逆序输出一个 7 位整数,如 8639427 输出为 7249368(不允许使用循环语句) 。*/package ch03;public class Exp2_3 public static void main(String args) long a = 8639427;System.
7、out.print(a % 10);System.out.print(a / 10 % 10);System.out.print(a / 100 % 10);System.out.print(a / 1000 % 10);System.out.print(a / 10000 % 10);System.out.print(a / 100000 % 10);System.out.print(a / 1000000 % 10);实验三/*4. 对于 int 型变量 a,以最快的速度计算 34a 的值。*/package ch03;public class Exp2_4 public static v
8、oid main(String args) int a = 3;int b = (a b ? a : b) c ? (a b ? a : b) : c;System.out.println(“max=“ + max);第三章实验一/*2. 使用循环结构逆序输出任意位数的整数。*/package ch04;import java.util.Scanner;public class Exp3_2 public static void main(String args) Scanner s = new Scanner(System.in);System.out.println(“输入整数:“);lo
9、ng n = s.nextLong();while (n 0) System.out.print(n % 10);n /= 10;实验二/*3. 输出以下由数字组成的菱形(要求将输出行数存放于变量中以便随时更改) 。1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1 */package ch04;import java.util.Scanner;public class Exp3_3 public static void main(String args) int rows;Scanner s = new Scanner(System.in);S
10、ystem.out.print(“输入行数:“);rows = s.nextInt();for (int i = -rows / 2; i ai) min = ai;return min;int findLastLocation(int a, int x) for (int i = a.length - 1; i = 0; i-) if (ai = x) return i;return -1;boolean isAsc(int a) for (int i = 0; i ai + 1) return false;return true;void reverse(int a) for (int i = 0; i x)