switch循环语句
switch语句
表达式的值:byte、short、int、char
JDK1.5之后,加入了枚举类型;
JDK1.7之后,加入了String类型;
break:中断switch语句的执行;
default:所有的情况都不匹配的时候,就执行该处的语句块;
掌握switch语句的执行顺序
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个日期:");
int day = sc.nextInt();
switch(day){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("该数字不属于日期");
}
while循环语句
基本格式:
while(判断条件语句){
循环体语句;
}
扩展格式
初始化语句;
while(判断条件语句){
循环体语句;
控制条件语句;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//被猜的数字
//int x = 354;
//随机产生 Math.random()-----0.0-0.9999999...
//Math.random()*1000-----0.0-999.9999...
//(int)((Math.random()*1000)+1)---1-1000
int x = (int)((Math.random()*1000)+1);
//System.out.println("要猜的数字:" + x);
System.out.println("请输入一个数:");
int guess = sc.nextInt();
while(guess != x){
if(guess < x){
System.out.println("猜小了");
}else if(guess > x){
System.out.println("猜大了");
}
System.out.println("继续猜");
guess = sc.nextInt();
}
if (guess == x){
System.out.println("恭喜你猜到了!");
}
}
do–while循环语句
基本格式:do{
循环体语句;
}while(判断条件语句);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = (int)((Math.random()*1000)+1);
System.out.println("请输入一个数:");
int guess = sc.nextInt();
do{
if(guess < x){
System.out.println("猜小了");
}else if(guess > x){
System.out.println("猜大了");
}
System.out.println("继续猜");
guess = sc.nextInt();
}while(guess != x);
if (guess == x){
System.out.println("恭喜你猜到了!");
}
}
}
break和continue的区别:
continue:使用循环结构中,用于结束本次循环继续下一次循环。
break:可以使用在switch结构和循环结构中,用于跳出当前结构。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = (int)((Math.random()*1000)+1);
System.out.println("请输入一个数:");
int guess = sc.nextInt();
do{
if(guess < x){
System.out.println("猜小了");
}else if(guess > x){
System.out.println("猜大了");
}
System.out.println("继续猜");
guess = sc.nextInt();
}while(guess != x);
if (guess == x){
System.out.println("恭喜你猜到了!");
}
}
}
数组:
数组的初始化:必须先初始化,后使用。
初始化:数组中的数组元素进行分配内存空间,并且为每一个数组元素进行赋值。
两种:1.静态初始化:初始化的时候,指定了每一个数组元素的初始化的值,有系统来决定数组的长度;
2.动态初始化:初始化的时候,只指定数组的长度,由系统来为数组分配初始值。
public static void main(String[] args) {
//数组的初始化:动态初始化
int[] arr = new int[6];//0,0,0,0,0,0
arr[0] = 1;//1,0,0,0,0,0
arr[3] = 3;
//静态初始化
int[] arr1 = {1,3,5};
int[] arr2 = new int[]{1,4,6};
int[] arr3;
arr3 = new int[]{1,3,5};
System.out.println(arr3[0]);
System.out.println(arr3[1]);
System.out.println(arr3[2]);
for(int i = 0;i < arr3.length; i++){
System.out.println(arr3[i]);
Java中的内存分配:
栈:
存储的是局部变量:
局部变量就是定义在方法中的变量:使用完毕之后就会被释放,立即回收;
堆:
存储的是new出来的对象:实体;
每一个对象都是有地址值的;
每一个对象的数据都是有默认值的:
byte、short、long-----0
float、double--------0.0
char----------------’\u0000’
boolean--------------false
引用类型-------------null
使用完毕之后,会在垃圾回收区空闲的时候,被回收。
方法区:
本地方法区:(和系统有关系)
寄存器:(给CPU使用的)
异常:
数组越界异常
ArrayIndexOutOfBoundsException
访问了数组中不存在的索引
空指针异常
NullPointerException
数组的引用没有指向对象,但是却在操作对象中的元素。
int[] arr = {1,3,5};
//访问数组的元素
//System.out.println(arr[5]);
//arr = null;
//System.out.println(arr[1]);
try {
arr = null;
System.out.println(arr[1]);
//System.out.println(arr[5]);
}catch(NullPointerException e){
System.out.println("发生了空指针异常");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("发生了数组越界异常");
}catch(Exception e){
e.printStackTrace();
}
System.out.println("1234");