学习编程,我们本能的起点自然如何进行数学运算,如何处理数学表达式。
即使没有编程的概念和计算机基础,在java的交互模式Jshell内,也能单单依靠本能做“加减乘除”运算。
jshell> 60 * 60 * 24
$8 ==> 86400
jshell> 177 + 3
$9 ==> 180
jshell> 177 - 3
$10 ==> 174
jshell> 177 / 3
$11 ==> 59
jshell> 177.0 / 3
$12 ==> 59.0
jshell> 177 % 3
$13 ==> 0
jshell> 177.0 % 4
$14 ==> 1.0
Java与C语言相同都没有幂运算,需要另外调用函数:
jshell> Math.pow(2, 11)
$17 ==> 2048.0
jshell> Math.sqrt(667)
$18 ==> 25.826343140289914
命名帮助我们超越记忆力的束缚,不必屡次都要重新书写一遍代码,而单单以抽象的命名调用代码结构。
举例计算圆的面积与圆锥的体积:
jshell> int radius = 9
radius ==> 9
jshell> int high = 21
high ==> 21
jshell> double S = Math.PI * radius * radius
S ==> 254.46900494077323
jshell> V = 1/3.0 * S *high
V ==> 1781.2830345854127
命名是“抽象”迈出的第一步,这种最基础的抽象手段表达力有限。函数作为更高阶的”抽象手段“就此引入。
在Java语言中,定义函数的语法规则,一言以蔽之 ”脱裤子放屁“。所有的函数必须写入到Class内,基本结构为:
public class Main {
public static void foo() {
// Do something here
}
}
写一个求square的函数:
public class Main {
public static double square(double x) {
return x * x;
}
}
从Jshell种运行:
jshell> Main.square(11)
$21 ==> 121.0
其中static-method隶属class,non-static-method隶属instance。
我们的抽象表达能力逐步升级,从有限抽象的”命名与变量”升级到”函数“。当下遇到的问题是,仅凭借以上两种工具,我们尚不能对某些条件实行测试,比如写一个简单地求绝对值的函数。
Java选择语句的基本结构为:
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
写一个求绝对值的函数:
jshell> public class Main {
...> public static double abs(double x) {
...> if (x >= 0) {
...> return x;
...> } else {
...> return -x;
...> }
...> }
...> }
| replaced class Main
jshell> Main.abs(-121)
$23 ==> 121.0
在条件语句中,除了比较大小><=!这几类简单的关系之外,还有逻辑运算符:
&& Conditional-AND
|| Conditional-OR
编程的首要任务之一就是要协助我们从机械重复的活动中抽身而出,将其全部交由计算机完成。
与C语言完全一致的While语句与do-while语句:
while (expression) {
statement(s)
}
do {
statement(s)
} while (expression);
While的案例
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
运行输出结果为:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
基本语法结构与C语言完全一致。
do-while的案例:
class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
}
}
运行并输出结果为:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
While语句往往多用于不确定循环次数的场合,而针对确定循环次数的应用场景,我们多考虑适用for-stament(又被称之为determinate-loop)。
for-statement的基本结构为:
for (initialization; termination;
increment) {
statement(s)
}
看具体的案例:
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}
与while(true)相同,for语句同样能处理无限循环:
// infinite loop
for ( ; ; ) {
// your code goes here
}
在实践中,我们用简单的方法处理数组的遍历:
class EnhancedForDemo {
public static void main(String[] args){
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}
至此,我们已经介绍完毕,编程语言表达力的所有元素,下面我们应用这些技术求平方根。
牛顿求平方根法用的是‘连续逼近’的思路 "successive approximations"。我们先猜一个y作为x的平方根,然后在此基础上更好的瞎猜,不断地接近平方根的真实值。这个bette-guess为前面的猜测值得与(x猜测值)的平均值,分步过程展示为:
public class Newton {
public static void main(String args[]) {
Newton instance = new Newton();
System.out.println(instance.sqrt(4.33));
}
public double sqrt(double x) {
return sqrtIter(1, x);
}
public double sqrtIter(double guess, double x) {
if (x < 0) {
throw new IllegalArgumentException("x must not be negative.");
}
while (true) {
if (goodEnough(guess, x)) {
return guess;
} else {
guess = improve(guess, x);
}
}
}
public double improve(double guess, double x) {
return average(guess, x / guess);
}
public double average(double x, double y) {
return (x + y) / 2;
}
public boolean goodEnough(double guess, double x) {
return Math.abs(guess * guess - x) < 0.0001;
}
}
页面更新:2024-05-30
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号