µÚÒ»Õ ʵÑéÒ»
package ch01;
import java.text.SimpleDateFormat; import java.util.Date;
class Timer extends Thread {
private SimpleDateFormat sdf = new SimpleDateFormat(\ÄêMMÔÂddÈÕ HH:mm:ss\
public void run() { while (true) {
System.out.print(\ÏÖÔÚʱ¼äÊÇ£º\ Date now = new Date();
System.out.print(sdf.format(now)); try {
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; import 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.setResizable(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 = button.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;
public 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(\ } }
ʵÑé¶þ
/*********************************
3. ÄæÐòÊä³öÒ»¸ö7λÕûÊý£¬Èç8639427Êä³öΪ7249368£¨²»ÔÊÐíʹÓÃÑ»·Óï¾ä£©¡£ **********************************/ package ch03;
public class Exp2_3 { public static void main(String[] args) { long a = 8639427; System.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£¬ÒÔ×î¿ìµÄËٶȼÆËã34¡ÁaµÄÖµ¡£ **********************************/ package ch03;
public class Exp2_4 { public static void main(String[] args) { int a = 3; int b = (a << 5) + (a << 1);
System.out.println(a + \ } }
ʵÑéËÄ
/*********************************
5. ×Ö·ûÐͱäÁ¿chÖдæ·Å×ÅÒ»¸ö´óСдδ֪µÄÓ¢ÎÄ×Öĸ£¬ÅÐ¶ÏÆä´óСдºó£¬½«chµÄֵתΪСд»ò´óд×Öĸ£¨²»ÔÊÐíʹÓüӼõÔËËã·ûºÍifÓï¾ä£©¡£ **********************************/ package ch03;
public class Exp2_5 { public static void main(String[] args) { char ch = 'E'; ch = (char) ((ch & 32) == 0 ? ch | 32 : ch & (Integer.MAX_VALUE - 32)); System.out.println(\ } }
ʵÑé5
/*********************************
6. ʹÓÃǶÌ×µÄÌõ¼þÔËËã·û£¬Çóa¡¢b¡¢cÖеÄ×î´óÕß¡£ **********************************/ package ch03;
public class Exp2_6 { public static void main(String[] args) { int a = 2, b = 4, c = 3; int max = (a > b ? a : b) > c ? (a > b ? a : b) : c; System.out.println(\ } }
µÚÈýÕ ʵÑéÒ»
/*********************************
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(\ÊäÈëÕûÊý£º\ long 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); System.out.print(\ÊäÈëÐÐÊý:\ rows = s.nextInt(); for (int i = -rows / 2; i <= rows / 2; i++) {