JavaSE基础第三天
分支结构
if
switch
switch分支注意事项:
① 表达式类型只能是byte、short、int、char,JDK5开始支持枚举,JDK7开始支持String、
不支持double、float、long。
② case给出的值不允许重复,且只能是字面量,不能是变量。
③ 不要忘记写break,否则会出现穿透现象。
public class SwitchDemo3 {
public static void main(String[] args) {
//目标:清楚switch的注意点,并在开发时注意
// ① 表达式类型只能是byte、short、int、char,JDK5开始支持枚举,JDK7开始支持String、
// 不支持double、float、long。
double a = 0.1+0.2;
System.out.println(a);
// switch(a){
//
// }
// ② case给出的值不允许重复,且只能是字面量,不能是变量。
int a1 = 3;
switch (3){
case 3:
break;
// case 3:
// break;
// case a1:
// break;
}
// ③ 不要忘记写break,否则会出现穿透现象。
}
}
switch的穿透性
public class SwitchDemo4 {
public static void main(String[] args) {
// 需求:用户输入月份可以展示该月份的天数。
// ⚫ 1、3 、5、 7 、 8、 10、 12月份是 31天
// ⚫ 2月份是闰年为29天、非闰年为28天。
// ⚫ 4 、6 、9、 11月份 是30天
int month = 7;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(month + "月是31天");
break;
case 2:
case 4:
case 6:
case 9:
case 11:
System.out.println(month + "月是30天");
break;
default:
System.out.println("数据有误");
break;
}
}
}
循环结构
for循环
//求1-10的奇数和
public class ForText3 {
public static void main(String[] args) {
//需求:求1-10的奇数和
int sum = 0;
for(int i = 1; i <= 10; i++){
if(i % 2 == 1){
sum += i;
}
}
System.out.println("1-10的奇数和是:" + sum);
sum = 0;
for (int i = 1; i <= 10; i+=2) {
sum += i;
}
System.out.println("1-10的奇数和是:" + sum);
}
}
//水仙花数
public class ForTest4 {
public static void main(String[] args) {
//需求:找出水仙花数并输出
//1.定义一个for循环找出全部的三位数
int count = 0;
for (int i = 100; i <= 999 ; i++) {
//2.判断这个三位数是否满足要求
//个位
int ge = i % 10;
//十位
int shi = i / 10 % 10;
//百位
int bai = i / 100;
if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
//System.out.println(i);
count++;
System.out.print(i+"\t");//153 370 371 407
}
}
System.out.println();//换行
System.out.println("水仙花个数是:" + count);
}
}
while循环
public class WhileTest6 {
public static void main(String[] args) {
//需求:珠穆朗玛峰高度884860 纸张厚度 0.1 折叠纸张直到不低于珠穆朗玛峰为止,求折叠几次
double PeakHeight = 8848860;//山峰高度
double PaperThickness = 0.1;//纸张厚度
int count = 0;
while(PaperThickness < PeakHeight){
PaperThickness *= 2;
count++;
}
System.out.println("折叠次数为" + count);
System.out.println("纸张的最终厚度为:" + PaperThickness);
}
}
do-while循环
死循环
public class DeadForDemo8 {
public static void main(String[] args) {
//目标:学会定义死循环
// for (; ; ) {
// System.out.println("HelloWorld");
//
// }
//经典写法
// while(true){
// System.out.println("我是快乐的死循环~~");
// }
// do{
// System.out.println("我是快乐的死循环~~");
// }while(true);
//1.定义正确的密码
int okPassword = 520;
//2.定义死循环让用户不断输入密码认证
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请您输入正确的密码:");
int password = sc.nextInt();
//3.使用if判断密码是否正确
if(password == okPassword){
System.out.println("登陆成功");
break;//可以立即结束当前所在循环的执行的
}else
{
System.out.println("密码错误");
}
}
}
}
循环嵌套
跳转关键字:break,continue
public class BreakAndContinueDemo10 {
public static void main(String[] args) {
//目标:理解break和continue的作用
//场景:假如你又有老婆了,然后你犯错了,你老婆罚你做5天家务,每天都是洗碗
//但是洗碗到第三天后老婆心软了,原谅你了就不用去洗了
for (int i = 0; i < 5; i++) {
System.out.println("快乐地洗碗");
if(i == 2){
break;//跳出并结束当前循环的执行
}
}
//continue跳出当前循环的当次执行,进入循环的下一次
//场景:假如你又有老婆了,然后你犯错了,你老婆罚你做5天家务,每天都是洗碗
//但是洗碗到第三天后老婆心软了,原谅你了就不用去洗了 但是依然不解恨,继续洗第4天第5天
for (int i = 1; i <=5 ; i++) {
if(i == 3){
continue;
}
System.out.println("洗碗:" + i);
}
}
}
案例分析:随机数Random类
猜数字:
public class RandomTest2 {
public static void main(String[] args) {
//1.随机一个幸运号码
Random r = new Random();
int luckNumber = r.nextInt(100) + 1;
//2.使用一个死循环让用户不断去猜测,并给出提示
Scanner sc = new Scanner(System.in);
while(true){
//让用户输入数据猜测
System.out.println("请您输入猜测的数据(1-100)");
int guessNumber = sc.nextInt();
//3.判断这个猜测的号码与幸运号码的大小情况
if(guessNumber > luckNumber){
System.out.println("您猜测的数据过大");
}else if(guessNumber < luckNumber){
System.out.println("您猜测的数据过小");
}else{
System.out.println("恭喜您猜中了,可以去买单了");
break;
}
}
}
}
部分图片来自黑马程序员