仅通过减去和找到余数来划分 - 不使用除法运算符

时间:2014-09-16 01:25:47

标签: java

我似乎已经能够找出第一个与乘法相提并论,但我的逻辑在试图找出余数时有点困惑。当我运行它时,我只是得到一个负数,而不是它看起来像分裂 - 任何帮助将不胜感激!

主Java类

import java.util.Scanner;

public class assignment4 {
public static void main(String args[]) {
    Scanner inputReader = new Scanner(System.in);

    int multiplicand, multiplier;
    int dividend, divisor;
    int total=0, total2 = 0, countMult, countDiv;

    System.out.print("Enter a multiplican: ");
    multiplicand = inputReader.nextInt();
    System.out.print("Enter a multiplier: ");
    multiplier = inputReader.nextInt();

    countMult = multiplier;
    while(multiplier > 0) {

        total = total + multiplicand;
        multiplier--;
    }

    System.out.println(multiplicand + " times " + countMult + " equals " + total );

    System.out.print("Enter a dividend: ");
    dividend = inputReader.nextInt();
    System.out.print("Enter a divisor: ");
    divisor = inputReader.nextInt();

    countDiv = divisor;
    while(divisor > 0) {

        total2 = total2 - dividend;
        divisor--;
    }

    System.out.print(dividend + " divided by " + countDiv + " equals " + total2 );


}

}

0 个答案:

没有答案