计算器
package com.fenghuo.base1;
import java.util.Scanner;
public class calc {
//写一个计算器,要求实现加减乘除的功能,并且可以循环接受新的数据。通过用户交互实现
public static void main(String[] args) {
int i=0;
int j=0;
while(true) {
System.out.println("请输入你需要进行的计算功能:加 减 乘 除 退出");
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
if (text.equals("退出")){
break;
}
switch (text) {
case "加":
System.out.println("请输入你所需要相加的两个数字");
i = in();
j = in();
System.out.println(add(i, j));
break;
case "减":
System.out.println("请输入你所需要相减的两个数字");
i = in();
j = in();
System.out.println(reduce(i, j));
break;
case "乘":
System.out.println("请输入你所需要相乘的两个数字");
i = in();
j = in();
System.out.println(ride(i, j));
break;
case "除":
System.out.println("请输入你所需要相除的两个数字");
i = in();
j = in();
System.out.println(except(i, j));
break;
default:
System.out.println("输入有错,请重新输入");
}
}
}
public static int add(int i,int j){
return i+j;
}
public static int reduce(int i,int j){
return i-j;
}
public static int ride(int i,int j){
return i*j;
}
public static double except(int i,int j){
if(j==0){
System.out.println("除数不为0,请重新输入");
}
return i/j;
}
public static int in(){
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
return i;
}
}