Java例题-源代码.doc
文本预览下载声明
【例1.1】编写一个应用程序,在屏幕上显示字符串“Hello, World!”。
/* HelloWorld.java */
public class HelloWorld {
public static void main(String args[]) {
System.out.println(Hello, World!);
}
}
============================
【例2.1】本例结合基本数据类型,说明如何使用变量。
public class SimpleTypes{
public static void main(String args[]){
byte b = 0x55;
short s = 0x55ff;
int i = 1000000;
long l = 0xfffL;
char c = c;
float f = 0.23F;
double d = 0.7E-3;
boolean bool = true;
System.out.println(b= + b);
System.out.println(s= + s);
System.out.println(i= + i);
System.out.println(l= + l);
System.out.println(c= + c);
System.out.println(f= + f);
System.out.println(d= + d);
System.out.println(bool= + bool);
}
}
【例2.2】算术运算符的使用示例。
public class ArithmaticOp{
public static void main(String args[]){
int a = 5+4; //a=9
int b = a*2; //b=18
int c = b/4; //c=4
int d = b-c; //d=14
int e = -d; //e=-14
int f = e % 4; //f=-2
double g = 18.4;
double h = g % 4; //h=2.4
int i = 3;
int j = i++; //i=4,j=3
int k = ++i; //i=5,k=5
System.out.println(a= + a);
System.out.println(b= + b);
System.out.println(c= + c);
System.out.println(d= + d);
System.out.println(e= + e);
System.out.println(f= + f);
System.out.println(g= + g);
System.out.println(h= + h);
System.out.println(i= + i);
System.out.println(j= + j);
System.out.println(k= + k);
}
}
【例2.3】递增运算符和递减运算符的使用示例。
public class AutoInc {
public static void main(String[] args) {
int i = 1;
System.out.println (i : + i);
System.out.println (++i : + ++i); // Pre-increment
System.out.println (i++ : + i++); // Post-increment
System.out.println (i : + i);
System.out.println (--i : + --i); // Pre-decrement
System.out.println (i-- : + i--); // Post-decrement
System.out.println (i : + i);
}
}
【例2.4】关系运算符的使用示例。
public class RelationalOp{
public static void main(String args[]){
float a =10.0f;
double b = 10.0;
if(a == b){
System.out.println(a 和 b 相等);
}else{
System.out.println(a 和 b 不相等);
}
}
显示全部