### 使用循环语句划分两个数字

``````System.out.print("Enter Divident: ");
System.out.print("Enter Divisor: ");
``````

``````  Enter Dividend: 25
Enter Divisor 5
5
``````

#### 6 个答案:

`````` int c=0;
while(ans1 >= ans2){
ans1 -= ans2;
c++;
}
``````

`````` boolean n1 = (ans1 & (1<<31))!=0;
boolean n2 = (ans2 & (1<<31))!=0;
ans1 = Math.abs(ans1);
ans2 = Math.abs(ans2);

int c=0;
while(ans1 >= ans2){
ans1 -= ans2;
c++;
}
if(!n1 && n2) c = -c;
else if(n1 && !n2){
c = -c;
if(ans1 > 0){
ans1 = ans2 - ans1;
c--;
}
}else if(n1 && n2){
if(ans1 > 0){
ans1 = ans2 - ans1;
c++;
}
}
``````

``````//  Calculate: a / b
public int divide (int a, int b) {
if ( a < b ) {
return 0;
} else {
return 1 + divide ( a - b, b );
}
}
``````

BigInteger使用`/`运算符完成这一操作。

`(new BigInteger(ans1 + "")).divide(new BigInteger(ans2 + ""))`

``````numtimes init at 0
counter init at ans1
while counter is greater than ans2
subtract ans2 from counter
numtimes increase by 1

check numtimes
``````

``````System.out.print("Enter Divident: ");
System.out.print("Enter Divisor: ");
int count=0;
while(ans1>=ans2)
{
ans1=ans1-ans2;
count++;
}
System.out.println(count);
``````

``````int i=1;
int mul;
while(true)
{
mul = i++;
if(mul*(ans2)==ans1)
{
System.out.println(mul);
break;
}
else if(mul*(ans2)>ans1)
{
System.out.println("Cannot be divided");
break;
}
}
``````