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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoxiao133/article/details/79808324
常量:
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

}




猜你喜欢

转载自blog.csdn.net/xiaoxiao133/article/details/79808324