您的位置:首页 > 编程语言 > Java开发

【Java】基本Java语法,初学Java

2018-04-03 19:57 267 查看
常量: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.javapublic 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.javapublic 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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java 初学