負数を正数に変換するには(絶対値と呼ばれます)、http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#abs%28int%29[Mathを使用します。 abs()]。
この
Math.abs()`メソッドは "number =(number <0?-number:number);
“のように動作します。
完全な例を参照してください。
package com.mkyong; public class app{ public static void main(String[]args) { int total = 1 + 1 + 1 + 1 + (-1); //output 3 System.out.println("Total : " + total); int total2 = 1 + 1 + 1 + 1 + Math.abs(-1); //output 5 System.out.println("Total 2 (absolute value) : " + total2); } }
出力
Total : 3 Total 2 (absolute value) : 5
この場合、 `Math.abs(-1)`は負の数1を正の1に変換します。
リンク://タグ/変換/[変換]リンク://タグ/java/[java]