Java代码实现一个计算机程序
package com.ynlg.test;
import java.util.Scanner;
public class Dark {
//加法运算
public void Add(double num1,double num2) {
System.out.println(num1+num2);
}
//减法运算
public void Subtraction(double num1,double num2) {
System.out.println(num1-num2);
}
//乘法运算
public void Multiply(double num1,double num2) {
System.out.println(num1*num2);
}
//除法运算
public void Divide(double num1,double num2) {
System.out.println(num1/num2);
}
public void Lotus() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要计算的第一个数字:");
double num1 = sc.nextDouble();
System.out.println("请输入需要计算的第二个数字:");
double num2 = sc.nextDouble();
System.out.println("请选择运算【+、-、*、/、?】");
char op = sc.next().charAt(0);
switch(op) {
case '+' :
Add(num1, num2);
break;
case '-' :
Subtraction(num1, num2);
break;
case '*' :
Multiply(num1, num2);
break;
case '/' :
if(num2==0) {
System.out.println("除数不能为0!");
}else {
Divide(num1, num2);
}
break;
default:
System.out.println("输入错误!");
break;
}
}
public static void main(String[] args) {
Dark D = new Dark();
D.Lotus();
}
}
期待你的指导与交流!