以下是一个简单的
Java 计算器代码,可以进行加、减、乘、除运算:
javaimport
java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = input.nextDouble();
System.out.print("Enter second number: ");
double num2 = input.nextDouble();
System.out.print("Enter operator (+, -, *, /): ");
char operator = input.next().charAt(0);
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:
java 基础计算代码 System.out.println("Invalid operator!");
return;
}
System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
}
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/25968.html