zoukankan      html  css  js  c++  java
  • java新手笔记5 类

    1.进制转换

    /*
    企业发放的奖金根据利润提成。
    利润(I)低于或等于10万元时,奖金可提10%;
    利润高于10万元,低于20万元时,
    低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
    20万到40万之间时,高于20万元的部分,可提成5%;
    40万到60万之间时高于40万元的部分,可提成3%;
    60万到100万之间时,高于60万元的部分,可提成1.5%,
    高于100万元时,超过100万元的部分按1%提成,
    从键盘输入当月利润I,求应发放奖金总数?
    */
    import java.util.Scanner;
    public class Demo1 {
    
    	public static void main(String args[]) {
    		/*
    		System.out.println("二进制:4 " +Integer.toBinaryString(4));
    		System.out.println("八进制:16 " +Integer.toOctalString(16)); 
    		System.out.println("十六进制:10 " + Integer.toHexString(10));
    		int a = 12328;
    		int b,c,d,e,f;
    		//取个位
    		b = a % 10;
    		//c = a % 100 / 10;
    		c = a / 10 % 10;
    		d = a / 100 % 10;
    		e = a / 1000 % 10;
    		f = a / 10000;
    		System.out.println("b = " + b + "	c = " + c);
    		System.out.println("d = " + d + "	e = " + e + "	f = " + f);
    		
    		int a = 462,b,c,d,e,f;
    		if(a < 10){
               System.out.println("1位数..."); 
    		} else if ( a < 100 ) {
    		   b = a % 10;
    		   c = a / 10 % 10;
               System.out.println("2位数..." + b + c);
    		} else if ( a < 1000 ) {
    		   b = a % 10;
    		   c = a / 10 % 10;
    		   d = a / 100 % 10;
               System.out.println("3位数..." + b + c + d); 
    		} else if ( a < 10000 ) {
    		   b = a % 10;
    		   c = a / 10 % 10;
    		   d = a / 100 % 10;
    		   e = a / 1000 % 10;
               System.out.println("4位数..." + b + c + d + e); 
    		} else {
    		   b = a % 10;
    		   c = a / 10 % 10;
    		   d = a / 100 % 10;
    		   e = a / 1000 % 10;
    		   f = a / 10000;
               System.out.println("5位数..." + b + c + d + e + f); 
    		}
    		
    		Scanner scan = new Scanner(System.in);
    		System.out.println("请输入你信息姓名、年龄、成绩:");
    		String name = scan.next();//获取字符串
    		int age = scan.nextInt();//获取整型数字
    		double score = scan.nextDouble();
    		System.out.println("输入的信息如下:");
    		System.out.println("姓名:" + name + " 年龄:" + age + " 成绩:" + score);
    		
    		Scanner scan = new Scanner(System.in);
    		System.out.println("请输入利润:");
    		double I = scan.nextDouble();
            double bound = 0;
    		//奖金方案
    		if(I <= 10) {
    			bound = I * 0.1;
    		} else if( I < 20 ) {
    			bound = 10 * 0.1 + (I - 10) * 0.075; // 35  10   10 0.075
    		} else if( I < 40 ) {
    			bound = 10 * 0.1 + 10 * 0.075 + (I - 20) * 0.05; 
    		} else if( I < 60 ) {
    			bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (I - 40) * 0.03; 
    		} else if( I < 100 ) {
    			bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (I - 60) * 0.015; 
    		} else {
    			bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (I - 100) * 0.01; 
    		}
    		System.out.println("奖金:" + bound);
    		//String s = NULL;
    		*/
    		int a = 1, b = 6, c = 9;
    		if(a > b){
    			if(a > c){
    				if(b > c){ 
    					System.out.println("@:" + a + b + c);
    				} else {
    					System.out.println(" :" + a + c + b);
    				}
    			} else {
                      System.out.println(" :" + c + a + b);
    			}
    		} else {
    			if(b > c) {
    				if( a > c){
    				   System.out.println(" :" + b + a + c);
    				} else {
                       System.out.println(" :" + b + c + a);
    				}
    
    			} else {
                   System.out.println("** :" + c + b + a);
    			}
    
    
    		}
    
    		int year = 2012;
    		boolean isLeapYear = year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
    
    	}
    }
    

     2.封装类

    //声明类  类封装
    public class Student {
        
    	String name;
    	char sex;
    	private int age;//属性私有
    	int sno;
        //提供访问属性的方法  通过方法防止非法数据存入属性
    	void setAge() {
    		age = 30;
    		if(age > 150 || age < 0){
               System.out.println("年龄不合法...");
    		   age = 0;
    		}
    		
             
    	}
    
    	int getAge() {
            return age; //返回age属性值
    	}
    
    	void study() {
    		System.out.println("学习!...");
    	}
    
    	//方法
    	void speak() {
    		System.out.println("大家好!...");
    	}
    	
    }
    

     3.测试类

    //声明类
    public class StudentTest {
    	public static void main(String[] args){
    		Student o = new Student();//创建自定义对象
    		//o 学生对象   Student 类
    
             int a = 20; 
    
    		System.out.println("o.name = " + o.name);
    		o.speak();//方法调用
    		o.study();
    
    		o.name = "张三";
    		o.sex = '男';
    		//o.age = -20;
    		o.setAge();
    		o.sno = 180;
    
    		o.name = "张飞";
    		System.out.println("o.name = " + o.name);
    		System.out.println("o.sex = " + o.sex);
    		//System.out.println("o.age = " + o.age);
    		System.out.println("o.getAge() = " + o.getAge());
    		System.out.println("o.sno = " + o.sno);
    
    	}
    }
    

     4.测试类1

    //声明类
    public class StudentTest1 {
    	public static void main(String[] args){
    		Student s = null;//声明对象的引用
    		//s.study();  new 
    		s = new Student();//new 创建对象 实例化对象
    		s.name = "李四";
    		System.out.println("s.name = " + s.name);
    		s.study();
    
    		Student s2 = new Student();
    		s2.name = "张三";
    		System.out.println("s2.name = " + s2.name);
    		System.out.println("s.name = " + s.name);
    		s2.study();
    
    	}
    }
    

     5.类说明

    //声明类
    public class Obj {
        
    	//状态  属性 实例变量
    	String name;
    	char sex;
    	int age;
    
    	//行为 方法  动作
    	void speak() {
    		System.out.println("大家好!...");
    	}
    	
    }
    //////////////////////////////////
    class ObjTest {
    	public static void main(String[] args){
    		Obj o = new Obj();//创建自定义对象
    		//Random ran = new Random();
    
    		System.out.println("o.name = " + o.name);
    		o.speak();
    		o.name = "张三";
    		o.sex = '男';
    		o.age = 20;
    
    		o.name = "张飞";
    		System.out.println("o.name = " + o.name);
    		System.out.println("o.sex = " + o.sex);
    		System.out.println("o.age = " + o.age);
    
    	}
    }
    

     6.类方法

    //声明类
    public class MethodTest1 {
    
        int a = 10;//与对象关联
        //方法  无参数 无返回值
    	void method1() {
    		System.out.println("call method1()");
    	}
        //返回值类型 add  方法名 ()传入参数 
    	void add (int a, int b) {
            int sum  = a + b;
            System.out.println("sum = " +  sum);
    	}
        //形参列表 数据类型 变量名
    	double caluSal(int job,int house,int bound) {
    		//System.out.println("sum = " +  job + house + bound);
          return job + house + bound;//  return返回计算结果
    	}
    
    	public static void main(String[] args){
    	   MethodTest1 mt = new MethodTest1();
           int b = mt.a;
    	   System.out.println("b = " + b);
    	   mt.method1();
    	   mt.add(10, 6);//调用时 参数类型 个数与声明的方法匹配
    
    	   double salary = mt.caluSal(8000,5000,6000);
    
    	   System.out.println("salary = " + salary);
    	   //结果返回还需要处理
    	   double result = salary * 0.05;
    
    		System.out.println("result = " + result);
    	}
    }
    
  • 相关阅读:
    pycharm2017.3.7的安装
    SVN Working Copy xxx locked 并 cleanup失败之解
    Lua查找字符串注意
    Lua运算符
    Python操作Excel
    项目中碰到的Ajax相关的问题记录
    线程的相关概念
    val()用法和value的区别
    JSON数组对象和JSON字符串的转化,map和JSON对象之间的转化
    算法复杂度相关概念
  • 原文地址:https://www.cnblogs.com/feilongblog/p/4656744.html
Copyright © 2011-2022 走看看