用java写一个加减乘除的代码
时间: 2023-09-10 09:04:56 浏览: 142
### 回答1:
好的,以下是用 Java 写一个加减乘除的代码:
```
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数字:");
double num1 = scanner.nextDouble();
System.out.print("请输入第二个数字:");
double num2 = scanner.nextDouble();
System.out.print("请输入运算符(+、-、*、/):");
char operator = scanner.next().charAt();
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.println("输入的运算符不正确!");
return;
}
System.out.println("运算结果为:" + result);
}
}
```
以上代码可以让用户输入两个数字和一个运算符,然后根据运算符进行加减乘除运算,并输出结果。
### 回答2:
使用Java编写一个加减乘除的代码示例如下:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个操作数:");
int num1 = scanner.nextInt();
System.out.println("请输入第二个操作数:");
int num2 = scanner.nextInt();
System.out.println("请选择操作:\n1.加法\n2.减法\n3.乘法\n4.除法");
int choice = scanner.nextInt();
switch (choice) {
case 1:
int sum = num1 + num2;
System.out.println("结果:" + sum);
break;
case 2:
int difference = num1 - num2;
System.out.println("结果:" + difference);
break;
case 3:
int product = num1 * num2;
System.out.println("结果:" + product);
break;
case 4:
if (num2 != 0) {
double division = (double) num1 / num2;
System.out.println("结果:" + division);
} else {
System.out.println("除数不能为0!");
}
break;
default:
System.out.println("请输入正确的选项!");
break;
}
scanner.close();
}
}
```
这段代码首先使用`Scanner`类获取用户输入的两个操作数和操作选项。然后,使用`switch`语句根据用户选择执行相应的操作,并输出结果。在除法操作中,我们还添加了除数为0的检查,以避免出现除数为0的异常。
用户可以根据提示输入两个操作数和操作选项,代码将根据用户选择执行相应的操作,并输出结果。如果用户输入了无效的选项或者除数为0,代码将给出相应的提示信息。这段代码实现了基本的加减乘除功能,可以根据需要进行扩展和优化。
### 回答3:
下面是一个用Java语言编写的加减乘除代码的例子:
```java
public class Calculator {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
int sum = add(num1, num2);
int difference = subtract(num1, num2);
int product = multiply(num1, num2);
double quotient = divide(num1, num2);
System.out.println("加法结果:" + sum);
System.out.println("减法结果:" + difference);
System.out.println("乘法结果:" + product);
System.out.println("除法结果:" + quotient);
}
public static int add(int a, int b) {
return a + b;
}
public static int subtract(int a, int b) {
return a - b;
}
public static int multiply(int a, int b) {
return a * b;
}
public static double divide(int a, int b) {
if (b != 0) {
return (double) a / b;
} else {
System.out.println("除数不能为0");
return 0;
}
}
}
```
上述代码中,首先定义了一个Calculator类。在main方法中,我们定义了两个整数num1和num2。然后通过调用自定义的add、subtract、multiply和divide方法来执行加法、减法、乘法和除法运算。最后,我们将计算结果打印出来。
add方法接收两个整数参数,返回它们的和。
subtract方法接收两个整数参数,返回它们的差。
multiply方法接收两个整数参数,返回它们的积。
divide方法接收两个整数参数,返回它们的商。在这个方法中,我们首先判断除数是否为0,若是则输出错误信息,否则返回它们的商。利用`(double) a / b`来将结果强制转换成double类型,以便得到精确的小数结果。
以上就是一个简单的加减乘除的Java代码实现。请注意,此代码只是作为示例,可能不包含所有的错误处理情况,实际使用时需要根据需求进行扩展和改进。
阅读全文
相关推荐















