JAVA(数据类型,控制,循环)
JAVA(数据类型,控制,循环)
引用数据类型:
- String(String是个类,JDK为我们提供的)
1 2 | String a= "abc" ; //String只能用双引号 |
- Scanner(构建一个工具,键盘输入)
1 | Scanner sc= new Scanner(System.in);<br>sc.next();<br> //返回值是一个字符串 |
- 数组
声明:int [] a=new int[5];//声明必须指定长度
int[] a1={1,3,4};
int a2[]={1,3,4};
int[] a3=new int[]{1,2,3,4,5};
地址:int[] a1=new int[]; 打印a1 得[I@6a5fc7f7 ,打印a[0]得0
二维数组:int [][] a=new int[5][5];//int [][] a=new int[5][];行必须赋值,列可以不给值
数据类型得初始值:
- byte,short,int,long:0
- float,double:0.0
- char:\u0000 -> "
- boolran:false
- String:null
- int[]:null
包:
- 文件夹package
- 命名规范:尽量用小写字母,站点名的逆序,
流程控制:
if(){}else{}:
1 2 3 4 5 6 7 8 9 10 11 12 | Scanner sc= new Scanner(System.in); System.out.println( "请输入第一个数" ); int a=sc.nextInt(); System.out.println( "请输入第二个数" ); int b= sc.nextInt(); if (a== 8 &&b== 9 ){ System.out.println(a+b); } else if (a== 8 ||b== 9 ||a== 9 ||b== 8 ){ System.out.println(a-b); } else { System.out.println(a+ " " +b); }<br> |
switch:
- 只能判断整形:byte,short,int。
- 字符型:char。
- String(JDK1.7及以后的版本),枚举(JDK5及以后的版本)。
- defalut:位置灵活,规范放最后。
异常:
出现异常后,出现异常处后的代码不会执行
循环:
for循环:初始化条件可以是任意类型,
增强的for循环:foreach
1 2 3 4 5 | int [] a= new int [ 10 ]; for ( int i:a){ System.out.println(i); //i就是数组中的元素,操作的不是原始数据,是镜像数据,JDK5及以后才有 } |
二维数组遍历
1 2 3 4 5 6 | int [][] aaa= new int [ 2 ][ 2 ]; for ( int [] ints : aaa) { for ( int anInt : ints) { } } |
案例:输出正方形
1 2 3 4 5 6 7 8 9 10 | for ( int i = 0 ; i < 10 ; i++) { for ( int j= 0 ;j< 10 ;j++){ if (i== 0 ||j== 0 ||i== 9 ||j== 9 ){ System.out.print( "*" + " " ); } else if ((i> 0 &&i< 9 )||(j> 0 &&j< 9 )){ System.out.print( " " ); } } System.out.println( "" ); } |
break:跳出循环
continue:终止当前次循环,进入下一次循环
break和continue只能控制最近的
控制外层循环:goto功能
1 2 3 4 5 6 7 | a: for ( int i = 0 ; i < 5 ; i++) { for ( int j= 0 ;j< 5 ;j++){ if (j== 3 ){ break a; } } } |
案例:简单计算器
输入2和数和一个字符,打印输出结果,提示是否进入下一次计算,1进入下一次,2退出,其他重新输入
Scanner sc=new Scanner(System.in);
p:while(true){
System.out.println("请输入第一个数");
double a=sc.nextDouble();
System.out.println("请输入第二个数");
double b=sc.nextInt();
System.out.println("请输入字符");
char c=sc.next().charAt(0);
//计算结果,是否输入继续,1是,2否,其他
switch (c){
case '+':
System.out.println(a+"+"+b+"="+String.format("%.2f",(a+b)));break;
case '-':
System.out.println(a+"-"+b+"="+String.format("%.2f",(a-b)));break;
case '*':
System.out.println(a+"*"+b+"="+String.format("%.2f",(a*b)));break;
case '/':
if(b==0){
System.out.println("计算错误");
}else{
System.out.println(a+"/"+b+"="+String.format("%.2f",(a/b)));
};break;
default:
System.out.println("输入错误");break;
}
System.out.println("是否继续计算,1:继续计算;2:停止运算");
while(true){
String ag=sc.next();
switch (ag){
case "1":continue p;
case "2":break p;
default:System.out.println("请输入1或者2");break;
}
}
while循环:
while(true){
System.out.print("while");
}
do{}while():
1 2 3 4 | do { System.out.print( "do while" ); } while ( true ); |
区别:do..while至少执行一次
案例:猜数字
1 2 3 4 5 6 7 8 9 10 | Scanner sc= new Scanner(System.in); System.out.println( "请输入数字" ); int num=sc.nextInt(); System.out.println( "请开始" ); int a; while ( true ){ a=sc.nextInt(); if (a>num){ System.out.println( "大了" ); } else if (a |
案例:冒泡
1 2 3 4 5 6 7 8 9 10 11 12 13 | //冒泡 int [] a= new int []{ 1 , 34 , 6 , 6 , 8 , 243 , 0 ,- 1 }; for ( int i = 0 ; i < a.length; i++) { for ( int j= 0 ;ja[j+ 1 ]){ int t=a[j]; a[j]=a[j+ 1 ]; a[j+ 1 ]=t; } } } for ( int i = 0 ; i < a.length; i++) { System.out.print(a[i]+ " " ); } |
案例:空心菱形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | for ( int i = 0 ; i <= 5 ; i++) { for ( int t= 5 ;t>i;t--){ System.out.print( " " ); } for ( int j= 0 ;j< 2 *i- 1 ;j++){ if (j== 0 ||j== 2 *i- 2 ){ System.out.print( "* " ); } else { System.out.print( " " ); } } System.out.println(); } for ( int i = 4 ; i > 0 ; i--) { for ( int t= 5 ;t>i;t--){ System.out.print( " " ); } for ( int j= 0 ;j< 2 *i- 1 ;j++){ if (j== 0 ||j== 2 *i- 2 ){ System.out.print( "* " ); } else { System.out.print( " " ); } } System.out.println(); } |
案例:百鸡百钱
1 2 3 4 5 6 7 8 9 10 11 12 | //百钱百鸡 公鸡5,母鸡3,小鸡1 int x,y,z; int count= 0 ; for ( x = 0 ; x < 20 ; x++) { for (y= 0 ;y< 33 ;y++){ for (z= 0 ;z< 100 ;z+= 3 ){ if (((x + y + z == 100 ) && ( 5 * x + 3 * y + z / 3 == 100 ))){ System.out.println(x+ " " +y+ " " +z+ " " ); } } } } |