文章目录
JAVA循环循环小结:
JAVA函数
JAVA循环
for循环的结构
for(初始化表达语句
;判断条件语句
;控制条件语句
){
循环体语句
;
}
for 循环(这个循环的写法很类似于C语言的写法)
for (int i
=0;i
<10;i
++){
System
.out
.println("祖国生日快乐!!!");
}
break 跳出for循环(普通break跳出离break最近的循环语句,带标记的break跳出的位置则依据标记值而定,即:可跳出嵌套循环!)
public class ForDemo {
public static void main(String
[] args
) {
x1
:for(int i
=0; i
<=7 ; i
++){
System
.out
.println("i="+i
);
x2
:for(int j
=0 ; j
<= 10; j
++){
System
.out
.println("j="+j
);
if (j
==2){
break x1
;
}
}
}
}
}
输出:
i
=0
j
=0
j
=1
j
=2
while 循环(从1打印到5!)
public class WhileDemo {
public static void main(String
[] args
) {
int i
= 0;
while( i
<5){
i
+= 1;
System
.out
.println(i
);
}
}
}
-------------------
结果:
1
2
3
4
5
do-while 循环结构:
初始化条件语句
;
do{
循环体语句
;
控制条件语句
;
}while(判断条件语句
);
------------------------
执行流程:
1. 执行初始化条件语句
2. 执行循环体语句
3. 执行控制条件语句
4. 执行判断条件语句,如果为
true 则继续跳到步骤
2执行,否则直接退出循环。
示例:
public class WhileDemo {
public static void main(String
[] args
) {
int i
= 10;
do{
System
.out
.println("i的数值是:"+i
);
i
-= 1;
}while (i
<5);
}
}
--------------------------
输出:
i的数值是:
10
break 同样可以跳出while循环和do-while循环无限循环:
while(true){
.........................
}
----------------------------
for(;;){
.....................
}
判断100~999内的水仙花数((所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。举例:153就是一个水仙花数。153 = 1×1×1 + 5×5×5 + 3×3×3 = 1 + 125 + 27 = 153)重点是如何取得一个数字的每一位数字-------------> 每次取得个位数)
public class ShuiXianHua {
public static void main(String
[] args
) {
for (int i
=100;i
<=999;i
++){
boolean result
= getBoolResult(i
);
if (result
){
System
.out
.println("数字"+i
+"是水仙花数!");
}
}
}
private static boolean getBoolResult(int i
) {
int x1
= i
/1 % 10;
int x2
= i
/10 % 10;
int x3
= i
/100 % 10;
if ((int)(Math
.pow(x1
,3)+Math
.pow(x2
,3)+Math
.pow(x3
,3)) == i
){
return true;
}
return false;
}
}
-------------------------
输出:
数字
153是水仙花数!
数字
370是水仙花数!
数字
371是水仙花数!
数字
407是水仙花数!
打印N*N的乘法口诀表:
public class MultiplationDemo {
public static void main(String
[] args
) {
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("请输入你要打印的N*N的乘法口诀表中的值N:");
int n
= scanner
.nextInt();
for (int i
=1;i
<=n
;i
++){
for (int j
=1; j
<=i
;j
++){
System
.out
.print(i
+"*"+j
+"="+(j
*i
)+" ");
}
System
.out
.println();
}
}
}
-------------------------
输出:
请输入你要打印的N
*N的乘法口诀表中的值N
:
9
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
打印菱形
public class PrintLingxing {
public static void main(String
[] args
) {
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("请输入您要打印的◇的长度:");
int num
= scanner
.nextInt();
Print(num
);
}
public static void Print(int num
){
for(int row
= (int)(num
/2);row
>0;row
--){
for(int i
=0;i
<num
;i
++){
if(i
==row
|| i
==(num
-row
-1)){
System
.out
.print("*");
}else{
System
.out
.print(" ");
}
}
System
.out
.println();
}
for(int row
=0;row
<=(int)(num
/2);row
++){
for(int i
=0;i
<num
;i
++){
if(i
==row
||i
==(num
-1-row
)){
System
.out
.print("*");
}else{
System
.out
.print(" ");
}
}
System
.out
.println();
}
}
}
--------------------
输出:
请输入您要打印的◇的长度
:
11
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
循环语句之continue: (对while,for,do-while都适用) 意义:跳过这次循环(在循环体内continue之后的语句不执行),直接执行下一次循环操作(从循环体的头部开始执行)
循环小结:
do…while循环至少执行一次,而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句事先知道循环要执行多少次的情况下,最好用for循环建议优先考虑for循环,然后是while循环,最后是do…while循环
JAVA函数
目的:避免大量的冗余代码,这些代码的逻辑都是一样的,返回的是同一个逻辑运算后的结果。定义:完成特定功能的模块。(也叫函数!)格式: 修饰符 返回值类型 方法名
(参数类型 参数名
1,参数类型 参数名
2 ....){
方法体
;
return 返回值
;
}
方法的格式详细说明: (1): 修饰符 比较多,后面会详细介绍。目前使用 public static (2): 返回值类型 用于限定返回值的数据类型 (3): 方法名 就是一个名称,它的存在是为了方便我们调用方法 (4): 参数类型 限定调用方法时传入参数的数据类型 (5): 参数名 是一个变量,接收调用方法时传入的参数,这个参数其实有一个专业的名词,被称之为形式参数,它的作用是用来接收实际参数的. (6): 方法体 完成功能的代码 (7): return 结束方法以及返回方法指定类型的值 (8): 返回值 就是功能的结果,由return带回,带回给调用者如何写一个方法:(两个明确) (1): 返回值类型 明确功能结果的类型 (2): 参数列表: 要传递几个参数,以及每一个参数的类型方法调用(有明确返回值的调用) (1): 单独调用,一般来说没有意义,所以不推荐 (2): 赋值调用,推荐方案。 (3): 输出调用,但是不够好。因为我们可能需要对结果进行进一步操作。方法的注意事项: A: 方法不调用不执行 B: 方法与方法是平级关系,不能嵌套定义 C: 方法定义的时候参数之间用逗号隔开 D: 方法调用的时候不用在传递数据类型 E: 如果方法有明确的返回值,一定要由return带回一个值JAVA函数传递形参和变量的时候方法不一样? 回答:(一样!)不能按照位置传参数吗? 回答:(只能按照位置传参,不能按照名字传参)JAVA中的函数传参示例(位置传参):
public class FuncitonDemo {
public static void main(String
[] args
) {
System
.out
.println(f1(1,2));
}
public static int f1(int x1
, int x2
){
return (x1
+x2
);
}
}
结论:JAVA中好像只能位置传参,不能名字传参。(这一点和Python不同)
double 赋给 long 会出现什么效果?(编译器无法通过!)
public class FunctionReload {
public static void main(String
[] args
) {
long x
= add1(1,1.2);
long x
= add1(1,1);
}
private static int add1(int i
, int i1
) {
System
.out
.println("第一个方法!");
return i
+i1
;
}
private static double add1(int i
, double i1
) {
System
.out
.println("第二个方法!");
return i
+i1
;
}
}
在IDEA中万能纠错键"Alt+Enter"是可以用来快速创建方法(即:先在调用的地方写调用函数,并将调用函数的形参也同样写好,此时利用Alt+Enter便可以自动创建对应的函数,并且已经定义好了形参的类型(与调用函数相对应))
JAVA中类的方法重载(这个Python中也没有)
public class FunctionReload {
public static void main(String
[] args
) {
System
.out
.println(add1(1,2.9));
System
.out
.println(add1(1,1));
}
private static int add1(int i
, int i1
) {
System
.out
.println("第一个方法!");
return i
+i1
;
}
private static double add1(int i
, double i1
) {
System
.out
.println("第二个方法!");
return i
+i1
;
}
}
结论:在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同(参数个数或者是参数类型不同),重载的判定方法与返回值的类型无关。