当前位置: 首页 > >

java两整数相除_Java实现两个整数相除 | 学步园

发布时间:

public class DeleTwoNum {


public static void main(String[] args){


int a = 7;


int b = 300;


int c = 5;


System.out.println("a/b=" + delTwoNum(a, b, c));


}


/**


* 得到两个整数相除的结果


* @param a 被除数


* @param b 除数


* @param c 精度,小数点位数


* @return 相除的结果


*


*/


private static float delTwoNum(int a, int b, int c){


if(b == 0){


throw new RuntimeException();


}


if(a == 0){


return 0;


}


int prePart = getPrePart(a, b);


String aftPart = getAftPart(a, b, c);


return Float.valueOf(prePart + "." + aftPart);


}


//获取小数部分


private static String getAftPart(int a, int b, int c) {


if(c == 0){


return "";


}


while(a >= b){


a = a - b;


}


if(a == 0){


return "0";


}


String aft = "";


int sum = 0;


for(int i = 0; i < 10; i++){


sum += a;


}


a = sum;


aft = aft + getPrePart(a, b) + getAftPart(a, b, c - 1);


return aft;


}


//获取整数部分


private static int getPrePart(int a, int b) {


if(a < b){


return 0;


}else{


int count = 0;


while(a >= b){


a = a - b;


count++;


}


return count;


}


}


}



友情链接: