该书适合刚入手java的童鞋,该书由浅入深,是一个不错的自学用书。
导论
(资料图片)
Java程序设计
导论
1.1 Hello, World!
程序清单1: Hello world.iava
1/
2*@(#)HelloWorld. java 0.30 08/28/10
4* Show message” Hello, World!” in Console
6 @author xiaohua xu
*@ version②.30
8 *k @ since 0.20
@see System. in
11
12
13 public class HelloWorld t
public static void main(String args)t
6
System. out. println( "Hello, World!
18}
程序清单2: Hello WorldDialog java
1
2 *@(#)HelloWorldDialog java 0.20 08/28/10
4* Show message Hello, World!" in JOptionPane
6 @author xiaohua xu
@version 0.30
8 @since 0.20
9 @see javax. swing JOptionPane
OT
12 import javax. swing JOptionPane
13
14 public class Hel loWor ldDialog i
5678
String message =>Hello, World!
public static void main (Stringl arg
JOptionPane showMessageDialog(nuLl, message);
1
20
程序清单3: Hello WorldApp.java
/
半米
2*@#)He L ldApp. java 0.30 08/28/10
4* Show message Hello, World! in Console
DONT DⅠSTRⅠ BUTE IT!
April 15,2012
2基础知识
Java程序设计
6 *k @author Xiaohua xu
7 @version 0.30
8 *k @since 0.20
@see System. in
10
12 public class he l lowor ldapp i
13
public void maino i
System, out. print("Hello, World! )
System, out. println(;
11111
56789
public static void main (String] args)t
Hel ldApp app= new He l loWor ldApp;
20
app. main(
21
2
12 Unicode演示
匚程序清单4 UnicodeDemo
1/*米
2* @(#Uni codeDemo, java 0. 20 08/28/10
4* Show Uni code u2122 as Java
TM
6 @author Xiaohua Xu
@version 0.20
8 @since 0.10
9 @see JoptionPane
1O冰
12 pubLic class Uni code Demo I
3
public static final char TRADE_MARK \u2122
15 public static void show (String str)i
javax. swing.JOptionPane showMessageDialog(nuLl, str);
1112
public static void main (String args)t
String message=”Java”+ TRADE MARK;
System, out. println(message);
show (message)
System. exit(o);
26
2基础知识
21加法
DONT DⅠSTRⅠ BUTE IT!
April 15,2012
2基础知识
Java程序设计
程序清单 Addition.java
1/**
2 * @(*)Addition. java
4 *@author Xiaohua Xu
5*@ version0.92009/03/05
8 import javax. swing JOptionPane
10 public class Addition i
public static void main( Stringl] args)t
111
String firstNumber;
String secondNumber
16
int number 1
int number 2
18
int sum,
19
firstNumber JOptionPane showInputDialog("First Num)
secondNumber OPtion Pane. showInputDialog ("Second Num
number1 Integer parseInt(firstNumber )
2345
number 2 Integer parseInt(secondNumber);
sum number 1+ number 2
J0ption Pane showMessageDialogo
6789o
The Sum is:”+sum
Results”
JOptionPane PLAIN_MESSAGE
32
System. exit( 0
22浮点数注意
程序清单6: DoubleDemo. java
/*
2 *@(#)DoubL eDemo java
4* @author Xiaohua Xu
5*@ verslon1.02009/03/05
8 public class Doub leDemo t
public static void main(String args)t
double x=0.0:
//初始量
12
double delta =0.1
增长量
int n=10
//迭代次数
14
5
System. out. println(x)
//输出到屏幕
DONT DⅠSTRⅠ BUTE IT!
April 15,2012
2基础知识
Java程序设计
for(int讠=0;i
1
678
x t= delta
/每次递增 delta
System. out. println(x);
//屏幕输出
19
0
21
23数值最值
程序清单z: MaxMin ValueOfVariableDemo java
1/*米
2 *@(#)MaxMinValueOfVariableDemo java
3*输出冬和变量的最大值和最小值
@author Xiaohua Xu
6*@ verslon1.0010/03/15
9 public class MaxMinValueOfVariableDemo t
10
public static void main(Stringl args) t
11
System. out. printInG The largest byte value is
+ Byte. MAX_ VALUE+”,”);
13
System. out. printLn( The smallest byte value is
+ Byte. MIN_VALUE+”.”);
15
System, out. println( The largest short value is
1
+ Short. maX_ VALUE+”.”);
8
System, out println("The smallest short value is
+ Short. MIN VALUE+”.”);
20
System, out. println( The largest integer value is
2
+ Integer. MAX VALUE+”.”);
System, out. println(" The smallest integer value is
+工 nteger MIN_ VALUE+”.”);
System. out. println( The largest long value is
Long MAX VALUE
System. out. println(" The largest long value is
2
Long. MIN_VALUE+”.);
30
31
ystem, out. printLn The largest float value is
+ FLoat. MAX VALUE+”.”);
33
System, out. printLn( The smallest float value is
34
+ Float. MIN value+”.”);
35
36
System, out. println(The largest double value is
+ Double. mAX_VALU+”.”);
System, out. println( The smallest double value is
+ Double. MIN VALUE+”.”)
41
42
循环
DONT DⅠSTRⅠ BUTE IT!
April 15,2012
2基础知识
Java程序设计
程序清单8: WhileDo. java
1/**
2 *@(#)WhileDo. java
4* Since b"" is already false when it reaches the
5* test, the loop never executes
@author Xiaohua xu
8*@ verslon.92009/03/05
11 public class Whiledo i
12
public static void main(String args)t
boolean b= false:
1111
4567
while( b)i
System. out. println( Execute WhileDo while b is true);
tt toc
1
程序清单g: WhileDoTest. java
/
@(#WhiLe DoTest java
4*@author Xiaohua Xu
5*@ verslon0.92009/03/05
8 import javax. Swing JOptionPane
10 public class Whiledotest t
11
11
2
public static void main(String[] args)t
int b= 2
int s=
1
while( b>s)t
18
JOptionPane. showMessageDialogo
1
null
0
Big is still bigger”
21
Results
2
JOption Pane. INFORMATION_ MESSAGE
String big =JOptionPane. showInputDialog("Big Number>)
26
String small= JOptionPane show InputDialog( SmaLl Number
b= Integer parseInt( big )
Integer parseInt( smaLl)
31
ystem. exit(0);
DONT DⅠSTRⅠ BUTE IT!
April 15,2012
2基础知识
Java程序设计
32
33
程序清单10: Do While. java
2 * @()DoWhileTest java
4*@author Xiaohua Xu
5*@ version0.92009/03/05
7 import javax. swing JOptionPane;
9 public class DoWhileTest t
public static void main ([ args)t
int b= 2;// preload variables
int s= 1;// preload variables
1111111
4
do t
56789
JOptionPane. showMessageDialogo
null
Big is still Bigger”
Results”,
JOption Pane INFORMATION_MESSAGE
222
012345
String big JOptionPane showInputDialog("Big Number")
String smaLl= JOptionPane. showInputDialog("SmaLl Number
26
b= Integer parseInt(big)
2
s=Integer parseInt(smaLL)
789o
f while(b> s;
System, exit( 0)
31
32
25选择
程序清单11: Selection Test java
米*/I
2* @()SelectionTest java
4* @author Xiaohua Xu
5*@ version9.92009/03/05
6*/
8 import javax. swing JOptionPane;
10 public class SelectionTest t
public static void main(String[ args) t
DONT DⅠSTRⅠ BUTE IT!
April 15,2012
2基础知识
Java程序设计
14
String big JOptionPane showInputDialog("Big Number>)
15
String small= J0ptionPane showInputDialog( SmaLL Number)
int b= Integer parseInt(big);
比t
Integer parseInt(smaLl);
1
1
String out =(b>s? Big was Larger: Small was larger
20
JOptionPane showMessageDialog(nuLl, out
Results", OPtion Pane. INFORMATION_MESSAGE
System. exit(0);
26
2.6增量
程序清单12: PrePostIncrement. java
/*米
2 *@(#PrePostIncrement java
4* @author Xiaohua Xu
5*@ verslon0.92009/03/05
8 public class PrepostIncrement
public static void main (String args)
11
int x= a
12
System.out. println(” Baseline,x=”+x);
System, out. println( >Pre-increment =++x=>+++X )
1
System, out. printLn( "After increment, x="+X);
X=0;
System,out. println(” 2nd baseline,x=”+×);
System. out. println( "Post-increment = X++=3+X++)
1
System, out. println( After increment,x
X
//System. exit( 0);
27温度转换
程序清单13: FahrenheitToCelsius.java
2 *@(#)FahrenheitToCelsius java
4* celsius =9(fahrenheit- 32)
@author xiaohua Xu
7*@ version0.92009/03/05
DONT DⅠSTRⅠ BUTE IT!
April 15,2012
3简单程序
Java程序设计
10 public class FahrenheitTocelsius i
public static void main(String args)t
double fahrenheit 100;
111111
double celsius =(5.0/9.0*(fahrenheit -32)
4567890
System. out. println(” Fahrenheit”+ fahrenheit+”is
+ celsius+” in Celsius.”);
3简单程序
31数学工具
程序清单14: AbsDemo.java
1/*米
2 *@(#) AbsDemojava
木
4 @author Xiaohua Xu
5*@ version1.002009/03/05
9 public class absDemo t
12
public static void main(Stringl args)t
double x =-0.0.
1
System.out, println(”abs(”++”)=”+Math.abs(x));
6
20
System. out. println(”abs(”+×+”)=”+Math.abs(x);
Double. POSITIVE INFINITY
ystem.out. println(”abs(”+x+”)=”+Math.abs(x);
x= Double NeGATive Infinity
System. out. println(”abs(”+Ⅹ+”)
Math. abs(x))
x= Double nan
System. out. println(”abs(”+x+”)=”+Math.abs(x));
10.5
30
double y
20.1
31
System.out. println(”max(”+x+
y+”)
Math. max
32
System.out. println(”mix(”+X+”,”+y+”)=”+Math.min
33
ystem.out. println(”max”+x+
十
nax(x, y
DONT DⅠSTRⅠ BUTE IT!
10
April 15, 2012
【实例截图】
【核心代码】