【问题标题】:how to multiply two negative values without using the multiplication (*) function in java如何在不使用java中的乘法(*)函数的情况下将两个负值相乘
【发布时间】:2014-03-13 01:08:05
【问题描述】:

例如,我有这段代码可以在不使用 * 符号的情况下将 2 个数字相乘,但是如何使用负数实现相同的效果?

public static void main(String[] args) {
    // TODO code application logic here
    Scanner in = new Scanner(System.in);

    int result = 0;

    System.out.println("Enter the first number: ");
    int num1 = in.nextInt();
    System.out.println("Enter the second number: ");
    int num2 = in.nextInt();
   if(num1==0 || num2==0) {
       System.out.println("the profuct of these numbers is: 0");
     }

    for(int i =0; i <num1; i++)

    {
        result += num2;            
    }
    System.out.println("the profuct od these numbers is: "+result);

}

}

【问题讨论】:

  • 提示:如果X小于0,则将其视为X = -1 * abs(X);
  • 提示:两个负数相乘得到一个正数。
  • 提示:您的代码可以在 for 循环中的 num1 前面添加额外的减号并在末尾添加结果。
  • 你为什么要那样做?键盘坏了? ;)

标签: java multiplication negative-number


【解决方案1】:

java.lang.Maths.abs(num1/2) 用于迭代和

类似:

if(num1<0^num2<0){
    result = 0 - result;
}

在交互和输出之间。

【讨论】:

    【解决方案2】:

    如果只有一个数字是负数,您可以使用相同的方法,但您需要确保在循环中使用正数(如代码中的num1)。如果两个数字都是负数,您可以使用 abs(num) 将它们都更改为正数,并且没有任何变化。

    【讨论】:

      【解决方案3】:

      保留您所拥有的,但要跟踪您拥有的底片数量并使用绝对值。

      bool resultNeg = false;
      if( num1 < 0 && num2 > 0 || num1 > 0 && num2 < 0 )
          resultNeg = true;
      
      num1 = Math.abs( num1 ); num2 = Math.abs( num2 );
      
      for( int i = 0; i < num1; ++i )
      {
          // ...
      }
      
      if( resultNeg ) result = 0 - result;
      
      // ...
      

      【讨论】:

        【解决方案4】:

        也许BigInteger

        BigInteger num1 = new BigInteger(-12);
        BigInteger num2 = new BigInteger(-13);
        BigInteger result  = num1.multiply(num2);
        

        您也可以按位使用shifting and adding 并试试这个on negative numbers

        当然,您可以将自己的价值观相乘,并使其正面或负面 - 您知道规则。

        【讨论】:

        • 好吧,这显然会在不使用 * 运算符的情况下成倍增加,但我认为这不是 OP 的目的。
        • -1:这个问题显然是一个使用循环模拟乘法的家庭作业/练习。虽然这直接解决了问题,但不会帮助 OP 学习编程。
        【解决方案5】:
        public static void main(String[] args)
        {
            // TODO code application logic here
            Scanner in = new Scanner(System.in);
        
            int result = 0;
        
            System.out.println("Enter the first number: ");
            int num1 = in.nextInt();
            System.out.println("Enter the second number: ");
            int num2 = in.nextInt();
            if(num1==0 || num2==0) {
                System.out.println("the profuct of these numbers is: 0");
                return;
            }
        
            String sign = "";
            if (num1 < 0 && num2 > 0)
            {
                sign = "-";
            }
            if (num2 < 0 && num1 > 0)
            {
                sign = "-";
            }
        
            num1 = abs(num1);
            num2 = abs(num2);
            for(int i =0; i <num1; i++)
            {
                result += num2;            
            }
            System.out.println("the profuct of these numbers is: "+sign+result);
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-24
          • 2010-11-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多