20210812学习日志
循环结构
while循环
package com.li.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i = 0;
while(i<100){
System.out.println(++i);
}
}
}
让while后面的布尔表达式失效,使得程序停下来。
package com.li.struct;
//计算1+2+3+...+100
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+...+100
int i = 0;
int sum = 0;
while(i<100){
i++;
sum += i;
System.out.println("ans+"+i+"="+sum);
}
}
}
do…while循环
至少执行一次
package com.li.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i =0;
int sum = 0;
do {
i++;
sum += i;
}while (i<100);
System.out.println(sum);
}
}
for循环
for循环是最有效,最灵活的循环结构。
在IDEA中可以用快捷键 100.for生成快速的for结构。
package com.li.struct;
public class ForDemo02 {
public static void main(String[] args) {
//计算0-100之间技术和偶数的和
int oddsum = 0;
int evensum = 0;
for (int i = 0; i < 100; i++) {
if (i%2!=0){
oddsum +=i;
}else {
evensum += i;
}
}
System.out.println("奇数的和:" + oddsum);
System.out.println("偶数的和: "+ evensum);
}
}
package com.li.struct;
public class ForDemo03 {
public static void main(String[] args) {
//输出1-1000之间能被5整除的数,并且每输出3个数换行
for (int i = 1; i <= 1000; i++) {
if (i%5 == 0){
System.out.print(i + "\t");
}
if (i%15 ==0){
System.out.print("\n");
}
}
//println 输出就会换行
//print 输出不会换行
}
}
package com.li.struct;
public class ForDemo04 {
public static void main(String[] args) {
//打印99乘法表
for (int i = 1; i <= 9; i++) {
System.out.println();
for (int j = 1; j <= i; j++) {
System.out.print(j+ "*"+i+"="+(i*j)+" ");
}
}
}
}
- 增强for循环
package com.li.struct;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
//遍历数组的元素
for (int x:numbers){
System.out.println(x);
}
}
}
- break和continue
break强行退出循环,continue是跳过一次继续循环。
package com.li.struct;
public class ForDemo06 {
public static void main(String[] args) {
//打印三角形
for (int i = 1; i <= 5; i++) {
for (int j = 5; j>=i; j--){
System.out.print(" ");
}
for (int j = 1; j <= i; j++){
System.out.print("*");
}
for (int j = 1; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
java方法
什么是方法
System.out.println(),是什么?
Java的方法是语句的集合,在一起执行一个功能。
方法的命名规则:小驼峰。
方法在程序中创建,在其他地方被引用。
方法的定义和重载
定义
修饰符 返回值类型 方法名(参数类型 参数名){
方法体
return 返回值;
}
package com.li.method;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a,b = 0;
a = scanner.nextInt();
b = scanner.nextInt();
int m = max(a , b);
System.out.println(m);
scanner.close();
}
//较大值
public static int max(int a, int b){
int res = 0;
if (a > b){
res = a;
}else if (a < b){
res = b;
}else {
System.out.println("两数相等");
return -1; //用来结束
}
return res;
}
}
- 值传递和引用传递
Java是值传递。
- 方法的重载
在一个类中有相同的方法名,但是参数不同。
仅仅返回类型不同不足以成为方法的重载。
package com.li.method;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a,b = 0;
a = scanner.nextDouble();
b = scanner.nextDouble();
double m = max(a , b);
System.out.println(m);
scanner.close();
}
//较大值
public static int max(int a, int b){
int res = 0;
if (a > b){
res = a;
}else if (a < b){
res = b;
}else {
System.out.println("两数相等");
return -1; //用来结束
}
return res;
}
public static double max(double a, double b){
double res = 0;
if (a > b){
res = a;
}else if (a < b){
res = b;
}else {
System.out.println("两数相等");
return -1; //用来结束
}
return res;
}
}
命令行传参
要靠传递命令行参数给main()函数传参
package com.li.method;
public class Demo03 {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "]" + args[i]);
}
}
}
找到包的路径执行
可变参数
在方法的声明中,在指定类型后加一个省略号(…)。
一个方法只能制定一个可变参数,它必须是方法的最后一个参数。任何普通的参数必须在它之前声明。
package com.li.method;
public class Demo04 {
public static void main(String[] args) {
//调用可变参数的方法
printMax(34,3,3,5,46); //传递了数列
printMax(new double[]{1, 2, 3});//传递了数组
}
public static void printMax(double... numbers){
if (numbers.length == 0){
System.out.println("No argument passed");
return;
}
double result = numbers[0];
//sorting
for (int i = 1; i < numbers.length; i++){
if (numbers[i] > result) {
result = numbers[i];
}
}
System.out.println("the max value is " + result);
}
}
递归
- A方法调用A方法,就是递归。
递归结构包括递归头,什么时候不能调用自身。和递归体,什么时候需要调用自身方法