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 + "\tc = " + c);
		System.out.println("d = " + d + "\te = " + e + "\tf = " + 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);
	}
}
时间: 2024-11-09 20:33:26

java新手笔记5的相关文章

java新手笔记33 多线程、客户端、服务器

1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = false; //跳出方法 同步锁 public synchronized void jump() { while(true) { if(!isLive ) { System.out.println("跳出第 " + index + " 田鼠"); //修改田鼠状态

java新手笔记34 连接数据库

1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcUtil { private static final String driver = "sun.j

java新手笔记10 构造器

1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java.util.Random; public class Demo1 { /** * 模拟摇奖 */ public static void main(String[] args) { Random ran = new Random(); int[] a = new int[7]; System.out.p

java新手笔记15 多态

1.Animal类 package com.yfs.javase; public class Animal { public void cry() { System.out.println("动物叫..."); } } 2.Dog/Cat/Bird类 package com.yfs.javase; public class Dog extends Animal { public void cry() { System.out.println("汪 汪..."); }

java新手笔记1

//Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { System.out.println("Hello World!"); } } //编译命令 C:\>javac C:\Users\Administrator\Desktop\Hello.java //javac 路径+文件名.java //javac -d D:\ C:\Users\

java新手笔记16 面积

1.图形类 package com.yfs.javase; public class Shape { //计算面积方法 public double getArea() { System.out.println("计算面积"); return 0; } } 2.圆 package com.yfs.javase; public class Circle extends Shape { private double r; public Circle(double r) { this.r =

java新手笔记9

1.bank类 package com.yfs.javase; //类封装 public class BankCard { //属性 int balance;//默认0 实例变量 分配给每个对象一个 //String name; // 方法 存钱 public void saveMoney(int money) { if(money < 0) { System.out.println("非法操作..."); return; } System.out.println("向

java新手笔记2

1.注释 /** doc注释 * 类说明信息 */ //声明类 文件名与类名一致 public class World {//类定界符 //声明方法 main方法 public static void main(String[] args) { System.out.println("World World!"); //System.out.println("welcome java world!"); //注释的代码不执行 单行注释 /* 多行注释 System.

java新手笔记3

1.包 2.运算符 public class Operator { public static void main(String[] args) { int a = 5; System.out.println("a = " + a); //a = -a; //+ - System.out.println("a = " + a); //+ 字符串链接 System.out.println("影分身" + "软件开发"); Sys

java新手笔记23 异常

1.import package com.yfs.javase; import java.util.Scanner; //import java.lang.String;//默认导入 public class Demo1 { public static void main(String[] args) { String s = new String("abc");//java.lang.String String s1 = "abc"; System.out.pri