第一种,创建普通Java工程,手动创建一个起名lib的目录,复制Jar包到lib目录。然后在project Structure,加入jar包在工程
双击shift键,输入Test
未成功添加
已经成功添加
第二种,引入pom依赖,同时加入阿里云镜像仓库
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>nexus-aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
然后创建一个类,有各种方法,点击generate
package org.qiu;
/**
* @author Aiver
* @date 2022/11/27 11:18
*/
public class Calculate {
public int add(int x,int y){
return x+y;
}
public int mul(int x,int y){
return x*y;
}
public float div(int x,int y){
return x/y;
}
public int sub(int x,int y){
return x-y;
}
}
自动生成成功 ,debug运行
其中关于除法,比如 1/2
public float div(int x,int y){
if (y ==0){
throw new ArithmeticException("除数不能为0!!");
}
return x/(y*1.0f);
}
上面这个结果就是 1/2 = 0.5
而下面就是 1/2 = 0
public float div(int x,int y){
if (y ==0){
throw new ArithmeticException("除数不能为0!!");
}
return x/y*1.0f;
}