codewars解题笔记---Opposite number

Instructions:

Very simple, given a number, find its opposite.

Examples:

1: -1
14: -14
-34: 34

Sample Tests: 

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class OppositeExampleTests {
  @Test
  public void tests() {
    assertEquals(-1, Kata.opposite(1));
  }
}

Solution:

public class Kata
    {
        public static int opposite(int number)
        {
            // your code here
        }
    }

题目的解释:

很简单,给定一个数字,找到它的相反数。

例如:

1: -1
14: -14
-34: 34

我的答案:

public class Kata
    {
        public static int opposite(int number)
        {
            number = 0 - number;
            return number;
        }
    }

 最优答案:

public class Kata
    {
        public static int opposite(int number)
        {
            return -number;
        }
    }

 

你可能感兴趣的:(codewars)