Java基础day3

Java基础语法

1. switch语句

1.1 switch语句结构

格式

switch (表达式) { 
case 1: 
	语句体1; 
	break; 
case 2: 
	语句体2; 
	break; 
... 
default: 
	语句体n+1; 
	break; //可省略
}

1.2 switch语句练习-春夏秋冬分类

一年四季月份分类:
春:3、4、5
夏:6、7、8
秋:9、10、11
冬:1、2、12

示例代码:

import  java.util.Scanner;
 public class HelloWorld{
	 public static void main(String[] args){
		 Scanner sc = new Scanner(System.in);
		 System.out.println("请输入一个月份:");
		 int month = sc.nextInt();
		 //case穿透
		 switch(month){
			case 1:
			case 2:
			case 12:
				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("你输入的月份有误");
		}
	 }
 }
 

2. for循环

2.1 for循环格式

or循环格式:

for (初始化语句;条件判断语句;条件控制语句) { 
	循环体语句; 
}

2.2 for循环练习应用

要求:输出1-5 和 5-1
示例:

public class HelloWorld{
	 public static void main(String[] args){
		for(int i =1; i<=5; i++){
			System.out.println(i);
		}
		System.out.println("-----");
		for(int i =5; i>=1; i--){
			System.out.println(i);
		}
		// System.out.println("-----");
	 }
 }
 

2.3 for循环练习-求和

要求:1-5求和
示例:

 public class HelloWorld{
	public static void main(String[] args){
		int sum = 0;
		for(int i = 1; i<=5; i++){
			sum += i;
		}
		System.out.println(sum);
	}
 }
 

2.4 for循环练习-求偶数和

需求:求1-100之间的偶数和,并把求和结果在控制台输出 }
示例代码:

 public class HelloWorld{
	public static void main(String[] args){
		int sum = 0;
		for(int i = 1; i<=100; i++){
			if(i%2 == 0 ){
				sum += i;
			}	 
		}
		System.out.println(sum);
	}
 }

2.5 for循环练习-水仙花(应用)

需求:在控制台输出所有的“水仙花数”
解释:什么是水仙花数?
水仙花数,指的是一个三位数,个位、十位、百位的数字立方和等于原数

public class HelloWorld { 
	public static void main(String[] args) { 
	//输出所有的水仙花数必然要使用到循环,遍历所有的三位数,三位数从100开始,到999结束 
		for(int i=100; i<1000; i++) {
			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);
			}
		}
	}
}
 

2.6 for循环练习-统计水仙花数个数(应用)

 public class HelloWorld { 
	public static void main(String[] args) { 
		int count = 0;
		for(int i=100; i<1000; i++) {
			int ge = i%10;
			int shi = i/10%10;
			int bai = i/100;
			//条件
			if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
				count += 1;	
			}
		}
		System.out.println(count);
	}
}
 

3. while循环

while循环完整格式:

初始化语句; 
while (条件判断语句) { 
	循环体语句; 
	条件控制语句; 
}

举例://输出五次hello world

public class HelloWorld { 
	public static void main(String[] args) { 
		int i = 0;
		while(i<=5){
			System.out.println("hello world!");
			i++;
		}
	}
 }

3.2 while循环练习-珠穆朗玛峰(应用)

  • 需求:世界最高山峰是珠穆朗玛峰(8844.43米=8844430毫米),假如我有一张足够大的纸,它的厚度是0.1毫
    米。请问,我折叠多少次,可以折成珠穆朗玛峰的高度?
  • 示例代码:
public class HelloWorld { 
	public static void main(String[] args) {
		int count = 0;
		double height = 0.1;
		while(height <= 884430){
			height *= 2;
			count += 1;
		}
		System.out.println(count);
	}
}

4. 循环细节

4.1 do…while循环结构(掌握)

  • 完整格式:
初始化语句; 
do {
	循环体语句; 
	条件控制语句; 
}while(条件判断语句);

举例:

public class HelloWorld { 
	public static void main(String[] args) {
		int i = 1;
		do{
			System.out.println("hello world!");
			i += 1;
		}while(i <= 5);
	}
}

4.2 三种循环的区别

先判断再执行,还是先执行再判断
定义变量是否在语法结构内

死循环(无限循环)的三种格式
1. for(;;){}
2. while(true){}
3. do {} while(true);

4.3 跳转控制语句

跳转控制语句(break)
	跳出循环,结束循环
跳转控制语句(continue)
	跳过本次循环,继续下次循环
注意: continue只能在循环中进行使用!

4.4 循环嵌套

循环嵌套概述:在循环中,继续定义循环
示例代码://输出一天时间的表示

 public class HelloWorld{
	public static void main(String[] args){
		for (int hour = 0; hour<=24; hour++){
			for (int minute = 0; minute<=60; minute++){
				System.out.println(hour + ":" + minute);
			}
		}	
	}
 }

5. Random

5.1 Random产生随机数

使用步骤:
 - 导入包
import java.util.Random;
 - 创建对象
Random r = new Random();
 - 产生随机数
int num = r.nextInt(10);//产生0-9的数字

5.2 Random练习-猜数字(应用)

  • 需求: 程序自动生成一个1-100之间的数字,使用程序实现猜出这个数字是多少? 当猜错的时候根据不同情况给出相应的提示 A.
    如果猜的数字比真实数字大,提示你猜的数据大了 B. 如果猜的数字比真实数字小,提示你猜的数据小了 C.
    如果猜的数字与真实数字相等,提示恭喜你猜中了
  • 示例代码:
 import java.util.Random;
 import java.util.Scanner;
 
 public class HelloWorld{
	 public static void main(String[] args){
		 Random r = new Random();
		 int number = r.nextInt(100) + 1;
		 
		 while(true){
			 Scanner sc = new Scanner(System.in);
			 System.out.println("请输入你要猜的数字");
			 int guesNumber = sc.nextInt();
			 
			 if(guesNumber > number){
				 System.out.println("你猜的数字太大了");
			 } else if(guesNumber < number){
				 System.out.println("你猜的数字太小了");
			 } else{
				 System.out.println("恭喜你猜对了");	 
				 break;
			 }
		 }
	 }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值