判断语句1—if

格式

  1. if(关系表达式) {
  2. 语句体;
  3. }

image.png

  1. public class Test {
  2. public static void main(String[] args) {
  3. int a = 1;
  4. int b = 1;
  5. if (a == b) {
  6. System.out.println("ok");
  7. }
  8. }
  9. }

判断语句2—if…else

格式

  1. if(关系表达式) {
  2. 语句体1;
  3. } else {
  4. 语句体2;
  5. }

image.png

  1. public class Test {
  2. public static void main(String[] args) {
  3. int a = 1;
  4. int b = 1;
  5. if (a == b) {
  6. System.out.println("true");
  7. } else {
  8. System.out.println("false");
  9. }
  10. }
  11. }

判断语句2—if…else if…else

格式

  1. if(关系表达式) {
  2. 语句体1;
  3. } else if (关系表达式) {
  4. 语句体2;
  5. } else if (关系表达式) {
  6. 语句体3;
  7. } else {
  8. 语句体;
  9. }
  1. public class Test {
  2. public static void main(String[] args) {
  3. int a = 1;
  4. int b = 1;
  5. int c = 1;
  6. if (a == b) {
  7. System.out.println("true");
  8. } else if (b == c ) {
  9. System.out.println("ture");
  10. } else {
  11. System.out.println("false");
  12. }
  13. }
  14. }