feat:添加计算器功能

main
yuan 2 months ago
parent 19c41339ab
commit 3ffc742dd5

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ApifoxUploaderProjectSetting">
<option name="apiAccessToken" value="APS-XcyreP6fmYRQPtrGBguxJiFaoFwNK7jA" />
</component>
</project>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/cyy_team.iml" filepath="$PROJECT_DIR$/.idea/cyy_team.iml" />
<module fileurl="file://$PROJECT_DIR$/demo/demo.iml" filepath="$PROJECT_DIR$/demo/demo.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,85 @@
import java.util.Scanner;
public class Main {
// 加法
public static double add(double num1, double num2) {
return num1 + num2;
}
// 减法
public static double subtract(double num1, double num2) {
return num1 - num2;
}
// 乘法
public static double multiply(double num1, double num2) {
return num1 * num2;
}
// 除法
public static double divide(double num1, double num2) {
if (num2 == 0) {
System.out.println("错误: 除数不能为0");
return Double.NaN; // 返回无效数字
}
return num1 / num2;
}
// 取余
public static double remainder(double num1, double num2) {
if (num2 == 0) {
System.out.println("错误: 不能对0取余");
return Double.NaN; // 返回无效数字
}
return num1 % num2;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入第一个数
System.out.print("请输入第一个数: ");
double num1 = scanner.nextDouble();
// 输入运算符
System.out.print("请输入运算符 (+, -, *, /, %): ");
char operator = scanner.next().charAt(0);
// 输入第二个数
System.out.print("请输入第二个数: ");
double num2 = scanner.nextDouble();
// 结果变量
double result = 0;
// 根据运算符选择操作
switch (operator) {
case '+':
result = add(num1, num2);
break;
case '-':
result = subtract(num1, num2);
break;
case '*':
result = multiply(num1, num2);
break;
case '/':
result = divide(num1, num2);
break;
case '%':
result = remainder(num1, num2);
break;
default:
System.out.println("无效的运算符");
return;
}
// 输出结果
if (!Double.isNaN(result)) {
System.out.println("结果: " + result);
}
scanner.close();
}
}
Loading…
Cancel
Save