1.顺序结构
语句执行的基本顺序按各语句出现的位置先后顺序执行,称之为顺序结构;
public static void main(String[] args) {
double a,b,c,p,area;
Scanner scanner = new Scanner(System.in); //Scanner可用于扫描控制台输入
System.out.print("请输入三角形的边A: ");
String s = scanner.nextLine();
a = Double.parseDouble(s); //将数字字符串转换为等效的双精度浮点数
System.out.print("请输入三角形的边B: ");
s = scanner.nextLine();
b = Double.parseDouble(s);
System.out.print("请输入三角形的边c: ");
s = scanner.nextLine();
c = Double.parseDouble(s);
System.out.printf("三角形的3边分别为:a = %6.1f,b = %6.1f,c = %6.1f\n",a,b,c);
p = (a + b + c) / 2;
area = Math.sqrt(p * (p-a) * (p-b) * (p-c));
System.out.printf("三角形的面积 = %8.1f\n",area);
}
2.选择结构
选择结构根据条件来控制代码的执行分支,也称为分支结构(if语句 switch语句)
(1)if语句 (单分支、双分支、多分支)
(2)if语句的嵌套
(3)switch语句 switch语句是一个控制语句,它通过将控制传递给其体内的case语句来处理多个选项和枚举。
switch(控制表达式) {
case(常量表达式)1;
语句序列1
break;
case(常量表达式)2;
语句序列2
break;
.
.
.
case(常量表达式)n;
语句序列n
break;
default;
语句序列n+1;
break;
}
//单分支 产生1~100之间的随机数a,b,并比较两个值的大小,使得a大于b
public static void main1(String[] args) {
int a, b, t;
//Math.random()方法在默认的情况下产生0(含)~1(不含)的double型随机数
a = (int) (Math.random() * 101); //产生0~100之间的随机数
b = (int) (Math.random() * 101);
System.out.printf("原始值: a = %d,b = %d\n", a, b);
if (a < b) {
t = a;
a = b;
b = t;
}
System.out.printf("降序值: a = %d,b = %d\n", a, b);
}
//双分支
public static void main2(String[] args) {
double x = 12.3;
double y;
if (x >= 0)
y = Math.sin(x) + 2 * Math.sqrt(x + Math.exp(4)) - Math.pow(x + 1, 3);
else
y = Math.log(-5 * x) - Math.abs(x * x - 8 * x) / (7 * x) + Math.E;
System.out.printf("如果x = %f,则y = %f\n", x, y);
}
//多分支
public static void main3(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入学生成绩(百分制): ");
String s = scan.nextLine();
int mark = Integer.parseInt(s);
String grade;
if (mark >= 90)
grade = "优";
else if (mark >= 80)
grade = "良";
else if (mark >= 70)
grade = "中";
else if (mark >= 60)
grade = "及格";
else
grade = "不及格";
System.out.printf("学生成绩(百分制) = %d,评定等级 = %s\n", mark, grade);
}
//if语句的嵌套
public static void main4(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.printf("请输入年份; ");
String s = scan.nextLine();
int year = Integer.parseInt(s);
boolean isleapyear; //声明是布尔变量
if (year % 400 == 0) {
isleapyear = true;
} else {
if (year % 4 == 0)
if (year % 100 == 0)
isleapyear = false;
else
isleapyear = true;
else
isleapyear = false;
System.out.printf("%d是否为闰年: %s\n", year, isleapyear);
}
}
//switch语句
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.printf("请输入月份(1~12): ");
String sMonth = scan.nextLine();
int mInt = Integer.parseInt(sMonth); //将数字字符串转换为等效的整数
int mDays; //对应月份的天数
switch (mInt) {
case 2:
mDays = 28;break;
case 4:
case 6:
case 9:
case 11:
mDays = 30;break;
default:
mDays = 31;break;
}
System.out.printf("%d月份有%d天。",mInt,mDays);
}
3.循环结构
for循环 while循环 do…while循环 for each循环
public static void main1(String[] args) {
//for循环求1~100中所有的奇数的和、偶数的和
int sum = 0, sumOdd = 0, sumEven = 0;
for (int i = 0; i < 100; i += 1) {
sum += i;
}
for (int i = 1; i < 100; i += 2) {
sumOdd += i;
}
for (int i = 2; i < 100; i += 2) {
sumEven += i;
}
System.out.printf("1~100中所有书的和位%d、奇数和为%d、偶数和为%d\n", sum, sumOdd, sumEven);
}
//显示斐波那锲数列前20项
public static void main2(String[] args) {
int f1 = 1, f2 = 1;
for (int i = 1; i <= 10; i++) {
System.out.printf("%5d\t%5d\t", f1, f2); //一次输出两项
if (i % 2 == 0) { //每行显示4项
System.out.println();
}
f1 += f2;
f2 += f1;
}
}
//while循环 求1+2+3+...的和,直到和大于3000为止
public static void main3(String[] args) {
int i = 1, sum = 0;
while (sum <= 3000) {
sum += i;
i++;
}
System.out.printf("结果为%d\n", sum);
System.out.println(i);
}
//do...while循环
public static void main4(String[] args) {
int i, sum, sumOdd, sumEven;
i = 1;
sum = 0;
do {
sum += i;
i++;
} while (i <= 100);
i = 1;
sumOdd = 0;
do {
sumOdd += i;
i += 2;
} while (i <= 100);
i = 2;
sumEven = 0;
do {
sumEven += i;
i += 2;
}
while (i <= 100);
System.out.printf("1~100中的所有数的和为%d、奇数和为%d、偶数和为%d\n", sum, sumOdd, sumEven);
}
/**
* 求任意两个正整数的最大公约数和最小公倍数
* 利用“辗转相除法”
* 对于一直的两个正整数m,n,使得m>n;
* m除以n得余数r
* 若r!= 0,则令m = n,n = t,继续相除得到新的余数r,如此重复,直到r = 0为止,最后的m就是最大公约数
* 求得最大公约数后,最小公倍数就是已知两个正整数之积除以最大公倍数的商
*/
public static void main5(String[] args) {
int m, n, r, m1, n1;
Scanner scan = new Scanner(System.in);
System.out.print("请输入整数m(>0): ");
String s = scan.nextLine();
m1 = Integer.parseInt(s);
System.out.print("请输入整数n(>0): ");
s = scan.nextLine();
n1 = Integer.parseInt(s);
//m > n
if (m1 > n1) {
m = m1;
n = n1;
} else {
m = n1;
n = m1;
}
do {
r = m % n;
m = n;
n = r;
} while (r != 0);
System.out.printf(" % d 和 % d 的最大公约数 = %d,最小公约数 = %d\n", m1, n1, m, m1 * n1 / m);
}
//for each循环
public static void main6(String[] args) {
int[] array = new int[]{12, 33, 45, 66, 56};
for (int i : array) {
System.out.printf("%5d", i);
}
}
4.跳转语句
a.break语句
(1)不带标签的break语句:在switch语句中的作用是跳出switch结构,继续执行switch语句的后继语句。
不带标签的break语句还可以用于退出for、for each、while、或do…while循环,
即提前结束循环,接着执行循环语句的后继语句。
注意:a.不能用于switch语句和循环语句之外的任何语句中,否则会产生编译时错误。
b.当有多个switch、while、do…while、for或for each语句彼此嵌套时,不带标签的break语句只应用于最里层的语句;
若要穿过多个嵌套层转移控制,必须使用带标签的break语句。
(2)带标签的break语句: 用于跳出由标签标记的语句/语句块。标签是一个标识符,标签后面带一个冒号,并置于一个语句或语句块之前,则该标签标记该语句或语句块。
注:带标签的break语句跳转到的目的标签只能指向包含该break语句的外层语句块,否则会产生编译错误。
//break语句
public static void main7(String[] args) {
LabelLoop1:
for (int i1 = 1; i1 < 10; i1++) {
LabelLoop2:
for (int i2 = 2; i2 < 10; i2++) {
LabelLoop3:
for (int i3 = 1; i3 < 10; i3++) {
System.out.printf("i1: %d,i2: %d,i3: %d\n", i1, i2, i3);
LabelLoop4:
System.out.println("处理语句4");
break LabelLoop1; //编译OK,跳出最外层循环i1;
//break LabelLoop2; //编译OK,跳出最外层循环i2;
// break LabelLoop3; //编译OK,跳出最外层循环i3;
// break; //编译OK,跳出最外层循环i3;
// break LabelLoop4; //编译错误,只能跳转至指向包含该break语句的外层语句块的标签
//break LabelLoop5; //编译错误,只能跳转至指向包含该break语句的外层语句块的标签
// break LabelLoop6; //编译错误,只能跳转至指向包含该break语句的外层语句块的标签
}
LabelLoop5:
System.out.println("处理语句5");
}
System.out.println("处理语句6");
}
}
//判断任意输入的一个正整数是否为素数 ---》 能否被2~(自身开根号)之间的任意一个整数整除即可,不能被整除就是素数,否则就是合数
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个正整数(>=2): ");
String s = scan.nextLine();
int m = Integer.parseInt(s);
int i,k;
//用for循环和break语句
k = (int) (Math.sqrt(m));
for ( i = 2; i <= k; i++)
if(m % i == 0)
break;
if(i == k + 1)
System.out.printf("整数%d是素数!",m);
else
System.out.printf("整数%d是合数!",m);
}
//用while循环和boolean变量
boolean flag = true; //假设所输入整数为素数
k = (int)(Math.sqrt(m));
i = 2;
while(i <= k && flag == true) {
if(m % i == 0) {
flag = false;
}
i++;
}
if(flag == true) {
System.out.printf("整数%d是素数!",m);
}
else{
System.out.printf("整数%d是合数!",m);
}
b.continue语句:
不带标签的continue语句与不带标签的break语句一样,
但是它结束本次循环,及跳过循环体内continue下面尚未执行的语句,返回到循环的起始,并根据循环条件判断是否执行下一次循环;
带标签的continue语句,与带标签的break语句相类似,用于结束本次循环,并跳转到由标签标记的外层循环,
int j = 0; //控制一行显示的数字个数
System.out.println("100~200之间不能被3整除的整数为: ");
for (int i = 100; i <= 200; i++) {
if (i % 3 == 0) {
continue;
}
System.out.printf("%5d", i);
j++;
if (j % 10 == 0) {
System.out.println();
}
}
c.return语句
return语句用于终止方法的执行并控制返回给调用方法。如果方法有返回类型,return语句必须返回这个类型的值;
如果方法为void类型,应使用没有表达式的return语句;如果方法为void类型,方法体的最后的return语句可以省略。
//计算圆的面积
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入圆的半径: ");
String s = scan.nextLine();
double radius = Double.parseDouble(s);
System.out.printf("圆的面积为: %9.2f",CalculateArea(radius));
}
static double CalculateArea(double r) { //方法CalculateArea()以double值的形式返回变量Area
//计算圆的面积
double area = r * r * Math.PI;
return area;
}