java 三角运算与循环

开发工具与关键技术:java MyEclipse 10

三角运算:详情看代码注释;
代码如下:

package com.gx.zydemo;

public class ZuoYeThree {
public static void main(String[] args) {
// 将弧度转换角度
System.out.println("Math.toDegrees = " + Math.toDegrees(1.57));
// 将角度转换为弧度
System.out.println("Math.toRadians = " + Math.toRadians(90));
// 计算反余弦,返回的角度范围在 0.0 到 pi 之间。
System.out.println("Math.acos = " + Math.acos(1.2));
// 计算反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。
System.out.println("Math.asin = " + Math.asin(0.8));
// 计算反正切;返回的角度范围在 -pi/2 到 pi/2 之间。
System.out.println("Math.atan = " + Math.atan(2.3));
// 计算三角余弦。
System.out.println("Math.cos = " + Math.cos(1.57));
// 计算值的双曲余弦。
System.out.println("Math.cosh = " + Math.cosh(1.2));
// 计算正弦
System.out.println("Math.sin = " + Math.sin(1.57));
// 计算双曲正弦
System.out.println("Math.sinh = " + Math.sinh(1.2));
// 计算三角正切
System.out.println("Math.tan = " + Math.tan(0.8));
// 计算双曲正切
System.out.println("Math.tanh = " + Math.tanh(2.1));
// 将矩形坐标 (x, y) 转换成极坐标 (r, thet));
System.out.println("Math.atan2 = " + Math.atan2(0.1, 0.2));
}
}

结果如下截图所示:
在这里插入图片描述

符号相关方法:详情看代码注释;
代码如下:

package com.gx.zydemo;

public class ZuoYeThree {
public static void main(String[] args) {
// 计算绝对值。
System.out.println("Math.abs = " + Math.abs(-4.5));
// 符号赋值,返回带有第二个浮点数符号的第一个浮点参数。
System.out.println("Math.copySign = " + Math.copySign(1.2, -1.0));
// 符号函数;如果参数为 0,则返回 0;如果参数大于 0,
// 则返回 1.0;如果参数小于 0,则返回 -1.0。
System.out.println("Math.signum = " + Math.signum(2.3));
}
}

结果如下截图所示:
在这里插入图片描述

Java循环结构:while、do…while和for三种主要的循环结构和java增强for循环结构;
格式规范如下:
While格式:
while( 布尔表达式 ) {
//循环内容
}
do…while格式:
do {
//代码语句
}while(布尔表达式);
For格式:
for(初始化; 布尔表达式; 更新) {
//代码语句
}
java增强for格式:
for(声明语句 : 表达式) {
//代码句子
}

While循环例子:
public static void main(String[] args) {
//while(布尔(true/false)表达式){
//循环内容
//只要布尔表达式为 true 循环体就会一直执行下去。
int a = 1;
while (a < 5) {
System.out.println(" a = " + a);
a++;//自增到小于5停止,失去将会一直执行
}
}
如下截图:
在这里插入图片描述

do…while循环例子:
public static void main(String[] args) {
int a = 1;
do {
System.out.println("a = " + a);
a++;
} while (a < 5);
}

如下截图:
在这里插入图片描述

While循环和do…while循环相同,不同的是,do…while循环至少会执行一次。

For循环例子:
public static void main(String[] args) {
for (int a = 1; a < 5; a = a + 1) {
System.out.println("a = " + a);
}
}

如下截图:
在这里插入图片描述

java增强for循环例子:
public static void main(String[] args) {
int[] numbers = { 1, 2, 3, 4, 5 };
for (int a : numbers) {
System.out.print(“a=” + a);
System.out.print(" , “);
}
System.out.print(”\n");
String[] b = { “小明”, “小李”, “小花”, “小红” };
for (String name : b) {
System.out.print(“name:” + name);
System.out.print(" , ");
}
}

如下截图:
在这里插入图片描述

break、continue、return关键字意义:
break : 结束循环
continue : 结束本次循环,进入下一次循环
return : 结束一个方法,方法的结束了,循环自然被结束
注意:在while和do…while中使用continue时要注意 更新语句的位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值