Java 语法 索引 ----- 条件语句(If Else,Switch)

if (x < 1)
   System.out.print(x + " < 1");
else if (x > 1)
   System.out.print(x + " > 1");
else
    System.out.print(x + " == 1");

Switch

switch (y)
{
  case 0: System.out.print(y + " is 0"); break;
  case 1: System.out.print(y + " is 1"); break;
  default:System.out.print(y + " is something else");
}

三元运算符

x = (x < 0.5) ? 0 : 1;

If x < 0.5

x = 0;

else

x = 1 ;

Java 语法 索引 ----- 条件语句(If Else,Switch),布布扣,bubuko.com

时间: 2024-10-06 00:50:47

Java 语法 索引 ----- 条件语句(If Else,Switch)的相关文章

Java 语法 索引 ----- 枚举(Enum)

enum Speed { STOP, SLOW, NORMAL, FAST } Speed s = Speed.SLOW; switch(s) { case SLOW: break; } 参考文献: Java Quick Syntax Reference by Mikael Olsson Java 语法 索引 ----- 枚举(Enum)

Java 语法 索引 ----- 主函数 (Main)

public class MyApp { public static void main(String[] args) { System.out.print("Hello World"); } } Java 语法 索引 ----- 主函数 (Main),布布扣,bubuko.com

Java 语法 索引 ----- 变量-----数据类型

数据类型 类型 bits/byte 范围 默认值 byte 8/1 -128 +127 0 short 16/2 -32,768+32,767 0 int 32/4 -2,147,483,648 = -231+2,147,483,647 = 231-1 0 long 64/8 -9,223,372,036,854,775,808 = -263+9,223,372,036,854,775,807 = 263-1 0L float 32/4 1.40129846432481707e-45  = 2-

Java 语法 索引 ----- 运算符

算术运算符 float x = 3+2; // 5 // addition 加 x = 3-2; // 1 // subtraction 减 x = 3*2; // 6 // multiplication 乘 x = 3/2; // 1 // division 除 x = 3%2; // 1 // modulus (division remainder) 余数 Combined assignment operators int x = 0; x += 5; // x = x+5; x -= 5;

Java 语法 索引 ----- 数组(Arrays)

数组声明,分配, 赋值 int y[] = new int[3]; y[0] = 1; y[1] = 2; y[2] = 3; int[] x = new int[] {1,2,3}; int[] x = {1,2,3}; 二维数组 String[][] x = {{"00","01"},{"10","11"}}; String[][] y = new String[2][2]; y[0][0] = "00"

Java 语法 索引 ----- 循环(loop)

While 和  Do-While //while int i = 0; while (i < 10) { System.out.print(i++); } //do - while int i = 0; do { System.out.print(i++); } while ( i < 10 ); For 和 Foreach for (int i = 0; i < 10; i++){ System.out.print(i); } for (int k = 0, l = 10; k &l

Java 语法 索引 ----- 字符串(String)

组合吃烤串的各种方法 String a = "Hello"; String b = new String(" World"); //在循环里面的话要小心用 String c = a+b; // Hello World a += b; // Hello World String x = "Hello " + "World"; 比较字符串 boolean x = a.equals(b); // compares string bo

Java 语法 索引 ----- 接口

interface MyInterface { void exposed(); } class MyClass implements MyInterface { public void exposed() {} public void hidden() {} } public static void main(String[] args) { MyInterface i = new MyClass(); } 参考文献: Java Quick Syntax Reference by Mikael

Java 语法 索引 ----- 构造函数(constructor)

重载 class MyRectangle{ int x, y; public MyRectangle() { x = 10; y = 20; } public MyRectangle(int a) { x = a; y = a; } public MyRectangle(int a, int b) { x = a; y = b; } } Constructor chaining public MyRectangle() { this(10,20); } public MyRectangle(in