本篇重点讲控制、选择、循环等基础语法以及方法的概念和递归算法。从这里开始,我们正式进入Java的程序世界
47 控制语句介绍
流程控制语句是用来控制程序中各语句执行顺序的语句。(本质)
包含三个主要类别:顺序结构(先…后…)、选择结构(如果…则…)、循环结构(若满足条件则进入循环,否则跳出循环)
任何软件和程序,本质都是变量、选择语句、循环语句组成。
48 if选择结构
- 单选择结构:if
- 双选择结构:if-else
- 多选择结构:if-else if-else
- switch 结构
Math类的介绍:
- 包里面是类,类里面是方法。包-类-方法对应 程序集合-程序-函数。
- java.long包中的Math类提供了一些用于数学计算的方法。
- Math.random()该方法用于产生到1之间的double类型的随机数,但是不包括1。
int i = (int)(6*Math.random()));//产生[0~5]之间的随机整数
package 选择以及循环;
public class 测试if语句 {
public static void main(String[] args) {
double d = Math.random(); //Math类可以直接用
System.out.println(d); //随机出现0到1的double型变量值,[0,1)
System.out.println(6 * Math.random());
System.out.println((int)(6 * Math.random())); //利用int型强制转换,随机出现整数0到5之间的值
System.out.println((int)(6 * Math.random())+1); //利用int型强制转换,加个1随机出现整数1到6之间的值
//上面是对随机数的使用测试
}
}
package 选择以及循环;
//if的单选择结构
public class 掷骰子游戏_if语句 {
public static void main(String[] args) {
//通过掷骰子看今天的运气如何
int i = (int)(6*Math.random()+1);
int j = (int)(6*Math.random()+1);
int k= (int)(6*Math.random()+1);
int count = i+j+k;
if(count > 15) {
System.out.println("今天运气不错:)");
}
if(count >= 10 && count <= 15) {
System.out.println("今天运气一般:|");
}
if(count < 10) {
System.out.println("今天运气不怎么样:(");
}
System.out.println("得了"+ count + "分");
49 if_else 双选择结构
如果…否则…
int h = (int)(6*Math.random()+1);
System.out.println(h);
if(h <= 3){
System.out.println("小");
}else{
System.out.println("大");
}
//随机产生一个[0.0,4.0)的半径,并根据半径求圆的面积和周长
double r = 4 *Math.random();
//Math.pow(r,2)是求半径r的平方,这个函数是求次幂函数,是Math程序(类)中的一个函数
double area = Math.PI * Math.pow(r,2);
double circle = 2*Math.PI*r;
System.out.println("半径为"+ r);
System.out.println("面积为"+ area);
System.out.println("周长为" + circle);
if(area >= circle) {
System.out.println("面积大于等于周长。");
}else {
System.out.println("面积小于周长。");
}
50 if_else if_else 多选择结构
public class 多选择结构 {
public static void main(String[] args) {
int age = (int)(100*Math.random());
System.out.println("年龄是" + age + ",属于");
if(age < 15) {
System.out.println("儿童,喜欢玩!");
}else if(age < 25){
System.out.println("青年,要学习!");
}else if(age < 45) {
System.out.println("中年,要工作!");
}else if(age < 65) {
System.out.println("中老年,要补钙!");
}else if(age < 85) {
System.out.println("老年,多运动!");
}else {
System.out.println("老寿星,古来稀!");
}
}
}
51 switch多选择结构
主要用来做多值的判断。
- 根据switch后表达式的值与case的值比较,若满足其中一个,则break退出语句。
- 若执行到最后一个case仍然不匹配,则执行default默认语句
- case标签值可以使整数、枚举或者字符串(string)
- switch适用于对表达式的等值判断,也可以用多选择结构if_else if_else来做。
- 对表达式的区间判断不能用switch,只能用多选择结构来做。
/**
* 测试switch语句
* 遇到多值判断的时候,使用switch。当然,switch完全可以使用ifelseifelse代替!
* @author 高淇
*
*/
public class TestSwitch {
public static void main(String[] args) {
int month = (int) (1 + 12 * Math.random());
System.out.println("月份:" + month);
switch (month) {
case 1:
System.out.println("一月份!过新年了!");
break;
case 2:
System.out.println("二月份!开春了");
break;
default:
System.out.println("我是其他月份!");
break; //可有可无
}
System.out.println("##############");
char c = 'a';
int rand = (int) (26 * Math.random()); //[0,25]中的任一整数
char c2 = (char) (c + rand);
System.out.print(c2 + ": ");
switch (c2) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("元音");
break;
case 'y':
case 'w':
System.out.println("半元音");
break;
default:
System.out.println("辅音");
}
}
}
52 while循环讲解
循环结构分为两大类:当型(while())和直到型(do()while() 不常用)
两种语法结构,while循环和for循环
//语法结构
while(布尔表达式){
循环体;
}
public class TestWhile {
public static void main(String[] args) {
//计算从1累加到100的和 :5050
//计算机可以暴力求解:累加
int i = 1;
int sum = 0;
while(i<=100) {
sum =sum +i;
i++;
}
System.out.println(sum);
}
}
还有一种形式是do…while 先进入循环,然后再进入判断,为真则继续循环,这个了解即可。
53 for循环讲解(重要)
for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
//以上面那个while循环为例,改成for循环
public class TestFor {
public static void main(String[] args) {
int sum = 0;
for( int i = 1;i<=100;i++) { //i++就是步进迭代的关键语句
sum = sum + i; //i是在循环内部定义的,出了循环则i失效,需要时应重新定义
}
System.out.println(sum); //结果:5050
}
/*
初值,判断,循环,步进迭代,判断,循环.....步进迭代,判断条件不成立,跳出循环。
*/
}
//复杂点的情况
for (int i = 1,j = i+10; i<5; i++,j = i * 2){
System.out,println("i = " + i + ",j = "+ j);
}
//无限循环或者死循环
for(;;){ //while(true)
System.out,println("爱你到永远!");
}
54 嵌套循环
在一个循环语句内部再套一个或多个循环,称为嵌套循环。
/**
* 测试嵌套循环
* @author 喵喵
*
*/
public class TestWhileQiantao {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
System.out.print(i+"\t"); //ptintln中的ln的意思是每次打印之后就换行
}
System.out.println();
//换行,等价于:System.out.print("\n");
}
System.out.println("###########################");
//打印99乘法表:1*1=2 2*2=4
for(int n=1;n<=9;n++){
for(int m=1;m<=n;m++){
System.out.print(m+"*"+n+"="+(m*n)+"\t");
}
System.out.println(); //换行
}
System.out.println("#################################");
//用while循环分别计算100以内的奇数及偶数的和,并输出
int sum01 = 0;
int sum02 = 0;
for(int i=1;i<=100;i++){
if(i%2==0){ //偶数
sum01 += i; //sum01 = sum01 +i;
}else{ //奇数
sum02 += i;
}
}
System.out.println("奇数和:"+sum01);
System.out.println("偶数和:"+sum02);
//用while循环或其他循环输出1-1000之间能被5整除的数,且每行输出5个
int h =0;
int j = 0;
for(int i=1;i<=1000;i++){
if(i%5==0){
System.out.print(i+"\t");
h++;
j++;
}
if(h==5){ //满5个数后换行
System.out.println();
h=0; //重新计数
}
/*
if(i%25==0){ //这样也可以,因为每个25个数字中有5个可以整除的数
System.out.println();
}*/
}
System.out.println("1~1000共有"+j +"个数能被5整除");
}
}
结果:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
###########################
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
#################################
奇数和:2550
偶数和:2500
5 10 15 20 25
30 35 40 45 50
55 60 65 70 75
80 85 90 95 100
105 110 115 120 125
130 135 140 145 150
..........中间省略
980 985 990 995 1000
1~1000共有200个数能被5整除
55 break和continue语句
break用于强行退出循环,不再执行循环中剩余的语句。
无论进行到哪一步,只要遇到break,直接退出循环。
continue用于跳过本次循环,无论进行到哪一步,本次循环后面的
语句不再执行,而是直接执行迭代语句,然后判断循环条件。
/**
* 测试循环语句中的break
*
*/
public class TestBreak {
public static void main(String[] args) {
int total = 0;// 定义计数器
System.out.println("Begin");
while (true) {
total++;// 每循环一次计数器加1
int i = (int) Math.round(100 * Math.random());
//Math.rount是指四舍五入取整数,负数往大的入。 -12.5 = -12;-12.4 = -12;-12.6 = -13
//如果没有Math。round,则直接舍去小数
System.out.println(i);//输出本次的随机值
// 当随机值i等于88时,退出循环
if (i == 88) {
break;
}
}
// 输出循环的次数
System.out.println("Game over, used " + total + " times.");
}
}
public class TestContinue {
public static void main(String[] args) {
// 把100~150之间不能被3整除的数输出,并且每行输出5个
int count = 0;// 定义计数器
for (int i = 100; i < 150; i++) {
// 如果是3的倍数,则跳过本次循环,继续进行下一次循环
if (i % 3 == 0) {
continue;
}
// 否则(不是3的倍数),输出该数
System.out.print(i + "\t");
count++;// 每输出一个数,计数器加1
// 根据计数器判断每行是否已经输出了5个数
if (count % 5 == 0) {
System.out.println();//换行
count = 0; //重新计数
}
}
}
}
56 带标签的break和continue
首先聊一下goto关键字,它很早在程序设计语言中出现,用于转向程序语句的执行顺序。尽管goto语句作为了Java的保留字,但并未得到使用;Java中没有goto语句。
goto语句不能滥用,但有时候需要,就引入了标签:lable:
Java仅在循环语句之前用到标签。 设置标签的唯一理由:我们希望在其中嵌套一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
|
57 方法的定义_形参_实参_返回值_语句块
语句块,有时又叫复合语句,是用花括号括起来的任意数量的简单Java语句。块确定了局部变量的作用域。块中的程序代码作为一个整体,是要被一起执行的。块可以被嵌套在另一个块中,但是不能在两个嵌套的块内声明同名的变量。语句块可以使用外部的变量,而外部不能使用语句块中定义的变量,因为语句块中定义的变量作用域只限于语句块。
|
**方法:**可以看成带名字的语句块。是一段用来完成特定功能的代码片段,类似于C语言的函数。
方法用于定于该类或该类的实例的行为特征和功能实现。方法是类和对象行为特征的抽象。方法很类似于面向过程中的函数。面向过程中,函数是基本单位,整个程序由一个个函数调用组成。面向对象中,整个程序的基本单位是类,方法是从属于类和对象的。
方法的声明格式:
{修饰符1 修饰符2 ...} 返回值类型 方法名(形参列表){
Java语句; ... ... ...
}
形参:定义的时候传入的参数
实参:使用的时候传入的参数
e
58 方法的重载
59 递归算法讲解_递归和迭代效率测试
递归的基本思想就是”自己调用自己“。