Home > Back-end >  i can not find the error , it doesn't stop expecting new numbers rather than giving me the resu
i can not find the error , it doesn't stop expecting new numbers rather than giving me the resu

Time:11-14

public class Division {
    private int a , b , i , d;
    public Division(int a, int b){
        this.a=a;
        this.b=b;

    }



    public void division (){
        if(a<b){
            d=a;
            a=b;
            b=d;
        }
        d=0;
        if(b==0)
            System.out.println("La division est impossible par 0!!");
        else{
            do{
                i=a-b;
                d  ;
            }while(i>b);
            System.out.println("Le quotion est " d " et le reste est " i);
        }

    }
}

principal function

Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
//
Division obj = new Division(a,b);
obj.division();

i tried to run the code ;( the first part ia class named Division , the second is the principal function) and then when i try to enter the two numbres (a,b) it doesnt stop from expecting new numbers . so the programm does not end , and does not give me result .

CodePudding user response:

 do{
    i=a-b;
    d  ;
 }while(i>b);

as u update the value of d in the loop, but check condation as i (always a-b) and b (always b) will lead a infinte loop. suggext

do{
    i=a-b;
    a=i; //update the dividend 
    d  ;
}while(i>b);

CodePudding user response:

The Error Is In Division Class -> division Method -> else -> Do While LOOP
Yes, Error In In DO WHILE Loop,
if value a is 13 and b is 5
Then in Do While Value Of i Is a-b = 13-5 = 8
and in every loop, i value is set to 8, not any other value,
To Fix This, Before The Loop set i = a then replace i=a-b; to i=i-b;
It Will Fix This Error

  • Related