java的代码大全_java代码大全
【實例簡介】
該書適合剛入手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
【實例截圖】
【核心代碼】
總結
以上是生活随笔為你收集整理的java的代码大全_java代码大全的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cpp [Error] referenc
- 下一篇: 快速幂,矩阵乘法,矩阵快速幂