控制流程
Java使用条件语句和循环结构确定控制流程
块作用域
- 块(即复合语句)由一对大括号括起来的若干条简单的Java语句.块确实了变量的作用域.一个块可以嵌套在另一个块中,下面就是main方法块中嵌套另一个语句.
public static void main(String[] args)
{
int n;
...
{
int k;
....
}// k is only defined up to here.
}
- 但是,不能在嵌套的两个块中声明同名的变量.例如,下面的代码有错误,而无法通过编译
public static void main(String[] args)
{
int n;
...
{
int k;
int n;// Error--can't redefine n in inner block
}
}
条件语句
- 在Java中,条件语句的格式为:
if(condition) statement
,这里的条件必须用括号括起来. - Java可以在条件为真时执行多条语句,在这种情况下,使用块语句.形式为
{
statement1;
statement2;
}
- 例如
if(yourSales >= target)
{
performance = "Satisfactory";
bouns = 100;
}
- 在Java中更一般的条件语句为
if(condition)statement1 else statement2
,其中else部分是可选的,else子句与最邻近的if构成一组. - 重复交替使用else if的格式
if(codition1)
{
statement1;
}
else if(condition2)
{
statement2;
}
else
{
statement3;
}
循环
- 当条件为true时,while循环执行一条语句(也可以是一个代码块),如果开始循环的条件为false,则while循环体一次也不执行.
while(condition) statement;
.
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("How much money do you need to retire?");
double goal=scanner.nextDouble();
System.out.println("How much will you contribute every year?");
double payment=scanner.nextDouble();
System.out.println("Interest rate in %");
double interestRate=scanner.nextDouble();
double balance=0;
int years=0;
while (balance<goal)
{
balance+=payment;
double interest=balance*interestRate/100;
balance+=interest;
years++;
}
System.out.println("You can retire in "+years+" years");
}
- do-while循环:先执行,后判断.所以代码至少被执行一次.
do statement while(condition)
.
do
{
balance+=payment;
double interest=balance*interestRate/100;
balance+=interest;
years++;
}while(input.equals("N")) //只要用户回答"N"时,循环就重复执行.
- 确定循环(for循环):是一种通用结构,利用每次迭代之后更新的计数器或类似的变量来控制迭代次数.
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
警告:在循环中,检测两个浮点数是否相等需要小心,下面的循环
for(double x=0;x!=10;x+=0.1)
可能永远不会结束,由于舍入误差,最终可能得不到精确值.
- 当在for语句的第一部分声明了一个变量之后,这个变量的作用域就为for循环的整个循环体.
for(int i=1;i<=10;i++)
{
....
}// i is still defined here.
- 多重选择:switch语句:在处理多个选项时,使用ifelse结构显得笨拙,Java使用switch语句进行多重选择.switch语句将从与选项值相匹配的case标签处开始执行知道遇到break语句,或者结束为止.如果没有相匹配的case标签,而有default子句,就执行这个子句.
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Select an option(1,2,3,4)");
int choice = scanner.nextInt();
switch (choice)
{
case 1:
...
break;
case 2:
...
break;
case 3:
...
break;
default:
// bad input
break;
}
}
- case标签可以是如下的类型
- 类型为char,byte,short,int的常量表达式.
- 枚举常量.
- 字符串字面量.
中断控制流程语句
- break的使用
while(years <= 100)
{
balance+=payment;
double interest=balance*interestRate/100;
balance+=interest;
if(balance >= goal) break;
years++;
}
- 还提供了一种带标签的break语句
read_data;
while(years <= 100)
{
balance+=payment;
double interest=balance*interestRate/100;
balance+=interest;
if(balance >= goal) break read_data;
years++;
}// 执行到break,会跳到标签.
- continue语句:将控制转移到最内层循环的首部.
while(years <= 100)
{
balance+=payment;
double interest=balance*interestRate/100;
balance+=interest;
if(balance >= goal) continue;
years++;
}// 执行到continue时,会跳到while循环开头(也就是跳过该次循环,执行下次).
大数值
- 如果基本的整数和浮点数精度不够,那么可以使用java.math包中两个类:BigInteger和BigDecimal.这两个类可以处理包含任意长度数字序列的数值,BigInteger类实现任意精度的整数运算,BigDecimal实现了任意精度的浮点数运算.
- 缺点:不能使用+-*/处理大数值,而需要使用类中的add和multiply方法.
- BigInteger中的方法
BigInteger add(BigInteger other)
:将两个大数值整数相加.BigInteger subtract(BigInteger other)
:将两个大数值整数相减.BigInteger multiply(BigInteger other)
:将两个大数值整数相乘.BigInteger divide(BigInteger other)
:将两个大数值整数相除.BigInteger mod(BigInteger other)
:将两个大数值整数相余.int compareTo(BigInteger other)
:如果这个大数值与另一个整数other相等返回0,如果这个大整数小于另一个,返回负数,否则返回正数.static BigInteger valueOf(long x)
:返回值等于x的大整数.
- BigDecimal中的方法
BigDecimal add(BigDecimal other)
:将两个大数值浮点数相加.BigDecimal subtract(BigDecimal other)
:将两个大数值浮点数相减.BigDecimal multiply(BigDecimal other)
:将两个大数值浮点数相乘.BigDecimal divide(BigDecimal other)
:将两个大数值浮点数相除.BigDecimal mod(BigDecimal other)
:将两个大数值浮点数相余.int compareTo(BigDecimal other)
:如果这个大数值浮点数与另一个浮点数other相等返回0,如果这个大整数小于另一个,返回负数,否则返回正数.static BigDecimal valueOf(long x)
:返回值等于x的实数.static BigDecimal valueOf(long x,int scale)
:返回值等于x/(10^scale)的实数.