java 基础笔记 第三天
1.循环结构
1.1for循环的格式及基本使用
1.1.1循环语句基本格式
for(初始化语句;条件判断语句;控制条件语句){
循环体语句;
}
1.1.2 执行流程
1.执行初始化语句
2.执行条件判断语句,看其是true还是false
如果是false,就停止循环
如果是true,就执行循环
3.执行循环体语句
4.执行条件控制语句
5.回到2继续执行
1.1.3 for循环的执行流程图
1.1.4 案例代码
package day03;
/*
* for循環語句的格式:
* for(初始化語句;判斷條件語句;控制條件語句)
* 循環體語句;
* 執行流程:1.初始化語句
* 2.執行判斷條件語句,看其結果是true還是false
* 如果是false,就結束循環
* 如果是true,就繼續執行
* 3.執行循環體語句
* 4.執行控制條件語句
* 5.回到2繼續執行
*
* */
public class ForTest01 {
public static void main(String[] args) {
System.out.println("Hello,world!");
System.out.println("Hello,world!");
System.out.println("Hello,world!");
System.out.println("Hello,world!");
System.out.println("Hello,world!");
System.out.println("--------------");
//用for循環改進
for(int x = 0;x<= 5;x++){
System.out.println("Hello,world");
}
}
}
1.1.5 for循环练习之获取1-5和5-1
package day03;
/*
*
* 需求:获取数据 1-5 和 5-1
*
* */
public class ForTest02 {
public static void main(String[] args) {
for(int i = 1;i<= 5;i++)
{
System.out.println(i);
// for(int x = 5;x>=1;x--)
// {
// System.out.println(x);
// }
}
}
}
1.2 while循环的概述与使用
1.2.1循环语句的基本格式
基本格式
while(判断条件语句) {
循环体语句;
}
扩展格式
初始化语句;
while(判断条件语句) {
循环体语句;
控制条件语句;
}
1.2.2 while循环的执行流程图
与for循环一致
1.2.3 案例代码
package day03;
/*
* While循环格式:初始化语句;
* while(条件判断语句){
* 循环体语句;
* 控制条件语句;
* }
*
* for循环格式复习:(初始化语句;条件判断语句;控制条件语句){
* 循环体语句;
* }
*
*
* */
public class MyWhileTest01 {
public static void main(String[] args) {
for(int i = 1;i<=5;i++){
System.out.println("hello,world!");
}
System.out.println("---------------");
int i = 1;
while(i<=5){
System.out.println("hello,world");
i++;
}
}
}
1.2.4 while循环的练习之求1-100之和
package day03;
/*
* 用while循环求1-100之间的数据
*
*
*
* */
public class MyWhileTest02 {
public static void main(String[] args) {
// int sum = 0;
// for(int i = 1;i<=100;i++){
// sum += i;
// }
// System.out.println("sum:" +sum);
//定义求和变量
int sum = 0;
int i = 1;
while(i<=100){
sum +=i;//sum = sum + i;
i++;
}
System.out.println("sum:" + sum);
}
}
1.3 dowhile循环的概述与使用
1.3.1 dowhile循环语句格式
基本格式
do {
循环体语句;
}while((判断条件语句);
扩展格式
初始化语句;
do {
循环体语句;
控制条件语句;
} while((判断条件语句);
1.3.2 执行流程图
1.3.3 案例代码
package day03;
/*
* do..while循环的格式
* do{
* 循环体语句;
* }while(条件判断语句);
*
* 完整格式: 初始化语句;
* do{
* 循环体语句;
* 条件控制语句;
* }while(条件判断语句);
*
*
* 执行流程:1.执行初始化语句
* 2.执行循环体语句
* 3.执行条件控制语句
* 4.执行条件判断语句,看是true还是false
* 如果是true,则继续执行2
* 如果是false,则退出
*
*
* */
public class DoWhileTest {
public static void main(String[] args) {
int i = 1;
do{
System.out.println("hello,world!");
i++;
}while(i<=5);
}
}
1.3.4 三种循环的区别
do…while循环至少会执行一次循环体。
for循环和while循环只有在条件成立的时候才会去执行循环体
for循环语句和while循环语句的小区别:
使用区别:控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继续使用,如果你想继续使用,就用while,否则推荐使用for。
原因是for循环结束,该变量就从内存中消失,能够提高内存的使用效率。
1.3.5 案列代码
package day03;
/*
* 三种循环的区别
* do... while :至少执行一次循环体
* 而for和while的循环语句要先进行条件判断,看是否执行循环体语句
*
* for循环和while循环的小区别
* for循环结束后,初始化变量不能使用了
* 而while循环结束后,初始化变量仍然能使用
*
* 推荐使用顺序:for---while ----do while
* */
public class DoWhileTest01 {
public static void main(String []args){
// int i = 1;
// do{
// System.out.println("我要成为工程师!");
// i++;
// }while(i<4);
//
// for(int a = 1;i<=5;a++){
// System.out.println("我要成为工程师!");
// }
int i = 0;
while(i<5){
System.out.println("我要成为工程师!");
i++;
}
System.out.println(i);
}
}
2.循环嵌套
2.1.1 请输出一个4行5列的星星(*)图案
package day03;
/*
* 需求:请输出一个4行5列的星星(*)图案
*结果: *****
* *****
* *****
* *****
*
* */
public class doubleForTest01 {
public static void main(String[] args) {
//原始做法
// System.out.println("*****");
// System.out.println("*****");
// System.out.println("*****");
// System.out.println("*****");
//用循环改进代码
//第一行上的5颗星
// for(int i = 1;i<= 5;i++){
// System.out.print("*");
//
// }
// System.out.println();
// //第二行上的5颗星
// for(int i = 1;i<= 5;i++){
// System.out.print("*");
// }
//
// System.out.println();
// //第三行上的5颗星
// for(int i = 1;i<= 5;i++){
// System.out.print("*");
// }
// System.out.println();
// //第四行上的5颗星
// for(int i = 1;i<= 5;i++){
// System.out.print("*");
//
}
// System.out.println();
for(int y = 1;y<=4;y++){ //外循环控制行
for(int i = 1;i<= 5;i++){ //内循环控制列
System.out.print("*");
}
System.out.println();
}
}
}
2.1.2 循环嵌套练习之打印正三角形
package day03;
/*
*
* 需求:输出如下图形
* *
* **
* ***
* ****
* *****
*
*
* */
public class DoubleForTest03 {
public static void main(String[] args) {
int z = 1;
// for(int x = 1;x<=5;x++){
// for(int y = 1;y<=z;y++){
// System.out.print("*");
// }
// System.out.println();
// z++;
//
// //分析
// //第一行 y = 1;y<=1
// //第二行 y = 2;y<=2
// //第三行 y = 3;y<=3
// //第四行 y = 4;y<=4
// //第五行 y = 5;y<=5
// //我们需要的变量变化是从1开始,到5结束
//
//
// }
//通过观察我们发现 x的值与z的值变化一样
for(int x = 1;x<=5;x++){
for(int y = 1;y<=x;y++){
System.out.print("*");
}
System.out.println();
}
}
}
2.1.3 循环嵌套练习之打印九九乘法表
package day03;
/*
* 需求:在控制台打印输出九九乘法表
*
*
* \t:转义字符,表示一个tab键
**/
public class DoubleForTest04 {
public static void main(String[] args) {
for(int x = 1;x<=9;x++){
for(int y = 1;y<=x;y++){
System.out.print(y+"*"+x+"="+y*x+"\t");
}
System.out.println();
}
}
}
3.控制循环语句
3.1 跳转控制语句break的概述和使用
在选择结构switch语句中
在循环语句中
离开使用场景的存在是没有意义的
break的作用:
跳出单层循环
跳出多层循环
– 带标签的跳出
– 格式:
标签名:循环语句
标签名要符合Java的命名规范
案例代码
package day03;
/*
* break:中断的意思
* 使用场景: Switch语句中,用于结束Switch语句
* 循环语句中,用于结束循环
*
*
*
* */
public class MyBreakTest {
public static void main(String[] args) {
for(int x= 1;x<=5;x++){
if(x==3){
// break;
}
System.out.println("hello,world");
}
}
}
3.2 跳转控制语句continue的概述和使用
3.2.1continue的使用场景
在循环语句中
离开使用场景的存在是没有意义的
continue的作用:
单层循环对比break,然后总结两个的区别
break 退出当前循环
continue 退出本次循环
3.2.2 案例代码
package day03;
/*
* continue:继续的意思
*
*
* continue和break的区别
* continue:跳出这一次循环,进入下一次的执行
* break:跳出整个循环
*
*
*
* */
public class ContinueTest {
public static void main(String[] args) {
for(int x=1;x<=5;x++){
if(x == 3){
continue;
}
System.out.println("hello,world"+x);
}
}
}
4. Random随机数
4.1 Random的概述和使用
4.1.1 Random的使用步骤
作用:
用于产生一个随机数
使用步骤(和Scanner类似)
导包
import java.util.Random
创建对象
Random r = new Random()
获取随机数
int number = r.nextInt(10);
产生的数据在0到10之间,包括0,不包括10
括号里面的10是可以变化的,如果是100,就是0-100之间的数据
4.1.2 案例代码
package day03;
/*
* Random:用于产生随机数的类。与Scanner类似
* 使用步骤:
* 1.导包 import java.util.Random;
* 2.创建对象 Random r = new Random();
* 3.获取随机数 int number = r.nextInt(10);
* 获取的随机数范围【0,10)包括0 ,不包括10
*
*
* */
import java.util.Random;
public class RandomTest01 {
public static void main(String[] args) {
//创建对象
Random r = new Random();
for(int x=1;x<=10;x++){
//获取随机数
int number = r.nextInt(10);
System.out.println("number:" + number);
System.out.println("-----------------");
//如何获取1-100之间的随机数呢?
int i = r.nextInt(100) + 1;
System.out.println(i);
}
}
}
4.2 猜数字小游戏案例:
4.2.1案例代码
package day03;
/*
* 需求:猜数字小游戏
* 系统产生1-100之间的随机数,猜猜这个数据是多少?
* 分析:1.系统产生一个 1-100之间的随机数
* Random r = new Random();
* int number = r.nextInt(100)+ 1;
* 2.键盘录入我们要猜的数据
* 3.比较这两个数据,看我们猜的是否正确
* 如果大了,提示:你猜的数据大了
* 如果小了,提示:你猜的数据小了
* 如果相等,提示:恭喜你,猜中了
* 4.为了实现多次猜数据,我们使用循环
* while(true){...}
* for(;;){...}
*
* */
import java.util.Random;
import java.util.Scanner;
public class RandomTest02 {
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("请输入你要猜的数据(1-100):");
int guessNumber = sc.nextInt();
//比较两个数据
if (guessNumber> number) {
System.out.println("你输入的数据" + guessNumber + "大了");
} else if(guessNumber < number) {
System.out.println("你输入的数据" + guessNumber + "小了");
} else {
System.out.println("恭喜你猜中了");
break;
}
}
}
}