控制流程
条件语句+循环结构 控制流程
条件语句 循环语句 switch语句(判断多个值)
块 用 { }括起来的若干条Java语句
块可嵌套在另一个块中
public static void main(String[] args){
int n;
......
{
int k;int k;
...... //k的作用域只在这个块中
//这里不能在声明int n; 嵌套的两个块不能声明同名变量
}
}
(C++中允许声明同名变量,内层定义会覆盖外层定义的变量,会导致程序设计出错,Java不允许)
条件语句
if(condition) {
statement1;
.....
}
或者
if(condition){
..... .....
}
else{
..... .....
}
或者
if…else if… …else
循环语句
while(condition) statement //循环体statement
//循环体可一次都不执行
或者
do statement while(condition)
//循环体至少执行一次
或者
for(int i = 1 ;i < 10;i++)
//计数器i 初始化
//每次新一轮循环要检查循环条件i<10是否符合,不符合的跳出循环
System.out.println(i);
若想在循环体外部使用i的最终值,则要在外部声明变量i
int i;
for(i=1;i<10;i++)
System.out.println(i);
for—>while 转换
int i=1;
while(i<10){
System.out.println(i);
i++;
}
switch语句
处理多个选项时使用
从case开始执行,
遇到break语句或者switch语句的结束处 } 结束执行
Scanner in = new Scanner(System.in);
System.out.print("Select an option(1,2,3,4)");
int choice = in.nextInt();
switch(choice){
case 1:
.....
break;
case 2:
......
break;
case 3:
......
break;
case 4:
......
break;
default: //其他输入 非1,2,3,4
......
break;
}
注意:若触发多个分支且case分支后无break语句,则继续执行下一个case分支。这种情况若以引发错误。程序中尽量不使用switch
case标签只能是整数或者枚举常量
在使用枚举常量时,直接使用常量值就可以
Size sz =......;
switch(sz){
case SMALL: //不必用Size.SMALL
......
break;
......
}
中断控制流程语句 break、continue语句
- 不带标签的break语句
与switch语句中的break一样 - 带标签的break语句
用于跳出多重嵌套的循环语句 在需要跳到的位置上放标签
标签格式为 名字:
Scanner in =new Scanner(System.in);
int n;
read_data:
while(...){
for(...){
System.out.print("Enter a number:");
n = in.nextInt();
if(n>7) //若输入大于7,则跳转到
break read_data; //跳转到标签read_data
......
}
}
可以将标签应用到任何语句中,甚至if语句或块语句中(只能跳出语句块,不能跳入语句块)
- continue语句
转移到最内层的循环处
Scanner in = new Scanner(System.in);
while(sum<goal){
System.out.print("Enter a number:");
n = in.nextInt();
if(n<0) continue;
//若n<0,则continue直接跳转到当期循环体的首部
//若是for语句,则跳转到第三个条件处
sum+=1;
}
带标签的continue语句,跳转到与标签匹配的循环首部
注:break,continue容易混淆,尽量不用
大数值
java.math包中BigInteger、BigDecimal类 任意精度的整数运算、浮点数运算
BigInteger.valueOf(100) 将普通的数值转换为大数值
不可使用算术运算符+、-等处理大数据
只能使用add和multiply方法
BIgInteger c = a.add(b); //c=a+b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); //d = c +(b+2)
API java.math.BigInteger.*
- BigInteger add(BigInteger other)
- BigInteger subtract(BigInteger other)
- BigInteger multiply(BigInteger other)
- BIgInteger divide(BigInteger other)
- BIgInteger mod(BIgInteger other)
一个大整数和另一个大整数othe的和、差、积、商、余 - int compareTo(BigInteger other)
若两个大整数相等,返回0;比other小,返回负数;比other大,返回正数 - static BigInteger valueOf(long x) 返回等于x的大整数
BigDecimal与BigInteger类似 详细请看API
数组
int[] a = new int[100];
int a[] = new int[100]; 数组下标从0-99
循环赋值:
for(int i = 0;i<100;i++)
a[i] = i;
一旦创建数组就不可以改变它的大小,若想在运行中扩展数组,则应该使用数组列表(第五章)array list
For each循环
for(variable:collection) statement
定义variable变量用于暂存集合中的每一个元素,并执行相应的语句/块语句
collection集合表达式 必须是 一个数组或一个实现了Iterable接口(卷11第二章)的类对象(例:ArrayList)
for (int element : a) //遍历数组中的每个元素
System.out.println(element);
//打印数组a中的每一个元素,一个元素占一行
传统for循环的实现:
for(int i= 0;i<a.length;i++)
System.out.println(a[i]);
注:打印数组中所有值:
System.out.println(Arrays.toString(a)); //结果为 [2,3,5,7,11,13]
数组初始化+匿名数组
数据初始化 数据初始化之后方可使用
int[] a =[2,3,5,7,11,13]; //直接赋予初始值
匿名数组 无数组名
new int[] {17,19,23,29}
数组的大小 = 初始值的个数
直接将数组赋值给数组
int []b;
b=a;
允许长度为0的数组,但不允许为null(第四章)
数组拷贝
拷贝:两个变量引用同一个数组
int[] copiedLuckyNumbers = Arrays.copyOf(Numers,Numbers.length);
//第二个参数是新数组的长度,通常用来增加数组大小2*length等
//若数组元素是数值型,多余元素赋值为0;
//若数组元素为布尔型,赋值为false
//若长度小于原始长度,则只拷贝最前面的
Java SE 6之前,将一个数组拷贝到另一个数组上用System类的arraycopy方法
System.arraycopy(from,fromIndex,to,toIndex,count)
//数组to必须有足够的空间存放拷贝的数组
//从from数组中第fromIndex位置开始拷贝,一共拷贝count个,拷贝到to数组的起始位置为toIndex
java数组中的[ ]运算符被预定义为检查数组边界,无指针运算,即不可通过a加1得到数组的下一个元素
命令行参数
命令行下
public class Message{
public static void main(String[] args){
if(args[0].equals("-h"))
System.out.print("Hello,");
if(args[0].equals("-g"))
System,out.print("Goodbye,");
for(int i = 1; i < args.length;i++)
System.out.print(" "+args[i]);
System.out.println("!");
}
}
用 java Message -g cruel world 运行该程序
args数组包含:args[0]:"-g"
args[1]:“cruel”
args[2]:“world”
程序结果为 Goodbye,cruel world!
数组排序
Arrays.sort( a ) a为数组
Math.random()返回一个0-1之间的(包含0,不包含1)的随机浮点数 用n乘以这个浮点数,就能得到从0到n-1之间的随机数
显示随机数排序的结果,对其使用Array.sort()进行排序
import java.util.*;
public class LotteryDrawing{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("你需要构造多大的数组?");
int k = in.nextInt();
SYstem.out.print("构造的数组中最大的数是?");
int n = in.nextInt();
int[] numbers = new int[n]; //构建数组 值为 1,2,3,,,n
for(int i = 0;i<numbers.length;i++)
numbers[i] = i+1;
int[] result = new int[k]; //构建第二个数组 存入随机数
for(int i =0; i<result.length;i++){
int r = (int)(Math.random()*n); //随机数0 ~ n-1
result[i] = numbers[r]; // 将随机数存入result中 1 ~ n
numbers[r] = numbers[n-1]; //把最后一个值n,n-1,n-2,,,,1, 赋给以随机数为下标的numbers的值
n--;
}
Arrays.sort(result);
System.out.println("打印排序后的数组:");
for(int r : result)
System.out.println(r);
}
}
多维数组 “数组的数组” Java实际上无多维数组,只有一维数组
二维数组 数据类型[][] 数组名 =
初始化 = new 数据类型[x][y] int[][] ds = new int[2][3];
直接进行初始化(含数据) int[][] ds = {{1,2,3,4},{5,6,7,8},{9,2,4,5},{6,3,1,7}}
for( ){//第一层for {1,2,3,4} i=0时 {5,6,7,8) i=1时。。。
for(){ //处理1,2,3,4
//处理5,6,7,8 。。。。
}
}
使用for each的代码:显示二维数组的全部内容
for (int[] row:数组名){
for(int n:row){
System.out.printf(" ",);
}
}
不规则数组
存储三角形的数据 |_ 这种格式的
int[][] odds = new int[NMAX +1];//构造二维数组
for(int n = 0; n<odds.length;n++)
//分配行 将每一行作为一个单元 {1,2,3,4}
odds[n] = new int[n+1];
for(int n = 0; n<=odds.length;n++) //odds的长度 有几个单元
for(int k = 0;k<odds[n].length;k++){ //单元中有多少的元素
......
}