码迷,mamicode.com
首页 > 编程语言 > 详细

Java基础案例

时间:2017-05-30 00:04:32      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:驼峰   建议   连接   default   logs   else   5.6   自动类型转换   code   

1.变量及基本数据类型

案例1:变量声明及赋值

            //1.变量的声明
		int a; //声明一个整型的变量a
		int b,c,d; //声明三个整型变量b,c,d
		
		//2.变量的初始化
		int a = 250; //声明整型变量a并赋值为250
		
		//声明整型变量b,然后赋值为250
		int b;
		b = 250;
		
		//3.变量的使用
		int a = 5;
		int b = b+10; //取出a的值5,加10后赋值给b
		System.out.println(b); //输出b的值15
		System.out.println("b");  //输出字符串b
		a=a+10;  //取出a的值5,加上10后再赋值给a
		System.out.println(a);  //15
		
		int c = 3.14; //编译错误,数据类型不匹配
		System.out.println(m);  //编译错误,m未声明
		
		int m;
		System.out.println(m);  //编译错误,m未初始化
		
		//4.变量的命名
		int a1,a_5$,$_;   //正确命名
		int a*b;   //编译错误,除了_和$,不能包含*等特殊符号
		int 1c;     //编译错误,不能以数字开头
		int static;   //编译错误,不能使用关键字
		int a = 6;
		System.out.println(A);   //编译错误,严格区分大小写
		int 年龄;   //正确,但不建议
		int age;    //正确,建议“见名知意
		int score,myScore,myJavaScore;   //正确,建议"驼峰命名法"
		
		
		//声明整型变量a并赋值为25.67
		int a = 25.67;   //编译错误,类型不匹配

案例2:数据类型转换

          //1.int整型
		int a = 3000000000;   //编译错误,30亿超出int范围
		System.out.println(5/2);  //2  整数直接量默认为int,两个int运算得到的还是int型,小数位无条件舍弃
		System.out.println(2/5);  //0
		System.out.println(5/2.0);  //2.5  发生自动类型转换,int转换为double
		
		int c = 2147483647;  //int范围最大值
		c +=1;
		System.out.println(c);  //-2147483648 发生溢出,溢出是需要避免的
		
		//2.long长整型
		long a = 10000000000;  //编译错误,100亿默认为int型,但超范围了
		long b = 10000000000L;  //100亿L为长整型直接量
		
		long c = 10000000*2*10L;
		System.out.println(c);  //200亿
		
		long d = 10000000*3*10L;
		System.out.println(d);  //发生溢出,10亿*3默认是int型,但是30亿已超出int范围
		
		long e = 100000000L*3*10;
		System.out.println(e);  //300亿,在计算有可能发生溢出的时候建议第一个数加上L
		
		long f = System.currentTimeMillis();  //从1970.1.1零时到现在的毫秒数是long型
		
		
		//3.double浮点型
		double pi = 3.14;  //3.14为浮点数直接量,默认为double型
		float pi = 3.14F;
		double a=6.0,b=4.9;
		System.out.println(a);  //1.0999999999999996,舍入误差,精确计算不建议double
		

  

2.运算符

 案例1:运算符的演示

public class Test {
	public static void main(String[] args){
		int a = 2, b = 7, c=5;
		System.out.println(a++);  //2
		System.out.println(a);    //3
		System.out.println(++b);  //8
		/*
		 * 当变量参与运算时,++x,先自加再运算
		 * 当变量参与运算时,x++,先运算在自加
		 */
		c = c++;    
		/*
		 * 运算顺序:
		 * 1.c取出自己内存的值5
		 * 2.进行自加c变成6
		 * 3.c++参与运算时的整体值是5
		 * 4.然后将(c++)整体值赋值给c
		 * 5.c的值又变成5
		 */
		System.out.println(c);  //5
	}
}

  

案例2:字符串连接

public class Test {
	public static void main(String[] args) {
		int a =1, b = 2;
		String o = "qwer";
		String P = a+b+o+a+b;  
		/*
		 *任何类型与字符串类型拼接,得到的依然是字符串类型
		 *计算顺序:
		 *1.先计算a+b,两个int相加得到int,结果为3
		 *2.再+o为字符串,得到字符串3qwer
		 *3再+a+b,得到的依然是字符串,结果为3qwer12 
		 */
		System.out.println(P);  //3qwer12
	}
}

  

案例3:三目运算输出三个数的最大值

ublic class Test {
	public static void main(String[] args) {
		int a = 50, b = 90, c = 20;
		int max = (a>b?a:b)>c?(a>b?a:b):c;
		System.out.println(max);
	}
}

 

案例4:逻辑运算符演示

public class Test {
	public static void main(String[] args) {
		int a = 1;
		int b = 2;
		boolean c = a>1 && ++a>1;
		boolean d = a++>1 || b--<2;
		boolean e = a<1 || ++b==3;
		System.out.println(a);     
		System.out.println(b);    
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
	}
}


/*
2
2
false
false
false
*/

 

 

3.分支结构

案例1:判断某一年的某一月是多少天

技术分享
 1 import java.util.Scanner;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         //用户输入数值
 6         Scanner scan = new Scanner(System.in);
 7         System.out.print("请输入查询的年份:");
 8         int year = scan.nextInt();
 9         System.out.print("请输入查询的月份:");
10         int month = scan.nextInt();
11         int days;  //用于记录天数
12         switch(month){
13         case 2:
14             if(year%400==0 || (year%4==0&&year%100!=0)){
15                 days = 29;
16                 break;
17             }else{
18                 days = 28;
19                 break;
20             }
21         case 4:
22         case 6:
23         case 9:
24         case 11:
25             days = 30;
26             break;
27         default:
28             days = 31;
29             break;
30         }
31         System.out.println(year+"年的"+month+"月有"+days+"天");
32     }
33 }
View Code

 

4.循环结构

 案例1:打印99乘法表

public class Test {
	public static void main(String[] args) {
		for(int i=1;i<10;i++){
			for(int j=1;j<=i;j++){
				System.out.print(j+"*"+i+"="+j*i+"\t");
			}
			System.out.println();
		}
	}
}

案例2:输入一个int整数,算出这个整数每个数字的和

比如输入321,则输出3+2+1=6

技术分享
 1 import java.util.Arrays;
 2 /*
 3 1.  3212/10=321...2
 4 2.  321/10=21....1
 5 3.  21/10=2....1
 6 4.  2/10=0....0
 7 规律:a/10最后结果为0的时候停止循环,即数字<10
 8 所以当a每次除10之后的数字>0的时候循环
 9  */
10 import java.util.Scanner;
11 
12 public class Test {
13     public static void main(String[] args) {
14         System.out.print("输入一个整数:");
15         Scanner scan = new Scanner(System.in);
16         String input = scan.nextLine();
17         int num = Integer.parseInt(input);  //将输入的字符串转化为数字
18         int sum = 0; //用于记录每个数的和
19         char[] arr = input.toCharArray();   //将输入的字符串转换为字符数组
20         while(true){
21             sum += num%10;
22             if(num<10){
23                 break;
24             }
25             num /= 10;
26         }
27         for(int i=0;i<arr.length-1;i++){
28             System.out.print(arr[i]+"+");
29         }
30         System.out.print(arr[arr.length-1]);
31         System.out.println("="+sum);
32     }
33 }
View Code

 

 

5.数组

 

6.方法

 

Java基础案例

标签:驼峰   建议   连接   default   logs   else   5.6   自动类型转换   code   

原文地址:http://www.cnblogs.com/jmwm/p/6919366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!