1.循环嵌套
理解:在循环中嵌套一层或者多层循环
例如:打印出简单的三行四列*图形;
public class Demo1 {
public static void main(String[] args) {
//用循环嵌套打印一个简单的图形
for (int i = 0; i < 3; i++) {//外层循环开始
for (int j = 0; j < 4; j++) {//内层循环
System.out.print("*");
}//内层循环开始
System.out.println( );//打印完一行后换行
}
}
}
输出:
"D:\Program Files (x86)\Java\bin\java.exe" "
****
****
****
Process finished with exit code 0
打印99乘法表:
public class Demo2 {
public static void main(String[] args) {
//打印99乘法表
for (int i = 1; i <= 9; i++) {//外层循环开始
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+j*i+"\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
2.方法定义
方法:
用来完成某些特定功能的代码组合;
形式:
修饰符 — 返回值类型— 方法名–(参数类型 参数名)
{
方法体
return 返回值;
}
修饰符定义了该方法的访问类型;
返回值类型不能和参数类型产生矛盾;定义的返回值类型和实际的返回值类型要匹配,即遵守数据类型的转换规则;
方法的调用:
在创建方法时,如果被static修饰;那么该方法即属于类级别,可直接在主方法中调用;
例如:
public class FangFa002 {
public static void main(String[] args) {
//开始调用方法
int max=max(50,80);
System.out.println(max);
}
//创建比较大小的方法
public static int max(int a,int b){
int result=0;
//定义一个返回值
if(a==b){
System.out.println("a=b");
return 0;//终止方法
}
if(a>b){
result=a;
}else{
result=b;
}
return result;
}
}
没有被static修饰;那么该方法即属于对象级别;在调用该方法时,就需要先创建该方法所在类的对象;
例如:
public class Method01 {
public static void main(String[] args) {
//调用方法,创建方法所在的类的对象
Method01 method01=new Method01();
method01.exercise(20,16);
}
public void exercise(int a, int b){
//当b不为0,进入循环
while(b!=0){
//将a%b赋值给temp
int temp=a%b;
//把b赋值给a
a=b;
//把temp赋值给b
b=temp;
}
System.out.println("最大公约数为"+a);
}
}
3.java数组
定义
一组相同数据类型的数据组合;数组也是对象;长度给定后就不能变;如果越界就会报错:
ArrayIndexOutOfBoundsException;
数组的下标是从0开始的;例如存了10个数据,最后的数字下标就是9;
声明数组:
//数组的声明
int[] a;//常用的一种声明用法;
int num[];//另一种声明用法;
//其中的int是数组中可以存储的数据类型
数组的创建方式
静态初始化
在创建时,就直接给数组中的元素直接赋值;
例如 int [ ] a={1,2,3,4};
动态初始化
在创建时,给了数组长度,没有给数组中的元素赋值;
例如: int [ ] a=new int [3];
当没有给定数组中元素时,不同的数据类型会有不同的默认值;
整数默认值: 0
浮点数默认值: 0.0
char默认值: 空格
引用类型默认值: null
布尔型默认值:false
public class ArrayDemo01 {
public static void main(String[] args) {
//int[] a;
//String[] b;
int[] c=new int[3];
c[0]=1;
c[1]=2;
c[2]=3;
//数组下标由0开始计数
System.out.println(c[2]);
//用Arrays.toString将数组对象以字符串的形式输出
System.out.println(Arrays.toString(c));
String[] d=new String[]{"q","w","e","r"};
System.out.println(Arrays.toString(d));
System.out.println("==================");
//boolean默认输出
boolean[] f=new boolean[5];
System.out.println(Arrays.toString(f));
System.out.println("==================");
//char默认输出
char[] g=new char[5];
System.out.println(Arrays.toString(g));
System.out.println("==================");
//double默认输出
double[] h=new double[5];
System.out.println(Arrays.toString(h));
System.out.println("==================");
//int默认输出
int[] j=new int[5];
System.out.println(Arrays.toString(j));
}
}
输出:
3
[1, 2, 3]
[q, w, e, r]
==================
[false, false, false, false, false]
==================
[ , , , , ]
==================
[0.0, 0.0, 0.0, 0.0, 0.0]
==================
[0, 0, 0, 0, 0]
数组元素的访问
可直接在数组进行通过索引进行访问,每个元素对应自己的位置编号,
数组的下标是从0开始的;例如存了10个数据,最后的数字下标就是9;
如果越界就会输出报错:ArrayIndexOutOfBoundsException;
public class Array002 {
public static void main(String[] args) {
int[] a=new int[4];
a[0]=2;
a[1]=3;
a[2]=6;
a[3]=8;
//a[4]=9;//此时已经超出数组长度,会越界报错
System.out.println(a[3]);//查询第四个数据
}
}
数组遍历
public class Array005 {
public static void main(String[] args) {
//数组遍历
//第一种:for循环;
int[] a={1,2,5,6,8};
//a.length 数组长度
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
System.out.println("=======分割线============");
//第二种:增强的for循环;适用于遍历数组
//声明一个变量,接收每次从数组输出的元素
//循环一次就在数组中取出一个元素;赋值给定义的变量;直到全部取出
int[] b={13,5,9,8};
for(int t:b){
System.out.println(t);
}
}
}
4.数组排序
4.1冒泡排序
每次取出相邻的两个元素进项比较;较大向后移动,直到将所有的元素全部比较完成;
例如:
public class ArrayDemo04 {
//冒泡排序练习
public static void main(String[] args) {
int[] a={10,4,3,2,2,1};
//4,3,2,2,1,10
//3,2,2,1,4,10
//2,2,1,3,4,10
//1,2,2,3,4,10
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length-1-i; j++) {
//相邻的两个元素进行比较
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
[1, 2, 2, 3, 4, 10]
本文详细介绍了Java编程中的循环嵌套,包括如何使用循环打印星形图案和99乘法表。接着讲解了方法的定义与调用,包括静态与非静态方法的区别。此外,还探讨了Java数组的声明、创建、访问和遍历。最后,以冒泡排序为例展示了数组排序的实现过程。内容涵盖了基础编程概念和技巧,适合初学者学习。

2687

被折叠的 条评论
为什么被折叠?



