赞
踩
一.do while循环
1.结构:
do{
循环体
}while(判断条件);
do while循环的特点是不管判断条件是否成立先执行一遍循环体.
public class Demo01{
public static void main(String[] args){
//使用do while循环打印十遍hello;
int num = 0;
do{
System.out.println("hello");
num++;
}while(num < 10)
}
2.练习:
/* 从星期1开始打印 并询问明天上班不
* 输入上班 继续打印
* 输入不上班 停止程序
*/
int num1 = 1;
Scanner num = new Scanner(System.in);
String str;
do{
System.out.println("星期" + num1);
System.out.println("请输入要不要上班");
str =num.nextLine();
num1++;
}while(str.equals("上班") && num1 <= 7)
}

3.break在while循环中的作用
int num = 0;
while(num < 10){
if(num == 5){
break;//break会结束当前的循环
}
System.out.println("number");
number++;
}
//输出:
{
number
number
number
number
number
}

二.for循环
1.结构:
for(声明循环增量;判断条件;自增/自减){
循环体
}
1.打印十遍helloworld
public class Demo02 {
public static void main(String[] args) {
for(int i= 0; i< 10; i++){
//注意:循环增量i只能在for循环的作用域内使用
System.out.println("hello world");
}
}
}
2.计算1-10的和
public class Demo02 {
public static void main(String[] args) {
int sum = 0
for(int i= 1; i<= 10; i++){
sum = sum + i;
}
System.out.println(sum);
}
}
3.使用for循环 1-100中,是7的倍数 的个数 打印个数
public class Demo02 {
public static void main(String[] args) {
int num = 0
for(int i= 1; i<= 100; i++){
if(i % 7 == 0){
num++;
}
}
System.out.println(num);
}
}
三.双重循环
1.结构:
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
}
}
2.利用双重循环打印正三角形
//外循环控制打印的行数
//里循环中,循环增量不变,改变判断条件中的值
public class Demo02 {
public static void main(String[] args) {
//打印三行
for(int i= 0; i< 3; i++){
for(int j = 0; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
//输出:
{
*
**
***
}

3.利用双重循环打印倒三角形
//外循环控制打印的行数
//里循环中,循环增量改变,判断条件中的值不变
public class Demo02 {
public static void main(String[] args) {
//打印三行
for(int i= 0; i< 3; i++){
for(int j = i; j < 3; j++){
System.out.print("*");
}
System.out.println();
}
}
}
//输出:
{
***
**
*
}

4.打印乘法口诀表
public class Demo02 {
public static void main(String[] args) {
//打印三行
for(int i= 0; i< 9; i++){
for(int j = 0; j < i; j++){
System.out.print(j +"*"+ i +"="+ i * j + "\t");
}
System.out.println();
}
}
}
//输出:
{
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
}

四.for循环中的break和continue
break:结束当前的循环
continue:结束本次循环,继续下一次循环
1.效果:
public class Demo02 {
public static void main(String[] args) {
for(int i= 0; i< 10; i++){
if(i == 5){
break;
}
System.out.println(i);
//输出:
{
0
1
2
3
4
}
}
for(int i= 0; i< 10; i++){
if(i == 5){
continue;
}
System.out.println(i);
//输出:
{
0 1 2 3 4 6 7 8 9
}
}
}
}

2.打印[0, 100]中,是7的倍数中的值最大那个数(两种方法一种用break)
//方法1:
public class Demo02 {
public static void main(String[] args) {
int num = 0;
for(int i= 0; i<= 100; i++){
if(i % 7 == 0){
num = i;
}
}
System.out.println(num1);
}
}
//输出:
{
98
}
//方法2:
public class Demo02 {
public static void main(String[] args) {
int num = 0;
for(int i= 100; i > 0; i--){
if(i % 7 == 0){
num = i;
break;
}
}
System.out.println(num);
}
}
//输出:
{
98
}

3.班级13个人 跟每个人打招呼(打印几号你好),讨厌的人不打招呼,我分别讨厌 3号 5号 9号
public class public001 {
public static void main(String[] args) {
for(int i = 1; i <=13; i++){
if(i == 3 || i == 5 || i == 9){
continue;
}
System.out.println(i + "号你好!");
}
}
}
4.for循环的死循环
for (; ; ) {
System.out.println("死循环");
}
注意:怎样判断使用哪个循环语句
如果有明确的停止条件就使用while循环,如果是数组一般使用for循环
for循环的死循环,一般使用死循环都使用while循环
while(true){
System.out.println("死循环");
}
五.随机数
1.随机数(系统提供的Math类)
random()方法,取值范围:[0,1)大于等于0小于1
伪随机:计算机在取随机数的的时候都是按照一定的规则去随机
2.随机一个[0-100]之间的整数
public class Demo02 {
public static void main(String[] args) {
double num = Math.random();
System.out.println(num);//随机一个0-1之间的数
//公式:Math.random() * (max - min + 1) + min
int num1 = (int)(Math.random() * (100 + 1));
System.out.println(num1);
}
}
3.随机[15,200]的整数 10个 并找出最大值
public class Demo02 {
public static void main(String[] args) {
int max = 0;
for (int i = 0; i < 10; i++) {
int num2 = (int)(Math.random() * (200 - 15 + 1) + 15);
System.out.println(num2);
if (num2 > max) {
max = num2;
}
System.out.println(max);
}
}
}
4.猜数字
public class Demo02 {
public static void main(String[] args) {
int num3 = (int)(Math.random() * (100 + 1));
System.out.println(num3);
System.out.println("请输入一个数");
Scanner str = new Scanner(System.in);
//方法1:
for (int i = 0; i < 100; i++) {
int putNum = str.nextInt();
if (putNum > num3) {
System.out.println("猜大了");
}else if (putNum < num3) {
System.out.println("猜小了");
}else {
System.out.println("恭喜你猜对了");
break;
}
}
//方法2:
while (true) {
int putNum = str.nextInt();
if (putNum > num3) {
System.out.println("猜大了");
}else if (putNum < num3) {
System.out.println("猜小了");
}else {
System.out.println("恭喜你猜对了");
break;
}
}
}
}

六.函数
1.定义:
函数:分装了特定功能的代码块
函数的好处:增加代码的重复使用率,提高效率
2.函数的写法:
关键字 返回值类型 函数名(参数类型1 参数名1,参数类型2 参数名2…){
函数体(执行代码)
return 返回值;(注意:返回值和函数声明上的类型保持一致)
函数可以没有返回值,这时函数声明上的返回值类型 使用void表示
}
注意:
函数名使用小驼峰写法
书写位置: 在类中书写
函数中不能再定义函数
函数可以命名相同的函数名(此现象称为函数的重载:函数的功能相同但是代码实现不同,可以使用相同的函数名)
函数的重载只跟参数的数量,类型,顺序有关 跟参数名和返回值类型无关
public class Demo02 {
public static void main(String[] args) {
//调用打印矩形的函数,直接用 函数名(传入的参数)调用
square(5,10);
System.out.println(sum(10, 20));
}
1.打印三行八列的矩形函数
//通过添加参数来扩展函数的功能
//打印row行,line列
public static void square(int row,int line) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < line; j++) {
System.out.print("*");
}
System.out.println();
}
}
2.计算两个数的和的值的函数
public static int sum(int x,int y) {
int sumNumber = x + y;
return sumNumber;
}
3. 需求: 定义函数
1.计算2个数的最大值
public static int max(int a,int b) {
return a > b ? a : b;
}
2.计算3个数的最大值
public static int max(int a,int b,int c) {
int num max(a,b);//调用2个数最大值的函数
return num > c ? num : c;
}
3.计算4个数的最大值
public static int max(int a,int b,int c,int d) {
int num = max(a, b, c);
return num > c ? num : c;
}
4.计算5个数的最大值
public static int max(int a,int b,int c,int d,int e) {
int num = max(a,b,c,d);
return num > e ? num : e;
}
}

七.递归函数
1.定义:在函数内部调用自身称为递归
public class Demo02 {
1.计算4的阶乘1*2*3*4
//首先看参数和返回值
//注意递归函数必须提供函数的出口,否则会出现死循环
public static int fn(int n) {
if (n == 1) {
//结束递归
return 1;
}
return n * fn(n - 1);
}
public static void main(String[] args) {
System.out.println(fn(5));
2.获取字符串当中的字符
String str = "hello";
char a = str.charAt(1);//根据角标获取字符
System.out.println(a);
}
}

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。