1. 程式人生 > >【Java】基本Java語法,初學Java

【Java】基本Java語法,初學Java

常量: public static final double PI = 3.14159265358979323846; 函式: 將ang角度轉換成徑度 public static double toRadians(double ang) { return ang / 180.0 * PI; }
if語句 int score = 70;    // 考試成績       
 if ( score >= 90 ) {                System.out.println("優秀");         } else if (score >= 80 ) {                System.out.println("良好");
        } else if (score >= 60 ) {                System.out.println("中等");         } else {                System.out.println("差");
        }
基本語法示例:
檔名稱: ClassFun.java public class ClassFun { public double hight; private double weight, age; String name; // init all class object { System.out.println("init ClassFun class!"); this.hight = 1.75; this.age = 18; this.weight = 50; this.name = "administor"; } ClassFun(String new_name, double new_age, double new_hight, double new_weight){ this.age = new_age; this.name = new_name; this.hight = new_hight; this.weight = new_weight; } public void spoke_name(int password) { System.out.println("My name is : " + name); if(password == 999) spoke_weight(); else if(password == 888) spoke_age(); } private void spoke_weight() { System.out.println("My weight is : " + weight); } private void spoke_age() { System.out.println("My age is : " + age); }
}
檔名稱: PrintHello.java public class PrintHello { public static void print_name(String name) { // this function is used to print String type. System.out.println(name); }
public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!"); int []a = {1,2,3,4}; for(int i = 0;i < a.length;i++) { System.out.println("a===" + a[i]); }// for(int i = 0;i < a.length;i++) int b = 4; switch(b) { case 100: System.out.println("a = 100"); break; case 10: System.out.println("a = 10"); break; default: System.out.println("XIAO"); }// switch(b) String str = "aa"; while(b <= 100) { str = System.out.toString(); System.out.println("b = " + b + " -> " + str); b++; if(b == 88) break; else if(b == 77) { System.out.println("b == 77"); }// if(b == 88) }// while(b <= 100) print_name("xiaogongwei10"); ClassFun Xiao = new ClassFun("xiaogongwei", 28, 1.75, 100); Xiao.spoke_name(888); Xiao.hight = 2.0; Xiao.name = "David Xiao"; Xiao.spoke_name(999); print_name(Xiao.toString()); }//main
}