#include
int main( )
{
double a, b;
char op;
scanf("%lf%c%lf", &a, &op, &b);
switch (op)
{
case '+':
printf("%lf + %lf = %lf\n", a, b, a + b);
break;
case '-':
printf("%lf - %lf = %lf\n", a, b, a - b);
break;
case '*':
printf("%lf * %lf = %lf\n", a, b, a * b);
break;
case '/':
if (b != 0)
printf("%lf / %lf = %lf\n", a, b, a / b);
else
printf("除数不能是0! \n");
break;
}
}
package Operat;
import java.util.Scanner;
public class Operat {
static int Addtion(int number1, int number2) {
int result = 0;
result = number1 + number2;
return result;
}
static int Multiplication(int number1, int number2) {
int result = 0;
result = number1 * number2;
return result;
}
static int Divsion(int number1, int number2) {
int ressult = 0;
try {
ressult = number1 / number2;
} catch (Exception e) {
// TODO: handle exception
e.getMessage();
System.out.println("除数不能为0");
}
return ressult;
}
static int Subtraction(int number1, int number2) {
int result = 0;
result = number1 - number2;
return result;
}
public static void main(String[] args) {
int number1;
int number2;
while (true) {
System.out.println("1:加法,2:乘法,3:除法,4:减法");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Addtion(number1,number2);
System.out.println(Addtion(number1, number2));
break;
case 2:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Multiplication(number1,number2);
System.out.println(Multiplication(number1, number2));
break;
case 3:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Divsion(number1,number2);
System.out.println(Divsion(number1, number2));
break;
case 4:
System.out.println("请输入两个数字:");
number1 = input.nextInt();
number2 = input.nextInt();
// Subtraction( number1,number2);
System.out.println(Subtraction(number1, number2));
break;
default:
System.out.println("请重新输入选项:");
break;
}
}
}
}
自己手打的java代码,不喜勿喷,自己也是小白,求大神指点。
#include
int main(void)
{
int data1,data2;
char op;
printf("Please enter the expression:\n");
scanf("%d%c%d",&data1,&op,&data2);
switch(op)
{
case'+':printf("%d+%d=%d\n",data1,data2,data1+data2);break;
case'-':printf("%d-%d=%d\n",data1,data2,data1-data2);break;
case'*':printf("%d*%d=%d\n",data1,data2,data1*data2);break;
case'/':
if(0==data2)
printf("Division by zero£¡\n");
else
printf("%d/%d=%d\n",data1,data2,data1/data2);break;
default:printf("Unknown operator!\n");
}
return 0;
}
要用java的话请找MIMOSAatBJ@163.com