Java编程与实例
例3.1 实例分析变量、常量、字符串和数组的声明、创建及初始化
/变量、常量、字符串和数组的声明、创建及初始化实例分析
public class L3_1{
public static void main(String args[]){ //程序入口
final int CONST_NUM=5; //定义一个常量
byte b=3; //定义原始数据类型的变量
short s=5;
int i=12;
long l=35L;
float f=1.50f;
double d=5.68d;
boolean bool=true;
char ch=’a’; //定义字符型变量
int c=ch; //char型变量值可以赋给int型变量
//无需强制类型转换,香港服务器,实际上是把char型变量的ASCII码值赋给int型变量
String s1=”Hello World!”; //定义一个字符串
int s1_length=s1.length(); //获得s1字符串的长度
String[] str={“Shanghai”,”Beijing”,”Wuhan”,”Guangzhou”,”Shenzhen”};
//在同一行中定义一个字符串数组,美国服务器,并初始化数组元素
//无需说明数组的长度,系统会自动计算并分配数组所需要的内存空间
System.out.println(“CONST_NUM=”+CONST_NUM+”\t”+
“b=”+b+”\t”+”s=”+s+”\t”+
“i=”+i+”\t”+”l=”+l+”\t”+
“f=”+f+”\t”+”d=”+d);
//在显示器上输出各个变量的值
System.out.println(“bool=”+bool);
System.out.println(“ch=”+ch+”\t”+”c=”+c);
//输出char型变量的值,以及它对应的ASCII码
System.out.println(“s1=”+s1+”\t”+”s1_length=”+s1_length);
//输出字符串的内容及其长度
for(int j=0;j<str.length;j++){
System.out.println(“str[“+j+”]=”+str[j]);
}
//使用for循环语句输出字符串数组的元素,length方法能够获得数组的长度
}
}
运行结果:
CONST_NUM=5 b=3 s=5 i=12 l=35 f=1.5 d=5.68
bool=true
ch=a c=97
s1=Hello World! s1_length=12
str[0]=Shanghai
str[1]=Beijing
str[2]=Wuhan
str[3]=Guangzhou
str[4]=Shenzhen
程序分析:
该程序定义了一个名为L3_1的公共类,因此该源程序的文件名必须与公共类的名称相同,即为L3_1.java,而且要注意大小写一定要完全一致。编辑好该源程序后,就可以在DOS环境下编译了,在DOS提示符后输入编译命令javac L3_1.java,如果源程序没有错误并被编译通过,在文件夹中会生成一个编译好的字节码文件L3_1.class,DOS提示符会重新显示出来,这时就可以运行该字节码文件了。输入运行命令java L3_1,这时会在屏幕上显示上面的运行结果。
例3.2 综合实例分析:在表达式中正确使用运算符
//表达式综合实例,在表达式中正确使用运算符
public class L3_2{
public static void main(String args[]){
int i=1;
//一元运算符中的自加和自减运算
System.out.println(“i++=”+(i++)+”\t”+”++i=”+(++i));
System.out.println(“i–=”+(i–)+”\t”+”–i=”+(–i));
int j=5/3; //两个整数相除,结果是整数
double k=5.0/3.0;//两个浮点数相除,结果是浮点数
System.out.println(“5/3=”+j+”\t”+”5.0/3.0=”+k);
int p=5%3; //两个整数相除取余数,余数为整数
double q=5.5%3.2;//两个浮点数相除取余数,余数为浮点数
System.out.println(“5%3=”+p+”\t”+”5.5%3.2=”+q);
String s1=”I am “;
String s2=”a student.”;
String s3=s1+s2; //+运算符可以用在字符串连接运算中
System.out.println(“s3=s1+s2=”+s3);
if(s1 instanceof String){ //判断s1是否是String类的实例
System.out.println(“s1 is an instance of String class”);
}
else{
System.out.println(“si is not an instance of String calss”);
}
//短路运算符的使用
if(!(5<0&&5/0>0)){//5<0为假,&&后面的条件将不再判断,因此也不抛出列外
System.out.println(“&& is a short-circuit operator”);
}
if(5>0||5/0>0){ //5>0为真,||后面的条件将不再判断,因此也不抛出例外
System.out.println(“|| is a short-circuit operator”);
}
boolean bool=true;
int a=bool?5:3; //三元运算符,bool为真,则a=5;否则,a=3
System.out.println(“a=bool?5:3″+”\t”+”a=”+a);
for(int x=0;x<5;x++){
x+=2; //赋值运算符的使用,等价于x=x+2
System.out.print(“x=”+x+”\t”);
}
}
}
运行结果:
i++=1 ++i=3
i–=3 –i=1
5/3=1 5.0/3.0=1.6666666666666667
5%3=2 5.5%3.2=2.3
s3=s1+s2=I am a student.
s1 is an instance of String class
&& is a short-circuit operator
|| is a short-circuit operator
a=bool?5:3 a=5
x=2 x=5
例3.3 判断某一年是否为闰年闰年的条件是符合二者之一:① 能被4整除,但不能被100整除;② 能被4整除,又能被100整除。源程序如下:
//判断某一年是否为闰年
import java.io.*;
public class L3_3{
public static void main(String args[])throws IOException{
//从键盘读入一个整型数据
int year=0;
System.out.print(“please enter an integer number as the year number:”);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String s=in.readLine();
year=Integer.parseInt(s);
//方法一:用逻辑表达式判断闰年的所有成立条件
//使用了一个if-else语句
if((year%4==0&&year%100!=0)||(year%400==0)){
System.out.println(year+” is a leap year.”);
}
else{
System.out.println(year+” is not a leap year.”);
}
//方法二:使用了if-else语句的特殊形式,对多个条件分支进行选择
//leap是一个标志,某一年是闰年,就标志为true,否则为false
boolean leap;
if(year%4!=0)
leap=false;
else if(year%100!=0)
leap=true;
else if(year%400!=0)
leap=false;
else
leap=true;
if(leap==true){
System.out.println(year+” is a leap year.”);
}
else{
System.out.println(year+” is not a leap year.”);
}
//方法三:if-else语句的嵌套使用
//else语句不能单独使用,只能和最近的if语句配套使用
//使用{}对if-else进行匹配
if(year%4==0){
if(year%100==0){
if(year%400==0)
leap=true;
else
leap=false;
}
else
leap=false;
}
else
leap=false;
if(leap==true){
System.out.println(year+” is a leap year.”);
}
else{
System.out.println(year+” is not a leap year.”);
}
}
}
运行结果:
please enter an integer number as the year number:2000
2000 is a leap year.
2000 is a leap year.
2000 is a leap year.
程序分析:
天下没有不散的宴席,也许这人间真的只有朦朦胧胧才是真。