简体   繁体   English

如何在不使用 math.abs 的情况下找到两个数字的差值和该答案的绝对值 function JAVA

[英]How to find the difference of two numbers and the absolute value of that answer without using math.abs function JAVA

How can I find the absolute value of the difference of two numbers.我怎样才能找到两个数字之差的绝对值。 (BEGINNER) (初学者)

ie My program will compute |ab|即我的程序将计算|ab| (in that order), WITHOUT using math.abs. (按此顺序),不使用 math.abs。

This is what I have so far:这是我到目前为止所拥有的:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    double a = in.nextDouble();
    double b = in.nextDouble();
    double value = a - b;

    System.out.println("Enter a: ");
    a = in.nextDouble();

    System.out.println("Enter b: ");
    b = in.nextDouble();

    //If value is negative...make it a positive number.
    value = (value < 0) ? -value : value;

    System.out.println(a + "-" + b + "=" + (a - b));
    System.out.println(b + "-" + a + "=" + (b - a));

}

} }

PLEASE HELP, I AM A BEGINNER!请帮助,我是初学者!

A bit more formatted code. 更多格式化的代码。

import java.util.Scanner;
class A {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double a;
        double b;
        System.out.println("Enter a: ");
        a = in.nextDouble();

        System.out.println("Enter b: ");
        b = in.nextDouble();
        double value = a - b;



        //If value is negative...make it a positive number.
        value = (value < 0) ? -value : value;
            System.out.println("|"+a + "-" + b +"|" + " =" + value);  // value should be printed here instead of (a-b) or (b-a)
        System.out.println("|"+b + "-" + a +"|" + " =" + value);

    }
}

First of all you are taking scanner two times without any reason 首先,您无故两次使用扫描仪

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


        System.out.println("Enter a: ");
        double a = in.nextDouble();

        System.out.println("Enter b: ");
        double b = in.nextDouble();

double value = a - b;
double value2 = b - a;

//If value is negative...make it a positive number.
        value = (value < 0) ? -value : value;
        value2 = (value2 < 0) ? -value2 : value2;

        System.out.println(a + "-" + b + "=" + value); //chaged to value
            System.out.println(b + "-" + a + "=" + value2); //changed to value

        }

its simple这很简单

assume we have two int a and b.假设我们有两个 int a 和 b。 And a variable diff to find the absolute difference.和一个变量 diff 来找到绝对差异。 code:代码:

int diff=a-b;
if(diff<0)
diff=b-a;

here you will get the absolute value between a and b.在这里你会得到a和b之间的绝对值。

To have an absolute value for your 为您带来绝对价值

value

You can add if condition like below 您可以添加以下条件

if (value < 0) {
   value = value * -1;
} 

So that the negative answer (difference) will always be converted to positive (absolute) value. 这样,否定答案(差异)将始终转换为正(绝对)值。

If the value is greater than 0, then no need as it is already a positive number. 如果该值大于0,则不需要,因为它已经是一个正数。

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

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