局部变量和全局变量代码:- 局部变量的代码:
- public class Areal
- {
- int a1=7;
- int s1=a1*a1;
- public static void main(String args[])
- {
- //长方形面积
- double a2=7.1,b2=4.3;
- double s2=a2*b2;
- //三角形面积
- double a3=5.2, b3=6.7;
- double s3=a3*b3/2;
- System.out.println("三角形的面积为"+s3);
- }
- }
- 全局变量的代码:
- public class var
- {
- byte x;
- short y;
- int z;
- int z1;
- long a;
- float b;
- double c;
- char d;
- boolean e;
- public static void main(String args[])
- {
- int z1=111;
- System.out.println("打印数据z="+z1);
- var m=new var();
- System.out.println("打印数据x="+m.x);
- System.out.println("打印数据y="+m.y);
- System.out.println("打印数据z="+m.z);
- System.out.println("打印数据a="+m.a);
- System.out.println("打印数据b="+m.b);
- System.out.println("打印数据c="+m.c);
- System.out.println("打印数据d="+m.d);
- System.out.println("打印数据e="+m.e);
- }
- }
复制代码 |