Java将正整数转换为负数,将负整数转换为正数

时间:2025-03-12 09:49:09

public class Demo {

   public static void main(String[] args) throws {

      int positiveVal = 100;

      int negativeVal = (~(positiveVal - 1));

      ("Result: Positive value converted to Negative = "+negativeVal);

      positiveVal = ~(negativeVal - 1);

      ("Actual Positive Value = "+positiveVal);

      negativeVal = -200;

      ("Actual Negative Value = "+negativeVal);

      positiveVal = ~(negativeVal - 1);

      ("Result: Negative value converted to Positive = "+positiveVal);

   }

}