java abs(绝对值) , max(最大值),min(最小值) 方法的应用

在写程序是,我们常常会计算一个数的绝对值,这时我们可以使用java里的方法来计算 public class Demo1{  public static void main(String [] args){     int a = 1;     int b = -...

在写程序是,我们常常会计算一个数的绝对值,这时我们可以使用java里的方法来计算

public class Demo1{
  public static void main(String [] args){
    int a = 1;
    int b = -2;
    int c = a - b;
    c = Math.abs(c);
    System.out.println(c);
  }
}

Math.abs() 括号里面放你需要求绝对值的数

求两个数的最大值

public class Demo1{
  public static void main(String[]args){
  int a = 1;
  int b = -2;
  int c = a - b;
  System.out.println(Math.max(a,b));
  }
}

Math.max(int a,int b)

int类型可改为double,float,long 要注意的是a,b两个数据的数据类型必须一样

求最小值也一样,只需把上面的max改为min即可

  • 发表于 2019-03-25 14:00
  • 阅读 ( 217 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除