1.
Hello
程序:
public
class Hello
{
public static void main(String[] args)
{
System.out.println ("hello
world!");
} }
2.
基本程序:
1.
Do循环
public
class Dox{
public static void main(String args[]) {int i=0,sum=0;
do {sum=sum+i;i++;}
while(i<101);
System.out.println("1+2+3+…+100="+sum); }
}
2.
For
循环
public
class Forx{
public static void main(String args[]) {int sum=0;
for(int
i=1;i<101;i++){sum=sum+i;
}
System.out.println("1+2+3+…+100="+sum);
} }
3.
If
语句
public
class Ifx{
public static void main(String args[]) {
int
x=3;
if(x!=0)
System.out.println("Hello
World!"); }
}
4.
While
语句
public
class Whilex{
public static void main(String args[]) {
int
i=0;
while(i<3){
System.out.println("Have you finished?");
i++;
}
System.out.println("Finished!");} }
5.
Switch
语句
import
java.util.Scanner;
public
class Switchx
public static void main(String[] args) {
int
k;
System.out.println("输入k值:");
java.util.Scanner aa=new java.util.Scanner(System.in);
k=aa.nextInt();
String str = k +"的汉字形式是:
";
switch(k){
case
1:
str +="一";
break;
case
2:
str
+="二";
break;
case
3:
str
+="三";
break;
default:
System.out.println("什么也没有定义。");
break;
}
System.out.println(str); }
}
斐波拉契数列:
public
class Fib
{
public long fid(int n){
if((n==1)||(n==2)){
return 1;
}
else{ return fid(n-1)+fid(n-2); //递归调用}
}
public void prinSum(int n){
long sum=0L;
for(int i=1;i<=n;i++){
sum+=fid(i);
//求和
System.out.println(fid(i)); }
System.out.println(sum);
}
public static void main(String[] args) {
new Fib().prinSum(40);
//实例化匿名对象并调用求和方法
} }
This的用法:
package
fx;
class
Pixel {
int x=5;
int y=6;
void init (int x, int y)
{ this.x =
x;
this.y = y;
System.out.println(this.x);
System.out.println(this.y);
}
public static void main (String args[])
{ Pixel p = new Pixel();
System.out.println(p.x);
System.out.println(p.y);
p.init (4,3);
} }
运行结果:5 6 4 3
有参数:
package
fx;
class
CanShuStrMeth {
int
month; int
day; int year;
CanShuStrMeth (int
m,int d,int y) {
month=m;
day=d; year=y;
System.out.println("日期是" + m + "/" + d + "/" + y +
".");
}
public static void main(String args[])
{
CanShuStrMeth S1,S2;
S1=new CanShuStrMeth(11,27,1969);
S2=new
CanShuStrMeth(3,3,1973);
}}
运行结果:日期是11/27/1969.
日期是3/3/1973
无参数(隐式构造):
package
fx;
class
WuCanStrMet {
int month=11;
int day=27;
int
year=1969;
public static void main(String args[ ]){
WuCanStrMet S1;
S1=new WuCanStrMet ();
System.out.println("日期是" + S1.day+"-" +S1.month+"-" +S1.day );
} }
Extend
实现
继承:
package fx;
class
Father{
//父类
private
int money;
float
weight,height;
String
head;
String
speak(String s) {
return s
;}}
class Son
extends
Father{
//子类
String
hand ,foot;}
public
class TestExtend
{
public static void main(String args[]){
Son boy=new Son();
boy.weight=120f; boy.height=1.8f;
boy.head="一个头"; boy.hand="两只手";
boy.foot="两只脚";
System.out.println("我是儿子");
System.out.println("我有:"+boy.hand+"、"+boy.foot+"、"+ boy.head
+"、重"+boy.weight+"、高"+boy.height);}}
运行结果:我是儿子,我有两只手,两只脚,一个头,重120.0
高1.8
Super用法:
class
FictionAuthor extends Author {
String
storytype;
FictionAuthor(String name, String type) {
super(name);
storytype = type;
System.out.println(“从
Fiction
Author
类输出");
System.out.println(“小说类型为" +
storytype);
}}
Author(String str) {
name
= str;
System.out.println(“从
Author
类输出");
System.out.println(“姓名为
" +
name);
}
接口的定义及执行:
package
fx;
interface
I1 { abstract void test(int i);
}
interface
I2 { abstract void test(String
s); }
public
class MultInterfaces implements I1, I2 {
public void test(int i) {
System.out.println("In MultInterfaces.I1.test");
}
public void test(String s)
{
System.out.println("In MultInterfaces.I2.test");
}
public static void main(String[] a)
{
MultInterfaces t = new
MultInterfaces();
t.test(42);t.test("Hello");
}}
TRY-CATCH:
package
fx;
import
java.lang.Math;
public
class ExceptionDemo {
public static void main(String args[]){
try { int c= calculate(9,0);
System.out.println(c);
}
catch (Exception e) {
System.err.println("发生异常:
" + e.toString());
e.printStackTrace();
}
}
static int calculate(int a,int b) {
int c = a/b; return
c;
} }
Throw:
try
{
if(flag<0) {
throw new
NullPointerException();
} }
异常:
1. public class Exception2{
public static void main (String args[])
{
String langs [] = {"Java","Visaul Basic","C++"};
int i =
0; while (i
< 4) {
System.out.println (langs[i]);
i++;
} }}
运行出错,字符串下标越界。将“while (i
< 4)”改为“while (i <3)”即可。
2. class MyoneException extends Exception {
}
public
class TryInbed {
public static void main(String[] args)
{
System.out.println("Entering first try block");
try
{
System.out.println("Entering second try block");
try
{
throw new MyoneException();
} finally {
System.out.println("finally in 2nd try
block");
}
}
catch (MyoneException e) {
System.err.println("Caught MyoneException in 1st try
block");
} finally
{
System.err.println("finally in 1st try
block");
} }}
运行错误,类重复定义。
将“class MyoneException
extends Exception { } public
class TryInbed”改为“class TryInbed extends
Exception”
编制applet程序,绘制一个长120、宽80的红色矩形并在矩形内部绘制一个兰色内切椭圆。
程序代码如下:
import
java.awt.*;
import
java.applet.Applet;
public
class Rectangle extends Applet {
public void paint(Graphics g) {
g.setColor(Color.red); //设置画笔颜色为红色
g.drawLine(20,20,20,100);
g.drawLine(20,20,140,20);
g.drawLine(140,20,140,100);
g.drawLine(140,100,20,100);
g.setColor(Color.blue); //设置颜色为兰色
g.drawOval(20,20,120,80); }
}
创建一个applet程序,绘制两个同心圆,园心显示“同心”两个汉字。
源代码:
import
java.awt.*;
import
java.applet.Applet;
public
class Txyuan extends Applet {
public void paint(Graphics g) {
g.setColor(Color.pink); //设置画笔颜色为粉红色
g.drawOval(20,20,80,80);
g.drawOval(10,10,100,100);
g.drawString("同心",60,60);
}
}
六、思考题
1)以下哪个是Java应用程序main方法的有效定义?
( C
)
A. public
static void main();
B. public
static void main( String args );
C. public
static void main( String args[] );
D. public
static void main( Graphics g );
E. public
static boolean main( String a[] );
2)
编译和运行以下代码的结果为:
( D
)
public
class MyMain{
public
static void main(String argv){
System.out.println("Hello cruel world")}}
A.编译错误;
B.运行输出
"Hello cruel
world";
C.编译无错,但运行时指示没有定义构造方法。
D.编译无错,但运行时指示没有正确定义main方法。
3)
下列选项中不属于Java虚拟机的执行特点的一项是:
( D
)
A.异常处理
B.多线程
C.动态链接
D.简单易学
4)
不属于Java语言特点的一项是:
( C
)
A.分布式 B.
安全性 C.
编译执行
D.面向对象
5)
以下程序的运行结果为:
( B
)
public class
Test{
public
static void main(String argv[ ]){
System.out.println("x="+5);}}
A.
5 B.
x=5 C.
"x="+5 D.
"x="5
6)
以下程序的运行结果为:
( A
)
public class
Test{
public
static void main(String argv[ ]){
System.out.println("good"+"morning");} }
A.
goodmorning
B.
"good"+"morning"
C. good
morning D.
good+morning
第一部分代码:
package
Experiment3;
import
java.util.*;
public
class dy {
public
dy(){
List dy=new ArrayList();
dy.add(1);
dy.add(10);
dy.add(5);
dy.add(7);
dy.add(6);
dy.add(2);
dy.add(9);
dy.add(20);
dy.add(14);
dy.remove(0);
dy.add(3,35);
System.out.println("排序前序列:");
for(int i=0;i<=dy.size()-1;i++){
System.out.print(dy.get(i)+"
");
}
System.out.println(" ");
System.out.println("排序后序列:");
Collections.sort(dy);
//
Collections.reverse(list);
Iterator it=dy.iterator();
while(it.hasNext()){
System.out.print(it.next()+"
");
}
List list1=new LinkedList();
list1.add("ddy");
ListIterator
it1=list1.listIterator();
while(it1.hasNext()){
Object object=it1.next();
it1.set(((String
)object));
}
while(it1.hasPrevious()){
System.out.println(it1.previous());
} }
public static void main(String[] args) {
new dy();
}}
第二部分代码:
package
experiment3;
import
java.util.*;
public
class Px{
public static void main(String []
args){
HashMap<Integer,Integer> hm = new
HashMap<Integer,Integer>();
hm.put(1,2);
hm.put(3,4);
hm.put(2,2);
hm.put(6,4);
hm.put(5,2);
hm.put(4,4);
Set<Integer> set =
hm.keySet();//取出所有键
TreeSet<Integer> ts =new
TreeSet<Integer>();
System.out.println("键的原序:");
for(Integer s:
set){
System.out.print(s+" ");
ts.add(s);
}
System.out.println(" ");
System.out.println("按键排序后的顺序:");
for(Integer s:ts){
System.out.print(s+"
"); }
System.out.println(" ");
Collection<Integer> cl=
hm.values();//取出所有值
TreeSet<Integer> ts1 =new
TreeSet<Integer>();
System.out.println("值的原序:");
for(Integer s:
cl){
System.out.print(s+" ");
ts1.add(s); }
System.out.println(" ");
System.out.println("按值排序后的顺序:");
for(Integer
s:ts1){ System.out.print(s+"
"); } }}
正则表达式
元字符
|
描述
|
.点
|
匹配任何单个字符。例如正则表达式r.t匹配这些字符串:rat、rut、r t,但是不匹配root。
|
$
|
匹配行结束符。例如正则表达式weasel$
能够匹配字符串"He's a
weasel"的末尾
但是不能匹配字符串"They are a
bunch of weasels."
|
^
|
匹配一行的开始。例如正则表达式^When
in能够匹配字符串"When in the course of human
events"的开始,但是不能匹配"What and When in the"
|
*
|
匹配0或多个正好在它之前的那个字符。例如正则表达式
.*
意味着能够匹配任意数量的任何字符。比如<T>.*</T>
可以匹配<T>不管是什么</T>
|
\
|
这是引用符,用来将这里列出的这些元字符当作普通的字符来进行匹配。例如正则表达式\$被用来匹配美元符号,而不是行尾,类似的,正则表达式\.用来匹配点字符,而不是任何字符的通配符。
|
[ ]
[c1-c2]
[^c1-c2]
|
匹配括号中的任何一个字符。例如正则表达式r[aou]t匹配rat、rot和rut,但是不匹配ret。可以在括号中使用连字符-来指定字符的区间,例如正则表达式[0-9]可以匹配任何数字字符;还可以制定多个区间,例如正则表达式[A-Za-z]可以匹配任何大小写字母。另一个重要的用法是“排除”,要想匹配除了指定区间之外的字符——也就是所谓的补集——在左边的括号和第一个字符之间使用^字符,例如正则表达式[^269A-Z]
将匹配除了2、6、9和所有大写字母之外的任何字符。
|
\< \>
|
匹配词(word)的开始(\<)和结束(\>)。例如正则表达式\<the\>能够匹配字符串"for the wise"中的"the",但是不能匹配字符串"otherwise"中的"the"。注意:这个元字符不是所有的软件都支持的。
|
\(
\)
|
将 \(
和 \)
之间的表达式定义为“组”(group),并且将匹配这个表达式的字符保存到一个临时区域(一个正则表达式中最多可以保存9个),它们可以用
\1
到\9
的符号来引用。
|
|
|
将两个匹配条件进行逻辑“或”(Or)运算。例如正则表达式(him|her)
匹配"it belongs to
him"和"it belongs to her",但是不能匹配"it belongs to them."。注意:这个元字符不是所有的软件都支持的。
|
+
|
匹配1或多个正好在它之前的那个字符。例如正则表达式9+匹配9、99、999等。注意:这个元字符不是所有的软件都支持的。
|
?
|
匹配0或1个正好在它之前的那个字符。注意:这个元字符不是所有的软件都支持的。
|
{i}
{i,j}
|
匹配指定数目的字符,这些字符是在它之前的表达式定义的。例如正则表达式A[0-9]{3}
能够匹配字符"A"后面跟着正好3个数字字符的串,例如A123、A348等,但是不匹配A1234。而正则表达式[0-9]{4,6}
匹配连续的任意4个、5个或者6个数字字符。注意:这个元字符不是所有的软件都支持的。
|
加载中,请稍候......