1.for循环
1.练习:打印1-100之内数之和
package cn.tedu.basic;
/**本类用于for循环巩固练习*/
//需求:打印1-100之内数之和
public class TestFor1 {
public static void main(String[] args) {
//1.定义变量保存求和的结果
int sum = 0;
//2.创建循环体,列出0-100之内的数
for(int i = 1; i < 101; i++) {
//3.将本轮循环到的数字,累加到sum
sum += i;
}
//4.打印最终累加结果
System.out.println(sum);
}
}
2.练习:求出1-100所有偶数之和
package cn.tedu.basic;
/**本类用于for循环巩固练习*/
//需求:求出1-100所有偶数之和
public class TestFor2 {
public static void main(String[] args) {
//1.定义保存结果的变量
int sum = 0;
//2.创建循环体,一次循环1-100之内数
// for(int i = 2; i < 101; i=i+2) {
// sum += i;
// }
for(int i = 1; i < 101; i++) {
//3.过滤出需要累加的数
if(i % 2 == 0) {
//4.能进来这个大括号,才说明是偶数,才能累加
sum += i;
}
}
//5.打印求和结果
System.out.println(sum);
}
}
3.练习:求出1-100以内奇数个数
package cn.tedu.basic;
/**本类用于for循环巩固练习*/
//需求:求出1-100以内奇数个数
public class Testfor3 {
public static void main(String[] args) {
//1.定义变量保存统计个数
int count = 0;
//2.创建循环,依次循环1-100的
for(int i = 1; i < 101; i++){
//3.判断过滤出所有的奇数
if(i % 2 != 0) {//不能整除2,说明是奇数
//4.出现一个奇数,count加1
count++;
}
}
//5.打印统计个数
System.out.println(count);
}
}
4.打印一个三行五列的矩形
package cn.tedu.basic2;
/**本类用于测试嵌套for循环
* 总结1:外层循环执行一次,内层循环执行多次
* 总结2:外层循环控制图形的行数,内层循环控制图形的列数
* */
public class TestForDemo {
public static void main(String[] args) {
//需求1:外层打印三次,内层打印5次
//1.打印1-3
for(int i = 1;i < 4; i++) {
System.out.println("外层"+i);
//2.打印1-5
for(int j = 1;j < 6; j++) {
System.out.println(j);
}
}
//需求2:打印一个三行五列的矩形
//*****
//*****
//*****
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 5; j++) {
System.out.print("*");
}
System.out.println();//打印空白行来换行
}
}
}
5.打印左直角三角形
package cn.tedu.basic2;
/**需求:利用嵌套for循环,打印左直角三角形*/
//行1星1 *
//行2星2 **
//行3星3 ***
//行4星4 ****
//行5星5 *****
//行i星i *i
public class TestForTriangle {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
6.打印99乘法表
package cn.tedu.basic2;
/**本类用于测试完成99乘法表*/
//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
public class TestFor99Excel {
public static void main(String[] args) {
for(int i = 1; i <= 9; i++) {//外层循环9次,也就是9行
for(int j = 1; j <= i; j++) {//内层循环9次,也就是9列
/**t代表table表格的意思,为了转义字母t的含义,需要用转义符号\
* 所以“\t”代表的师一个制表符*/
System.out.print(j+"*"+i+"="+(j*i)+"\t");
// System.out.print("j*i="+(j*i)+"\t");// \t为制表符
}
System.out.println();//空白行
}
}
}
2.变量
1.局部变量
位置:定义在方法里或者局部代码块中
注意:必须手动初始化来分配内存.如:int i = 5;或者int i; i = 5;
作用域:也就是方法里或者局部代码块中,方法运行完内存就释放了
2. 成员变量
位置:定义在类里方法外
注意:不用初始化,也会自动被初始化成默认值
作用域:整个类中,类消失了,变量才会释放
3.练习:各种类型变量的默认值
package cn.tedu.basic;
/**本类用于测试各种类型变量的默认值*/
public class TestVariable1 {
static String name;//null是引类型的默认值
static byte a;//整型数据的默认值是0
static short b;
static int c;
static long d;
static float e;//浮点型数据的默认值是0.0
static double f;
static char g;//字符类型数据的默认值是\u0000(是一个空格)
static boolean h;//布尔类型数据默认值是false
public static void main(String[] args) {
System.out.println(name);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
}
}
4.练习:测试成员变量与局部变量
package cn.tedu.basic;
/**本类用于测试成员变量与局部变量*/
public class TestVariable2 {
//2.创建成员变量
/** 1.位置:类里方法外
* 2.注意事项:无需手动初始化,会赋予对应类型成员变量的默认值
* 3.作用域:在整个类中都生效,类消失,成员变量才会消失
*/
static int count;
public static void main(String[] args) {
//1.创建局部变量:
/**
* 1.位置:在方法里/局部代码块中
* 2.注意事项:必须手动初始化--给变量赋值
* 3.作用域:在方法里/局部代码块中,当对应的代码执行完毕,局部变量也随之释放
*/
int sum = 100;
System.out.println(sum);
System.out.println(count);
for(int i = 1;i < 11; i++) {
System.out.println(i);
}
// System.out.println(i);//报错,无法引用变量i,因为i是一个局部变量
}
}
3.方法
1.定义格式
2.方法调用顺序图
顺序执行代码,调用指定方法,执行完毕,返回调用位置
3.方法的入门案例
package cn.tedu.method;
/**本类用于方法的入门案例*/
public class TestMethod {
public static void main(String[] args) {
System.out.println(1);
/**2.我们通过方法名+参数列表的方式来确定具体调用哪个方法的功能*/
method1();//调用method1方法
System.out.println(2);
method2(3);
}
private static void method2(int i) {
System.out.println("海绵宝宝今年"+i+"周年了");
}
/**1.方法的修饰符 方法的返回值类型 方法名(参数列表){方法体}*/
public static void method1() {
System.out.println(5);
System.out.println(6);
System.out.println(7);
}
}
4.测试方法的重载现象
package cn.tedu.method;
/**本类用于测试方法的重载现象
* 在同一个类中,存在多个方法名相同,但参数列表不同的方法
* 如果在同类中,同名方法的参数个数不同,一定构成重载
* 如果在同类中,同名方法的参数个数相同,
* 需要查看对应位置上的参数的类型,而不是参数名,与参数名无关
* (int a,String b)与 (int b,String a)--不构成重载
* (int a,String b)与 (String a,int b)--构成重载
* */
public class MethodOverload {
public static void main(String[] args) {
method();
method(2);
method(888,"泡泡");
method("泡泡",8);
}
//1.创建一个无参的method()
public static void method() {
System.out.println("我没有参数");
}
//2.创建一个含有int类型参数的method()
public static void method(int n) {
System.out.println("我的参数是:"+n);
}
//3.创建一个含有int,String类型参数的method()
public static void method(int a,String b) {
System.out.println("我的参数是:"+a+",我叫"+b);
}
//这个不构成重载,会报错,与参数的名字无关,看参数类型
//public static void method(int b,String a){}
public static void method(String a,int b) {
System.out.println("我是干饭王"+a+",我"+b+"岁了");
}
}