java 数据类型及作用域、数据类型转换、运算符、流程控制

1、数据类型及作用域

package debug;
public class Datatype {
	
	private int a2;
	private static float a3;//静态方法只能调用静态全局变量
	private double a4;
	private byte a5;
	private short a6;
	private long a7;
	private String a8;//类变量,作用域为整个类,下面的所有方法都可以使用的变量。
	final int AAA=900;//常量,使用过程中不被改变
	
	double fun1(double a1,double a2) {//a1、a2方法参数变量,作用域为整个方法
		double a3 = 0;//a3局部变量,定义于{}中或方法中,作用域为定义变量处到函数体结束
		return a1+a2+a3;
	}
	
	
	
	public static void main(String[] args) {
		int a=077;//8进制,局部变量,定义于{}中或方法中
		int vv=0xABCDEF9;//16进制
		short a1=1;
		a1=(short) (a1+1);//报错
		a1+=1;//为什么不报错
		
		
		//数据类型:基本+引用
		//基本:
		byte a11=-128;//(127) 1
		short a12=-32768;//(32767) 2
		int a13=-2147483648;//(2147483647) 4 10位
		long a14=-9223372036854775808L;//9223372036854775807L 8 19位
		
		float a15=0.00f;//4 加f/F
		
		double a16=0.00;//8  默认双精度
		
		boolean a17=true;//1 不能进行类型转换
		char a18='a';//2 char a18=98;
		
		byte a19=(byte) a18;
		
		a19=(byte) a12;
		short a22=a19;//short > byte
		
		a19=(byte) a22;
		a22=(short) a18;//short=char 
		a18=(char) a22;//short=char 
		
		//引用:字符串,数组,类,接口					
	}	

}

2、类型转换

package debug;
public class Type_to_type {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//类型:字符(char)、数值(byte short int long float double)、布尔(boolean)、String、Date
		
		//1、简单数据类型转换
		
		//自动转换:(byte short char)-int-long-float-double
		byte a1 = 0;
		int a2=a1;
		long a3=a2;
		float a4=a3;
		double a5=a4;
		
		//强制转换:
		short a6=a1;
		char a7=(char) a1;//同级强制
		char a8=(char) 99;//高级到低级强制	
		System.out.println(a8);
		
		//包装过渡类转换:Boolean\Character\Byte\Short\Integer\Long\Float\Double:适用于自动强制转换
		float a9=100.009f;
		Float a10=new Float(a9);
		Short a21=new Short(a6);
		Byte a22=new Byte(a1);
		short a12=a10.shortValue();
		int a13=a10.intValue();
		byte a14=a10.byteValue();
		long a15=a10.longValue();
		double a16=a10.doubleValue();
		
		//2、简单数据类型使用包装类转换为String类型:
		String str1=a10.toString();
		
		//3、String类型转换为简单数据类型:
		String str2="100";
		int a17=Integer.parseInt(str2);
		short a18=Short.parseShort(str2);
		float a19=Float.parseFloat(str2);
		byte a20=Byte.parseByte(str2);
		long a23=Long.parseLong(str2);
		double a24=Double.parseDouble(str2);
		//char a25=Character.没有
		
		byte a26=(byte) Character.getNumericValue('3');
		int a27=Character.getNumericValue('3');//是多少就是多少
		int a28='a';//ascII编码 97
		int a29='A';//65
		System.out.println(a26);
		System.out.println(a27);
		System.out.println(a28);
		System.out.println(a29);
	}
}



3、运算符

package debug;

public class Operator {
	
	//+、-、*、/、% 
	//、=、+=、-=、*=、/=、%=
	//>、>=、<、<=、==、!=
	//&&、||、!
	//&、|、^、~、>>、<<、>>>
	//?:
	//i++、++i、i--、--i
	
	public static void main(String[] args) {
		char c=(char) -24;//=左右的类型必须一致,自动进行类型转换
		System.out.println((false)?10.9:9);//?:运算符要求:左右两边的数据类型要一致  9.0
		System.out.println(5/2.0);//类型转换
	}
}


4、if 、if selse  、if selse 嵌套、 switch 、while 、 for  、foreach、do while  

package debug;
import java.util.Scanner;
public class Colloter {
	public static void main(String[] args) {
				
		
		//if
		int a=34;
		int b=30;
		if(a<b) {
			System.out.println("a<b");
		}
		if(a>b) {
			System.out.println("a>b");
		}
		
		
		//if else
		if(a<b) {
			System.out.println("a<b");
		}else {
			System.out.println("a>=b");
		}
		
		
		//if else if else if······
		@SuppressWarnings("resource")
		Scanner scan=new Scanner(System.in);
		int aa=scan.nextInt();
		if(aa>=90) {
			System.out.println("优");
		}else if(aa>=80) {
			System.out.println("良");
		}else if(aa>=70) {
			System.out.println("中");
		}else if(aa>=60) {
			System.out.println("差");
		}else {
			System.out.println("不及格");
		}
		
		
		//switch case break default
		int bb=scan.nextInt();
		switch(bb%10) {
			case 0:
			case 1:
			case 2:
			case 3:
			System.out.println("一等奖");
			break;
			case 4:
			case 5:
			case 6:
			case 7:
				System.out.println("二等奖");
				break;
			case 8:
				System.out.println("三等奖");
				break;
		    default:
		    	System.out.println("hahahahh");
		    	break;
		}
		
		
		//for循环
		System.out.println("乘法口诀表:");
		for(int i=1;i<=9;i++) {
			for(int j=1;j<=i;j++) {
				System.out.print(j+"*"+j+"="+j*j+'\t');
			}
			System.out.println();
		}
		
		
		//foreach循环语句
		String[] days= {"lll","hhh","ggg","mmm","fff","nnn"};
		for(String day:days) {
			System.out.println(day);
		}
		
		
		//while循环
		int i=2,sum=0;
		while(i<=100) {
			if(i%2==0) {
				sum=sum+i;
			}
			i++;
		}
		System.out.println(sum);
		
		
		//do while
		i=2;sum=0;
		do {
			sum+=i;
			i+=2;
		}while(i<=100);
		System.out.println(sum);
		
		
		//break 本轮  while for foreach switch
		for(int i1=0;i1<3;i1++) {
			System.out.println("第"+i1+"轮");
			for(int j1=0;j1<100;j1++) {
				if(j1==10) {
					break;
				}
				System.out.print(j1+" ");//注意这里写成‘ ’的话会出大问题
			}
			System.out.println();
		}
		
		
		//continue 本次 while for foreach
		for(int i1=0;i1<3;i1++) {
			System.out.println("第"+i1+"轮");
			for(int j1=0;j1<100;j1++) {
				if(j1==10) {
					continue;
				}
				System.out.print(j1+" ");//注意这里写成‘ ’的话会出大问题
			}
			System.out.println();
		}
		
		//return 返回基本类型,数组,对象
		
	}
}


猜你喜欢

转载自blog.csdn.net/cincoutcin/article/details/79794510