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