简体   繁体   English

为什么我的程序无法打印出正确的余数?

[英]Why won't my program print out the correct remainder?

I need help so bad. 我需要的帮助太糟糕了。 I've been working on this homework assignment for 2 hours and i can't get my logic straight. 我已经在这个家庭作业中工作了2个小时,我无法弄清我的逻辑。 We are not allowed to use the % or / operators but we must divide and find the remainder just by subtracting- my program prints out the division part but not the remainder. 我们不允许使用%或/运算符,但我们必须除法并找到余数,只需减去-我的程序会打印出除法部分,而不是余数。 Any clues guys? 有什么线索吗? I'm learning and i know it's a small error but i can't figure it out. 我正在学习,我知道这是一个小错误,但我无法弄清楚。 I've checked all of the other questions that are similar but they weren't of any help. 我检查了所有其他类似的问题,但它们没有任何帮助。 Thank you all. 谢谢你们。

this is my program so far 这是我到目前为止的程序

import java.util.Scanner;

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

    int multiplicand, multiplier;
    double dividend, divisor, countDivisor, countDividend, remainder;
    int total=0, total2 = 0, countMult;

    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();

    countDivisor = divisor;
    countDividend = dividend;
    while(dividend >= 0) {

        dividend = dividend - divisor;
        divisor--;
    }

    remainder =  divisor - 1;
    System.out.print(countDividend + " divided by " + countDivisor + " equeals " + divisor + " with a remainder of " + remainder);
}

}

your while loop is iterating while the dividend is >=0 while loop红利> = 0时,您的while loop正在迭代

example dividend = 10 and divisor = 3 例如,股息= 10,除数= 3

looping 循环播放

10 - 3 = 7 // >= 0 true 10-3 = 7 //> = 0是
7 - 3 = 4 // >= 0 true 7-3 = 4 //> = 0是
4 - 3 = 1 // >= 0 true it should stop here because dividend is < divisor 4-3 = 1 //> = 0是,它应在此处停止,因为股息为<除数
1 - 3 = -2 // >= 0 false 1-3 = -2 //> = 0否

this would do the division 这将做分裂

while(dividend >= divisor){
  dividend -= divisor;
  // count your result ++
}  
System.out.println(dividend);//this is the remainder

Everything you have is fine exception your while loop and your computation for remainder: 您拥有的一切都很好,您的while循环和余数的计算除外:

Here's a helpful adjustment: 这是一个有用的调整:

int result = 0;
while (dividend >= divisor)
{
   dividend = dividend - divisor;
   result = result + 1;
}
remainder = dividend;
System.out.print(countDividend + " divided by " + countDivisor + " equals " + integer_result + " with a remainder of " + remainder);

By the way, your program doesn't handle negative numbers too well. 顺便说一句,您的程序不能很好地处理负数。 You should consider fixing that bug before submitting your homework. 您应该在提交作业之前考虑修复该错误。

You are doing it entirely wrong, and your prof is gonna ding you bad. 您所做的完全错误,您的教授将对您不利。

To multiply you shift and add: 要相乘,您需要移位并加:

        1 0 1 0 -- bits A B C D  == 10  
      x 1 1 0 0 -- bits E F G H  == 12

D is zero, so the first cycle (multiply EFGH by zero) results in D为零,因此第一个周期(EFGH乘以零)得出

        0 0 0 0 

Shift EFGH left one 将EFGH左移

      1 1 0 0 0

Bit C is 1 so the result is 位C为1,因此结果为

      1 1 0 0 0 

Shift EFGH left one 将EFGH左移

    1 1 0 0 0 0

Bit B is 0 so no change B位为0,所以没有变化

Shift EFGH left one 将EFGH左移

  1 1 0 0 0 0 0

Bit A is 1 so add 位A为1,所以加

  1 1 0 0 0 0 0
   +  1 1 0 0 0
  -------------
  1 1 1 1 0 0 0

Division is the same thing, only backwards. 除法是同一件事,只是倒退。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM