一 switch
1.基本使用
1.格式:
switch(变量){
case 目标值1:
执行语句1;
break;
case 目标值2:
执行语句2;
break;
case 目标值3:
执行语句3;
break;
…
default:
执行语句n;
break;
}
2.执行流程:
用变量代表的值,去和下面每一个case后面的目标值做匹配,匹配上哪个case,就执行哪个case后面的执行语句
走对应break,结束当前switch;如果以上所有的case,都没有匹配上,就走default对应的执行语句n
3.break:代表的是结束switch语句
4.如果没有break,会出现case的穿透性,一直往下穿透执行,直到switch结束了,或者遇到break了
public class Demo01Switch {
public static void main(String[] args) {
int i = 4;
switch (i){
case 1:
System.out.println("我是case1");
break;
case 2:
System.out.println("我是case2");
break;
case 3:
System.out.println("我是case3");
break;
default:
System.out.println("我是default");
break;
}
}
}
//case的穿透性
public class Demo02Switch {
public static void main(String[] args) {
int i = 2;
switch (i) {
case 1:
System.out.println("我是case1");
case 2:
System.out.println("我是case2");
case 3:
System.out.println("我是case3");
break;
default:
System.out.println("我是default");
}
}
}
2.练习
需求:键盘录入一个整数1-12的月份,输出对应的季节
public class Demo03SwitchTest {
public static void main(String[] args) {
//创建Scanner对象
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请您输入一个月份:");
int month = sc.nextInt();
switch (month) {
case 12:
System.out.println("冬季");
break;
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;
case 8:
System.out.println("夏季");
break;
case 9:
System.out.println("秋季");
break;
case 10:
System.out.println("秋季");
break;
case 11:
System.out.println("秋季");
break;
default:
System.out.println("输入有误");
break;
}
}
}
}
public class Demo04SwitchTest {
public static void main(String[] args) {
//创建Scanner对象
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请您输入一个月份:");
int month = sc.nextInt();
switch (month) {
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
default:
System.out.println("输入有误");
break;
//return;//结束方法
// System.exit(0);//退出jvm
}
}
}
}
二 continue和break
break:
在switch中:结束switch
在循环中:结束循环
continue:
结束的本次循环,进入到下一次循环
public class Test01 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i==3){
//break;
continue;
}
System.out.println("我爱Java"+i);
}
}
}
三 Random
1.概述:Java自带的一个类
2.作用:生成随机数
3.使用:
a.导包:java.util.Random
b.创建对象:Random 名字 = new Random()
c.调用方法:
nextInt()->在int的取值范围内随机一个整数
nextInt(int n)-> 0到(n-1)
4.需求:
a.在1-100之间随机一个数
nextInt(100)+1 -> (0-99)+1–> 1 -100
b.在所有的三位数中随机一个整数 100-999
nextInt(900)+100 ->(0-899)+100 -> 100-999
public class Demo01Random {
public static void main(String[] args) {
Random rd = new Random();
int data = rd.nextInt(10);
System.out.println(data);
int data1 = rd.nextInt(100)+1;
System.out.println(data1);
}
}
练习:猜数字小游戏
要求:在1-100之间随机一个数,通过键盘录入的形式去猜这个随机数
步骤:
1.创建Random对象
2.创建Scanner对象
3.调用Random中的nextInt(100)+1,随机一个1-100之间的数字 ->randomNumber
4.调用Scanner中的nextInt()猜一个数字 ->scNumber
5.如果scNumber>randomNumber 输出"您猜大了"
如果scNumber<randomNumber 输出"您猜小了"
如果scNumber = randomNumber 输出"恭喜您,猜中了!"
public class Test {
public static void main(String[] args) {
//1.创建Random对象
Random rd = new Random();
//2.创建Scanner对象
Scanner sc = new Scanner(System.in);
//3.调用Random中的nextInt(100)+1,随机一个1-100之间的数字 ->randomNumber
int randomNumber = rd.nextInt(100) + 1;
//System.out.println(randomNumber);
while (true) {
//4.调用Scanner中的nextInt()猜一个数字 ->scNumber
int scNumber = sc.nextInt();
/* 5.如果scNumber>randomNumber 输出"您猜大了"
如果scNumber<randomNumber 输出"您猜小了"
如果scNumber = randomNumber 输出"恭喜您,猜中了!"*/
if (scNumber > randomNumber) {
System.out.println("您猜大了!");
} else if (scNumber < randomNumber) {
System.out.println("您猜小了!");
} else {
System.out.println("恭喜您猜中了!");
break;//结束循环
}
}
}
}
四 debug
1.概述:找bug的一种手段,代码调试
2.步骤:
a.在想要开始运行的当前行左边单击一下,出现小圆点
五 方法的重载
需求:定义方法,完成2个整数相加,3个整数相加,4个整数相加
public class Method01 {
public static void main(String[] args) {
sum(10,20);
sum(1,2,3);
sum(1,2,3,4);
}
//2个数相加
public static void sum(int a,int b){
int sum = a+b;
System.out.println(sum);
}
//3个数相加
public static void sum(int a,int b,int c){
int sum = a+b+c;
System.out.println(sum);
}
//4个数相加
public static void sum(int a,int b,int c,int d){
int sum = a+b+c+d;
System.out.println(sum);
}
}
1.什么是重载方法
方法名相同,参数列表不同
2.重载的方法必须是在同一个类中
3.什么叫参数列表不同呢
a.参数个数不同
b.参数类型不同
c.参数类型的顺序不同
public static void open(){}
public static void open(int a){}
static void open(int a,int b){}
public static void open(double a,int b){}
public static void open(int a,double b){}
public void open(int i,double d){}
public static void OPEN(){}
public static void open(int i,int j){}
六 循环嵌套
执行流程:
先走外层for循环,再走内层for循环,那么内层for循环就一直循环,直到内层for循环结束,外层for循环进行下一次循环,直到外层for循环结束,那么所有循环就都结束了.
public class Test01 {
public static void main(String[] args) {
for (int fen = 0; fen < 60; fen++) {
for (int miao = 0; miao < 60; miao++) {
System.out.println(fen+"分"+miao+"秒");
}
}
}
}
七 方法参数
1.基本数据类型做方法参数传递
局部变量:定义在方法内部或者方法参数位置的变量
自己方法内部的局部变量的值发生变化,不会影响其他方法的局部变量
2.引用数据类型做方法参数传递
引用类型做方法参数传递,一个方法对这个引用类型做改变会影响另一个方法
3.练习
在编程竞赛中,有6个评委为参赛的选手打分,分数为0-100的整数分。
选手的最后得分为:去掉一个最高分和一个最低分后 的4个评委平均值 (不考虑小数部分)。
步骤:
1.在main方法中,定义一个数组,长度为6
2.创建Scanner对象
3.循环录入,将录入的分数存储到数组中
4.求出数组中的最大值,最小值,和
5.和-最大值-最小值 除以 数组长度-2
public class Test03 {
public static void main(String[] args) {
//定义数组
int[] arr = new int[6];
//创建Scanner
Scanner sc = new Scanner(System.in);
//循环录入,将录入的存储到数组中
for (int i = 0; i < arr.length; i++) {
System.out.println("请您输入第"+(i+1)+"个分数:");
int s = sc.nextInt();
arr[i] = s;
}
int max = getMax(arr);
int min = getMin(arr);
int sum = getSum(arr);
//求平均值
int result = (sum - max - min) / (arr.length - 2);
System.out.println(result);
}
//求最大值
public static int getMax(int[] arr){
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (max<arr[i]){
max = arr[i];
}
}
return max;
}
//求最小值
public static int getMin(int[] arr){
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if (min>arr[i]){
min = arr[i];
}
}
return min;
}
//求和
public static int getSum(int[] arr){
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum+=arr[i];
}
return sum;
}
}