首页 > 代码库 > Java基础

Java基础

package liping.com;

import java.util.Date;
import java.io.*;

public class HelloWorld {

    public String name;
    public String sex;
    public int age;
    private double salary;
    public void sleep(){
        age = age +25;
        System.out.println("人类啥都吃");
        System.out.println("你的年纪是:"+age);
    }
    public void setNmae(String name1){
        name = name1;    
    }
    public void  setSalary(double salary1){
        salary = salary1;
    }
    public void printInf(){
        System.out.println("名字:"+name);
        System.out.println("薪水:"+salary);
    }
    public static int max(int num1,int num2){
        int result;
        if(num1 > num2){
            result = num1;
        }else{
            result = num2;
        }
        return result;
    }
    
    public static void main(String[] args) throws IOException {
//类和对象
        HelloWorld xiaoming=new HelloWorld();
        xiaoming.setNmae("小明");
        xiaoming.setSalary(10000);
        xiaoming.sleep();
        xiaoming.printInf();
        
        int larger = max(888,999);
        System.out.println("比较大的一个值是:"+larger);
        
//基本数据类型的基本情况打印        
        System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE);
        System.out.println("包装类:java.lpng,integer");
        System.out.println("最小值:interger.Min_value="http://www.mamicode.com/+Byte.MIN_VALUE);
        System.out.println("最大值:interger.Max_value="http://www.mamicode.com/+Byte.MAX_VALUE);
//数据类型的强制转换        
        char c1 = ‘a‘;
        int i1 =c1;
        System.out.println("char类型转换为int后的值:"+i1);
        char c2 = ‘A‘;
        int i2 = c2 +1;
        System.out.println("char类型转化为后的值:"+i2);
        byte b = (byte)c1;
        System.out.println("char强转之后的值:"+b);
//位运算符
        int a3 = 60; /* 60 = 0011 1100 */
        int b3 = 13; /* 13 = 0000 1101 */
        int c3 = 0;  
        c3 = a3 & b3;/* 12 = 0000 1100 */// 如果相对应位都是1,则结果为1,否则为0
        System.out.println("a3 & b3 = " +c3);
        c3 = a3 | b3;/* 61 = 0011 1101 */// 如果相对应位都是0,则结果为0,否则为1
         System.out.println("a3 | b3 = " + c3 );
         c3 = a3 ^ b3;/* 49 = 0011 0001 */// 如果相对应位值相同,则结果为0,否则为1
         System.out.println("a3 ^ b3 = " + c3 );
         c3 = ~a3;   /*-61 = 1100 0011 */// 按位补运算符翻转操作数的每一位,即0变成1,1变成0。
         System.out.println("~a3 = " + c3 );
         c3 = a3 << 2; /* 240 = 1111 0000 *///按位左移运算符。左操作数按位左移右操作数指定的位数。
         System.out.println("a3 << 2 = " + c3 );
         c3 = a3 >> 2; /* 15 = 1111 */// 按位右移运算符。左操作数按位右移右操作数指定的位数。
         System.out.println("a3 >> 2  = " + c3 );
         c3 = a3 >>> 2; /* 15 = 0000 1111 *///按位右移补零操作符。左操作数的值按右操作数指定的位数右移,移动得到的空位以零填充
         System.out.println("a3 >>> 2 = " + c3 );
//循环
         int  x = 10;
         while (x<20){
         System.out.println("Volue of x :" + x);
         x++;
         System.out.println("\n"); 
         }
         
         do {
             System.out.println("Volue of x :" + x);
             x++; 
         }while(x < 20);
         
         for(int x1 = 10; x1<20; x1++){
             System.out.println("value of x1:"+x1);
             System.out.println("\n");
         }
         
         String [] names = {"科比","詹姆斯","库里","杜兰特"};
         for( String name : names ) {
             if(name.equals("库里")){
                 break;
             }
             System.out.print( name );
             System.out.print(",\n");
                  } 
//分支结构             
        int x2 = 888;
        if(x2 == 10){
            System.out.println("value is 10");
        }else if(x2 == 50){
            System.out.println( "value is 50");
        }else{
            System.out.println("我也不知道x2的值是多少");
        }
        
        char grade = ‘C‘;
          switch(grade)
          {
             case ‘A‘ :
                System.out.println("优秀"); 
                break;
             case ‘B‘ :
                 System.out.println("良好"); 
                    break;
             case ‘C‘ :
                System.out.println("中等");
                break;
             case ‘D‘ :
                System.out.println("及格");
             case ‘F‘ :
                System.out.println("你需要再努力努力");
                break;
             default :
                System.out.println("未知等级");
          }
          System.out.println("你的等级是 " + grade);
//Java Math类
          System.out.println("90度的正玄值:"+Math.sin(Math.PI/2));
          System.out.println("0度的余玄值:"+Math.cos(0));
          System.out.println("60度的正切值:"+Math.tan(Math.PI/3));
          System.out.println("1的反切值:"+Math.atan(1));
          System.out.println("n/2的角度值:"+Math.toDegrees(Math.PI/2));
          System.out.println(Math.PI); 
//Java StringBuffer 和 StringBuilder 类  
          StringBuffer sBuffer = new StringBuffer("菜鸟教程官网:");
          sBuffer.append("www.runoob.com");
          System.out.println(sBuffer);
//数组
          double[] myList = {1.9, 2.9, 3.4, 3.5};  
          // 打印所有数组元素
          for (int i = 0; i < myList.length; i++) {
             System.out.println(myList[i] + " ");
          }
          // 计算所有元素的总和
          double total = 0;
          for (int i = 0; i < myList.length; i++) {
             total += myList[i];
          }
          System.out.println("Total is " + total);
          // 查找最大元素
          double max = myList[0];
          for (int i = 1; i < myList.length; i++) {
             if (myList[i] > max) max = myList[i];
          }
          System.out.println("Max is " + max);
          
         Date date = new Date();
         System.out.println(date.toString());
         String str = String.format("Current Date/Time : %tc", date );
         System.out.println(str);
         System.out.printf("%s %tB %<te, %<tY", 
                 "Due date:", date);
         
         char scanf;
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("s输入字符,按下‘q’键退出");
        do{
            scanf = (char) br.read();
            System.out.println(scanf);
        }while(scanf != ‘q‘);
          
}

}

 

Java基础