public static int add(int a,int b){
return a+b;
}
public static int abs(int i){
if (i>=0)
return i;
else return -i;
}
}
public static void main(String args[]){
int a=abs(-4);
int b=abs(5);
System.out.println(add(a,b));
}

解决方案 »

  1.   

    太简单了,通过一个函数就可实现:public int abs(int x,int y)
    { if((x>=0)&&(y>=0))
       return x+y;
      else if(x>0)
       return x-y;
     else if(y>0)
       ret;urn y-x;
     else return -y-x;
    }
      

  2.   

    我今天要让大家知道我是多么的菜:
    public class value {
    int i,j;
    int value(int i1,int j1){
          if (i1<0) i=-i1;
           else  i=i1;                     
            if (j1<0)     j=-j1;                         
           else j=j1;                     
          return(i+j);
                             }
       public static void main(String[] arguments){
        value rect =new value();
        int x;
        x=rect.value( 4,-66);
       System.out.print(x);
                      }
      

  3.   

    根据楼上几位来改的,适当的加工了一下:int absadd(int i,int j)
      {
        i=i>0?i:-i;
        j=j>0?j:-j;
        return(i+j);
      }
      

  4.   

    完整的程序,可实现从键盘接收两个整形数进行绝对值加法运算。
    import java.io.*;
    class aaa
    {

    public static void main(String args[])  throws Exception 
    {   
         byte array1[]=new byte[10] , array2[]=new byte[10]; 
         Integer theFirst,theSecond;
         String  tempOne,tempTwo;
         int  a,b,lengthOfArray=0;

    System.out.print("请输入第一个数:");
    lengthOfArray=System.in.read( array1);
    tempOne= new String(array1,0,lengthOfArray-2);
     theFirst=new Integer(tempOne);

     System.out.print("请输入第二个数:");
         System.in.read(array2,0,array2.length);
         tempTwo= new String(array2,0,lengthOfArray-2);
     theSecond=new Integer(tempTwo);     a=theFirst.intValue();
         b=theSecond.intValue();
      
      System.out.println(absAdd(a,b));
     }
      private static int absAdd(int x,int y)
        { if((x>=0)&&(y>=0))
           return x+y;
          else if(x>0)
            return x-y;
         else if(y>0)
             return y-x;
         else return -y-x;
    }
     
    }