javaSE基础总结

2019-06-18  本文已影响0人  尽人事听天命_6c6b

/**

*/
public class VariableDemo{
public static void main(String[] args) {
String name="tom";
byte age = 100;//一个字节=8位
short number = 45;//16位
int mon=1000;//32位
long nuk = 1133L;//64位
float f = 23.567F;
double d = 3.4;
boolean isRight = false;
char c = 'a';
System.out.println(c);
System.out.println(isRight);
System.out.println(f);
System.out.println(d);
System.out.println(34.567);//浮点数默认的类型为double类型
System.out.println(age+number+mon+nuk);//+会累加求和
System.out.println("相当于字符串的连接"+age+number+mon+nuk);//+相当于字符串的连接

}

}
/**

*/
public class OperatorDemo {

public static void main(String[] args) {
    
    
    int a = 5;
    int b = 2;
    int c = a + b;
    int d = a / b;
    int e = a % b;
    String s1 = "aaa";
    String s2 = "bbb";
    // 关系运算符--------------------------------start
    boolean b1 = a>b;
    boolean b2 = a<b;
    System.out.println(b1);//true
    System.out.println(b2);//false
    System.out.println(a>=b);//true
    System.out.println(a<=b);//false
    System.out.println(a==b);//false
    System.out.println(a!=b);//true
    // 关系运算符--------------------------------end
    
    // 赋值运算符--------------------------------start
    //      System.out.println(a+=b);
    //      System.out.println(a-=b);
    //      System.out.println(a*=b);
    //      System.out.println(a/=b);
    //      System.out.println(a%=b);
    // 赋值运算符--------------------------------end
    // 算术运算符--------------------------------start
    System.out.println("---------------------------");
    //算法:(a++)=5 a=6  (++a)=7 a=7   (a--)=7 a=6   (--a)=5 a=5
    //      int f = (a++)+(++a)+(a--)+(--a);
    //      System.out.println(f);//24
    //      System.out.println(s1+f+s2);
    //int f = ++a;//a++:a先参与运算,之后a自身加1
    //System.out.println("f="+f);
    //System.out.println("a="+a);
    System.out.println("---------------------------");
    System.out.println(c);
    System.out.println(d);
    System.out.println("5除以2的余数为:"+e);
    // 算术运算符--------------------------------end

}

}

/**

*规则:
*1.不同类型的变量进行运算最后得到的是位数最大的类型 int a = 1 float f = 3.14F double d = 12.90 double dd = a+f+d
*2.字符串与任何基本数据类型连接后,最终的结果都是字符串 System.out.println("hello"+a+d);

*/
@SuppressWarnings("unused")
public class Person {

public static void main(String[] args) {
    
    String name = "tom";
    char sex = 'M';
    int age = 30;
    String birthday = "1995-6-13";
    String school = "foreknow test";
    boolean marry = true;
    double salary = 2000.50;
    
    float fff = '1';
    System.out.println(fff);
    
    System.out.println(name);
    System.out.println(sex);
    System.out.println(age);
    System.out.println(birthday);
    System.out.println(school);
    System.out.println(marry);
    System.out.println(salary);
}

}
/**

*/
public class OperatorDemo {

public static void main(String[] args) {
    int x = 140;
    int a = 5;
    int b = 2;
    int c = a + b;
    int d = a / b;
    int e = a % b;
    System.out.println(d);
    String s1 = "aaa";
    String s2 = "bbb";
    
    System.out.println(x>20 && x<100);//20<x<100
    System.out.println(x>20 || x<100);
    
    boolean isRight = false;
    System.out.println("取反:"+!isRight);
    // 关系运算符--------------------------------start
    boolean b1 = a>b;
    boolean b2 = a<b;
    System.out.println(b1);//true
    System.out.println(b2);//false
    System.out.println(a>=b);//true
    System.out.println(a<=b);//false
    System.out.println(a==b);//false
    System.out.println(a!=b);//true
    // 关系运算符--------------------------------end
    
    // 赋值运算符--------------------------------start
    //      System.out.println(a+=b);
    //      System.out.println(a-=b);
    //      System.out.println(a*=b);
    //      System.out.println(a/=b);
    //      System.out.println(a%=b);
    // 赋值运算符--------------------------------end
    // 算术运算符--------------------------------start
    System.out.println("---------------------------");
    //算法:(a++)=5 a=6  (++a)=7 a=7   (a--)=7 a=6   (--a)=5 a=5
    //      int f = (a++)+(++a)+(a--)+(--a);
    //      System.out.println(f);//24
    //      System.out.println(s1+f+s2);
    //int f = ++a;//a++:a先参与运算,之后a自身加1
    //System.out.println("f="+f);
    //System.out.println("a="+a);
    System.out.println("---------------------------");
    System.out.println(c);
    System.out.println(d);
    System.out.println("5除以2的余数为:"+e);
    // 算术运算符--------------------------------end

}

}
**

*/
public class IFControlDemo {

public static void main(String[] args) {
    int x = 160;

// if(x>=60){
// System.out.println("太好了,及格了!!!!!");
// }
// System.out.println("我很爱学习java!!!!!!");

    System.out.println("---------------------------------------------");
    
    if(x>=60) {
        System.out.println("及格了!!!!!!");
    }else {
        System.out.println("不及格!!!!!!");
    }
    System.out.println("需要重修!!!!!!");
    
    System.out.println("---------------------------------------------");
    
    boolean b = true;
    if (b=false) {
        System.out.println("111111111111111");
    } else {
        System.out.println("222222222222222");
    }
    System.out.println(b);
    
    System.out.println("-----------------------------------------------");
    
    int y = 2;
    
    if(y<2){
        ++y;
    }
    else{
        y--;
    }
    System.out.println("y="+y);
    System.out.println("-------------------------------------------------");

// y=y<2?++y:y--;
// System.out.println("y="+y);

// int z=(y--);
// System.out.println(z);
// System.out.println(y);

}

}
/**

*/
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个整型数组,数组名为array并进行初始化
// int[] array = new int[5];
// //如何对数组进行赋值
// array[0] = 12;
// array[1] = 1;
//// array[2] = 3;
//// array[3] = 14;
//// array[4] = 5;
//
// System.out.println(array[4]);
//
// String[] s1 = new String[4];
// s1[0] = "tom0";
// s1[1] = "tom1";
// s1[2] = "tom2";
// s1[3] = "tom3";
// System.out.println(s1[3]);
//
// //静态初始化
// int[] array1 = new int[]{1,23,4,5,66,77,12};
// System.out.println(array1[1]);
// System.out.println("数组的长度为:"+array1.length);
//
// String[] s = {"java","c++","python","javascript","spring"};
// System.out.println("输出数组中的某一个元素:"+s[4]);
//
// System.out.println("------------------------------------------------");
// //如何将数组中的元素一次输出(对数组进行遍历),可以使用循环
// for(int i = 0;i<s.length;i++) {
// System.out.println(s[i]);
// }
//
// //练习:已知数组[2,14,68,15,3,66,5,35] 用户从控制台输入一个数并判断这个数在数组中是否存在
// //思路:
// //1.对数组进行遍历操作
// //2.每遍历一次需要判断用户输入的这个数与数组中的某一个元素是否相等,如果相等跳出循环
//
// Scanner input = new Scanner(System.in);
// boolean isRigth = false;
// System.out.println("请输入一个数:");
// int guess = input.nextInt();
// int[] arr = {2,14,68,15,3,66,5,35};
// for(int i = 0;i<arr.length;i++){
// if(guess==arr[i]) {
// isRigth = true;
// break;
// }
// }
// if(isRigth) {
// System.out.println("success......");
// }
//
// //已知数组[2,14,68,15,3,66,5,35] 求数组中所有数的奇数和以及偶数和
//
// //二维数组的定义以及初始化(数组的数组)
// int[][] arr1 = new int[3][4];
// arr1[0][0] = 12;
// arr1[0][1] = 13;
// arr1[0][2] = 14;
// arr1[0][3] = 15;
//
// arr1[1][0] = 11;
// arr1[1][1] = 12;
// arr1[1][2] = 13;
// arr1[1][3] = 14;
//
// arr1[2][0] = 22;
// arr1[2][1] = 23;
// arr1[2][2] = 24;
// arr1[2][3] = 25;
//
//
//
// int[][] arr2 = {{1,2,3},{4,5,6},{7,8,9}};
// System.out.println(arr2[0][1]);
//
// int[][] a = new int[3][ ];
// a[0] = new int[2];
// a[1] = new int[3];
// a[2] = new int[4];

    // System.out.println(a[0][0]);
    int[][] a = { { 1 }, { 4, 5, 6 }, { 7, 8 } };
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) {
            System.out.print(a[i][j] + " ");
        }
        System.out.println();
    }

// [3,1,4,67,13,12,2]
}
}

/**

*/
public class FunctionDemo {

public static void eat(String tools){
    System.out.println("我们可以使用"+tools+"来吃饭!!!");
}

public static void sleep() {
    System.out.println("sleep......");
}

/**
 * 定义一个登录的方法
 * 思考:是否需要参数(username,password)
 * @param args
 */
public static void login(String username,String password){
    System.out.println("用户名:"+username+"密码:"+password);
}

public static void cacul() {
    int[] array = {1,2,3,4,5,6,7};
    for(int i = 0;i<array.length;i++){
        System.out.println(array[i]);
    }
}

/**
 * 用户从控制台输入两个数并计算两个数之和
 * 用函数来实现
 * @param args
 */
public static void sum(int a,int b) {
    int sum = a+b;
    System.out.println(sum);
}



/**
 * 计算三个数之和
 * @param a 输入参数
 * @param b 输入参数
 * @param c 输入参数
 * @return sum 和
 */
public static int total(int a,int b,int c) {
    int sum = a+b+c;
    return sum;
}

public static void main(String[] args) {
    int sum = FunctionDemo.total(1, 2, 3);
    int fol = sum+100;
    System.out.println(sum);
    
    System.out.println("-----------------------------------------");
    //函数的调用
    FunctionDemo.eat("筷子");
    FunctionDemo.sleep();
    FunctionDemo.login("tom","123456");
    FunctionDemo.cacul();
    
    Scanner intput = new Scanner(System.in);
    
    System.out.println("请输入第一个数:");
    int num1 = intput.nextInt();
    System.out.println("请输入第二个数:");
    int num2 = intput.nextInt();
    
    //函数的调用
    FunctionDemo.sum(num1, num2);
    
    
}

}
/**

*/
public class FunctionDemo2 {
public static int f1(int a, int b) {
return a + b;
}

public static String f2(String username, String password) {
    int sum = f1(1, 2);
    String school = f3("forknow");
    return username + "  " + password + sum + school;
}

public static String f3(String school) {
    return school;
}

}

/**

*/
public class PopSort {

// public static void pop() {
// int[] array = {3,1,16,22,11,4,5,7};
// for(int i = 0;i<array.length;i++) {
// //相邻的两个元素比较
// for(int j = 0;j<array.length-1;j++) {
// if(array[j]>array[j+1]) {
// int temp = array[j];
// array[j] = array[j+1];
// array[j+1] = temp;
// }
// }
// }
// //对排序后的结果输出
// for(int i = 0;i<array.length;i++) {
// System.out.println(array[i]);
// }
// }

// public static void pop(int[] array){
// for(int i = 0;i<array.length;i++) {
// //相邻的两个元素比较
// for(int j = 0;j<array.length-1;j++) {
// if(array[j]>array[j+1]) {
// int temp = array[j];
// array[j] = array[j+1];
// array[j+1] = temp;
// }
// }
// }
// //对排序后的结果输出
// for(int i = 0;i<array.length;i++) {
// System.out.println(array[i]);
// }
// }

public static int[] pop(int[] array){
    for(int i = 0;i<array.length;i++) {
    //相邻的两个元素比较
    for(int j = 0;j<array.length-1;j++) {
        if(array[j]>array[j+1]) {
            int temp = array[j];
            array[j] = array[j+1];
            array[j+1] = temp;
        }
    }
}
    return array;
}

public static void main(String[] args) {

// PopSort.pop();
int[] array = {3,1,16,22,11,4,5,7};
int[] a = PopSort.pop(array);
//对排序后的结果输出
for(int i = 0;i<a.length;i++) {
System.out.println(a[i]);
}
}
}

/**

*/
public class ConstructorDemo {
String name;
int age;
String school;
User user;

//初始化变量
public ConstructorDemo(String name,int age,String school){
            //this表示当前对象(ConstructorDemo)自身
            this.name=name;
            this.age = age;
            this.school = school;
            user = new User();
            //user.setEmail("zjjlive@163.com");
    
}

// public ConstructorDemo() {
// System.out.println("这是一个默认的构造函数......");
// }

public void method() {
    System.out.println("method......");
}

public static void main(String[] args) {
    ConstructorDemo cDemo = new ConstructorDemo("tom",30,"foreknow");   
}

}

/**

/
public class ConstructorDemo2 {
public ConstructorDemo2() {
System.out.println("D......................");
}
public ConstructorDemo2(int a) {
this();
System.out.println("A......................");
}
public ConstructorDemo2(int a,int b) {
this(a);
System.out.println("B......................");
}
public ConstructorDemo2(int a,int b,int c) {
this(a, b);
System.out.println("C......................");
}
public static void main(String[] args) {
ConstructorDemo2 cDemo2 = new ConstructorDemo2();
}
}
/
*

*/
public class OverLoadDemo {
public int sum(int a,int b) {
int c = a+b;
return c;
}
public void sum(int a,int b,int c) {
int d = a+b+c;
System.out.println(d);
}
public static void main(String[] args) {
OverLoadDemo oDemo = new OverLoadDemo();
int s = oDemo.sum(1, 2);
System.out.println(s);

    oDemo.sum(3, 4, 5);
}

}

**

*/
public class SayHello {

public String say() {
    return "say";
}

public void abc() {
    System.out.println("abc......");
}

public String method() {

// SayHello sayHello =new SayHello();
// String ss = sayHello.say();

    return new SayHello().say();
}

public static void main(String[] args) {
    SayHello sHello = new SayHello();
    sHello.say();
    sHello.abc();
    
    //匿名對象
    new SayHello().say();
    new SayHello().abc();
}

}
/**

*/
public class StaticDemo {
String name;//成员变量(实例变量)
static int a;//静态变量
public StaticDemo() {
a++;
}

//静态方法
public static void method() {
    System.out.println("static method......");
}

//实例方法(成员方法)
public void test() {
    System.out.println("test.....");
}

public static void main(String[] args) {
    //调用静态方法
    StaticDemo.method();
    
    StaticDemo s1 = new StaticDemo();
    System.out.println(s1.name);
    System.out.println(StaticDemo.a);
    StaticDemo s2 = new StaticDemo();
    System.out.println(s2.name);
    System.out.println(StaticDemo.a);
    StaticDemo s3 = new StaticDemo();
    System.out.println(s3.name);
    System.out.println(StaticDemo.a);
    StaticDemo s4 = new StaticDemo();
    System.out.println(s4.name);
    System.out.println(StaticDemo.a);
}

}
/**

*/
public class Student {

int sid;//学号
String name;//姓名
String school;//所在学校
int age;//年龄

public void eat() {
    System.out.println("eat......");
}

public void sleep() {
    System.out.println("sleep......");
}

public void study() {
    System.out.println("study......");
}

public static void main(String[] args) {
    //如何创建对象
    Student s1 = new Student(); 
    s1.sid = 1000;
    s1.name = "tom";
    s1.age = 20;
    s1.school = "neusoft";
    s1.eat();
    s1.sleep();
    s1.study();
    System.out.println(s1.sid+"--"+s1.name+"--"+s1.age+"--"+s1.school);
    Student s2 = new Student(); 
    s2.sid = 1001;
    s2.name = "jazz";
    s2.age = 30;
    s2.school = "neusoft";
    s2.eat();
    s2.sleep();
    s2.study();
    System.out.println(s2.sid+"--"+s2.name+"--"+s2.age+"--"+s2.school);
}   

}
/**

*/
public class User {
private String email;
private String password;
private String confirm_pass;
private String require_code;
private String phone;
public User() {
}
public User(String email,String password,String confirm_pass) {
this.email = email;
}
//获取email的属性值
public String getEmail() {
return email;
}
//可以给User对象的属性email进行初始化(设置一个值)
public void setEmail(String email) {
this.email = email;
}
}

上一篇下一篇

猜你喜欢

热点阅读