用Java進行簡單的乘除計算

來源:互聯網
上載者:User

標籤:

用Java進行簡單的乘除計算

 

一、題目簡介

一個兩個數簡單的乘除計算的Java代碼並驗證是否其正確性。

 

二、源碼的github連結

https://github.com/Battleblocke/Math

代碼如下:

1.

public class Math

{
  public static int divide(int x,int y)

  {
        return x/y;
  }

  public static int multiple(int x,int y) {
    return x*y;
  }
}

2.

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;


@RunWith(Parameterized.class)
public class MathTest

  {
  int faciend;
  int multiplicator;
  int result;

  public MathTest(int faciend, int multiplicator, int result) {
  this.faciend = faciend;
  this.multiplicator = multiplicator;
  this.result = result;
  }

@BeforeClass
public static void setUpBeforeClass() throws Exception

  {
  }

@AfterClass
public static void tearDownAfterClass() throws Exception {
  }

@Test(expected=ArithmeticException.class)
public void testDivide() {
assertEquals(3,Math.divide(9,3));
assertEquals(3,Math.divide(10,3));
Math.divide(10,0);//除數不能為0,會拋出異常

  }

//@Ignore("忽略乘法測試")
@Test
public void testMultiple() {
assertEquals(result,Math.multiple(faciend,multiplicator));
  }

@Parameters
public static Collection multipleValues() {
return Arrays.asList(new Object[][] {
{3, 2, 6 },
{4, 3, 12 },
{21, 5, 105 },
{11, 22, 242 },
{8, 9, 72 }});
}

}

三、所設計的模組測試案例、測試結果

 

 

四、問題及解決方案、心得體會

  本次實驗學習了Github的基本使用方法以及項目的簡單建立。

  學習中遇到了不少困難,也學習了不少有用的知識,受益匪淺。。

 

用Java進行簡單的乘除計算

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.