if语句:
根据条件判断是否执行某一代码。
1.if条件判断语句基本形式
(1)if-then类型:
if (commands){
commands;
}
(2)if-then-else类型:
if (commands){
commands;
} else {
commands;
}
(3)if-else if-else类型:
if (commands1){
commands;
} else if (commands2){
commands;
} else if (commands3){
commands;
} ... {
} else {
commands;
}
2.关系运算符与逻辑运算符
与许多编程语言一样:
关系运算符 | 逻辑运算符 |
大于:> | 与:&& |
大于等于:>= | 或:|| |
小于:< | |
小于等于:<= | |
等于:== 不等于:!= |
3.注意
(1)当if语句省略花括号{}时,该语句块仅对下方一行语句起作用。如:
public class Main {
public static void main(String[] args) {
int n = 100;
if (n >= 90)
System.out.println("优秀");
System.out.println("合格");
}
}
无论条件判断是否成立,都会输出”合格“,if语句判断结果仅对是否执行System.out.println("优秀”)有效。
由于缩进形式容易出现问题,很容易把相邻的语句也看作if语句的执行块,因此不建议平时写作时忽略花括号。
(2)else语句一般不进行串联,而else if语句可以进行串联。
(3)注意if条件语句的执行顺序:(因为当从上到下出现第一条满足条件的语句时仅执行当前语句,后续语句不再执行)因此应注意判断条件(范围)应按照要求从大到小或从小到大书写。
4.浮点数的判断
浮点数的判断应该利用差值小于某个临界值。
当涉及浮点数时,编译存在以下情况:
public class EqualTest {
public static void main(String[] args) {
double x = 1 - 9.0 / 10;
System.out.println("x="+x);
}
}
显然x=0.1,而编译结果却不为0.1。这是由于涉及浮点数计算时,存在精度丢失的情况。因此在涉及浮点数判断时,应该尽量避免直接使用”==“的情况判断是否相等,而采用差值小于临界值的条件来判断,如下所示:
public class EqualTest {
public static void main(String[] args) {
double x = 1 - 9.0 / 10;
if(Math.abs(x-0.1)<0.0001) {
System.out.println("x=0.1");
} else {
System.out.println("x≠0.1");
}
}
}
更精细的解决浮点数精度丢失的方法:采用构造方法创建BigInteger和BigDecimal对象。详见:https://blog.csdn.net/m0_66605858/article/details/125664245?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_utm_term~default-0-125664245-blog-102770679.235^v38^pc_relevant_sort_base3&spm=1001.2101.3001.4242.1&utm_relevant_index=3
5.留存问题:字符串的判断
字符串对对象判断,使用”==“;对内容判断,使用equal()语句。
方法(函数)调用
为了使代码能够重复使用,因此引入函数,同时更方便于后期代码的维护与修改。
(1)声明函数
创建(声明)函数的基本语法:
修饰符 返回值类型 方法/函数名(参数形式[] 参数名){
...
}
例如:
package com.pancake;
public class HelloWorld {
public static void main(String[] args){
System.out.println("helloworld!");
}//of main
public static void test(){
System.out.print("This is a test");
}//of test
}//of main
在public static void main(String[] args)中,public static修饰名为main的方法,表示其为公开的静态方法,void表示其返回类型;括号内是参数表,其中args是参数名,String[]是参数类型。
在写上述代码过程中,IDEA会提示:Method 'test()' is never used.并且此时编译结果也仅有helloworld!这是因为除了主函数之外其他自定义函数都需要手动调用才可执行。
(2)调用自定义函数
调用:即执行函数内容,具体形式如下:
对象或类名.函数名(参数表);
对上述函数进行修改后,简单调用test函数,如下所示:
package com.pancake;
public class HelloWorld {
public static void main(String[] args){
System.out.println("helloworld!");
test();//调用
}//of main
public static void test(){
System.out.print("This is a test");
}//of test
}//of main
此时输出:
说明调用test函数成功!
问题:如何使用参数返回值等?
方法(函数)头部规范注释
//单行注释
/*
* 多行注释
*/
/**
* 文档注释
* 写入Javadoc中,一般在类、方法变量上方。
*/
IDEA中自动生成文档注释的方法:(创建类的时候即出现)
File-Settings-Editor-File and Code Templates-右侧Includes-File Header在里面编辑即可:
快捷键注释方法:
详见在idea中设置文档注释的方法(图文版)_idea文档注释_数据Java的博客-CSDN博客
练习代码
package com.pancake;
/**
* @Auther:CubicPancake
* @Date:2023-10-08
* @Description:{BasicStructionIf}
*/
public class IfStatement {
/**
* ********************
* The entrance of the program.
*
* @param args Not used now.
* ********************
*/
public static void main(String args[]) {
int tempNumber1, tempNumber2;
// Try a positive value
tempNumber1 = 5;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
} // Of if
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
// Try a negative value
// Lines 27 through 33 are the same as Lines 15 through 19
tempNumber1 = -3;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
} // Of if
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
// Now we use a method/function for this purpose.
tempNumber1 = 6;
System.out.println("The absolute value of " + tempNumber1 + " is " + abs(tempNumber1));
tempNumber1 = -8;
System.out.println("The absolute value of " + tempNumber1 + " is " + abs(tempNumber1));
}// Of main
/**
*********************
* The absolute value of the given parameter.
*
* @param paraValue The given value.
*********************
*/
public static int abs(int paraValue){
if (paraValue >= 0) {
return paraValue;
} else {
return -paraValue;
} // of if
} // of abs
}// of class IfStatement
最终运行结果: