1.输出“hello world,this is my first program”
public class HelloWorld {
public static void main(String[] args){
System.out.println("hello world,this is my first program!");
}
}
结果
hello world,this is my first program!
2.冒泡排序
import java.util.Random;//引入随机数
public class SortNum {
public static void main(String[] args) {
int[] arr = new int[10];//定义一个有10个数字的数组
Random rm = new Random();//初始化随机数的对象
System.out.print("[");
for (int i = 0; i < 10; i++) {
arr[i] = rm.nextInt(100);//从100以内随机选择一个数字
System.out.print(arr[i] + ",");//打印输出数组第i个数字的值
}
System.out.print("]\n");
int temp = 0;//临时变量
int len = arr.length;//数组的长度
//从右向左遍历,默认每次最右边的数字都是排序结束的数字
for(int j = len ;j>0;j--) {
//从左向右遍历,将未排序的数组中最大数字运送到剩余数组的最右边
for(int i = 0;i<j-1;i++) {
if (arr[i] > arr[i+1]) {//如果左边的值比右边的值大,则交换两者的位置
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
//每次循环结束,都有一个最大的数字被运送到右侧,按大小从右向左排序
}
//打印输出排序后的数组
System.out.print("[");
for(int i:arr) {
System.out.print(i+",");
}
System.out.print("]");
}
}
结果
[4,84,44,74,92,72,8,97,15,20,]
[4,8,15,20,44,72,74,84,92,97,]
3.IP地址转换
public class IPConvert {
public static long ip2Long(String strIP){
String[]ip = strIP.split("\\.");
return (Long.parseLong(ip[0])<<24)+(Long.parseLong(ip[1])<<16)+(Long.parseLong(ip[2])<<8)+Long.parseLong(ip[3]);
}
public static String long2IP(long longIP){
StringBuilder sb = new StringBuilder("");
sb.append(String.valueOf((longIP>>>24)));
sb.append(".");
sb.append(String.valueOf((longIP & 0x00FFFFFF)>>>16));
sb.append(".");
sb.append(String.valueOf((longIP & 0x0000FFFF)>>>8));
sb.append(".");
sb.append(String.valueOf((longIP & 0x000000FF)));
return sb.toString();
}
public static void main(String[]args){
System.out.println(ip2Long("219.239.110.138"));
System.out.println(long2IP(3121201111L));
}
}
4.贷款期限
public class LoanRate {
public double getInterestRate (String term,double floatScale){
//获取最后一位字符,Y代表年,M代表月,D代表天
String type = term.substring(term.length()-1);
//获取对应的贷款期限
int terms = Integer.parseInt(term.substring(0,(term.length()-1)));
double loanRate = 0.0;
//短期期限1年以内,基础利率4.38
if (!"Y".equals(type)){
loanRate = getShortBase()*(1+floatScale);
System.out.println("贷款期限是"+term+",根据基准利率"+ getShortBase()
+ "和浮动比例"+floatScale+",计算出来的贷款利率是:"+ loanRate);
}else {
if (5>terms){
//中期期限为1~5年,基础理论为4.75
loanRate = getMidBase()*(1+floatScale);
System.out.println("贷款期限是"+ term+",根据基准利率"+ getMidBase()+
"和浮动比例"+floatScale+",计算出来的贷款利率是:"+loanRate);
}else {
//长期期限为5年以上,基础理论为5.25
loanRate = getLongBase()*(1+floatScale);
System.out.println("贷款期限是"+ term+",根据基准利率"+ getLongBase()+
"和浮动比例"+floatScale+",计算出来的贷款利率是:"+loanRate);
}
}
//返回贷款利率 基准利率*(1+上浮比例)
return loanRate;
}
//短期基准利率
public double getShortBase(){
return 4.38;
}
//中期基准利率
public double getMidBase(){
return 4.75;
}
//长期基准利率
public double getLongBase(){
return 5.25;
}
public static void main(String[]args){
LoanRate Ir = new LoanRate();//初始化利率计算类的对象
double loanRateShort = Ir.getInterestRate("8M",0.7);//短期利率
double loanRateMid = Ir.getInterestRate("4Y",0.7);//中期利率
double loanRateLong = Ir.getInterestRate("6Y",0.6);//长期利率
}
}
结果
贷款期限是8M,根据基准利率4.38和浮动比例0.7,计算出来的贷款利率是:7.446
贷款期限是4Y,根据基准利率4.75和浮动比例0.7,计算出来的贷款利率是:8.075
贷款期限是6Y,根据基准利率5.25和浮动比例0.6,计算出来的贷款利率是:8.4
5.打印
public class PeopleDemo {
void print(int i) {
System.out.println("打印整型:"+i);
}
void print(String s){
System.out.println("打印字符串类型值:"+s);
}
void print(String s1,String s2){
System.out.println("打印字符串类型值1:"+s1+";字符串类型值2:"+s2);
}
public static void main(String[] args){
PeopleDemo owd =new PeopleDemo();
owd.print(12);
owd.print("1234");
owd.print("字符串1","字符串2");
}
}
结果
打印整型:12
打印字符串类型值:1234
打印字符串类型值1:字符串1;字符串类型值2:字符串2