代码思路:
1.写4个方法:加减乘除;每个方法输入两个数据,并传出操作结果。
2.main()中利用for循环和switch进行用户交互,并退出循环。- import java.util.Scanner;
- public class calculation {
- public static void main(String[] args) {
- System.out.println("==========简单计算机===========");
- System.out.println("1.加法\t" + "2.减法\t" + "3.乘法\t" + "4.除法\t" + "5.退出");
- Scanner scanner = new Scanner(System.in);
- for ( ; ; ){
- System.out.println("请输入您想要进行的操作:");
- int s =scanner.nextInt();
- switch (s){
- case 1:
- add();
- break;
- case 2:
- reduce();
- break;
- case 3:
- ride();
- break;
- case 4:
- divide();
- break;
- case 5:
- break;
- default:
- System.out.println("输入的操作符号有误!");
- System.out.println();
- }
- if (s==5){
- scanner.close();
- System.out.println("退出循环!");
- break;
- }
- }
- }
- public static void add(){
- Scanner scanner2 = new Scanner(System.in);
- System.out.println("输入第一个数x:");
- double x = scanner2.nextDouble();
- System.out.println("输入第二个数y:");
- double y = scanner2.nextDouble();
- System.out.println("================================");
- System.out.println("x + y = "+(x+y)+"\n");
- }
- public static void reduce(){
- Scanner scanner2 = new Scanner(System.in);
- System.out.println("输入第一个数x:");
- double x = scanner2.nextDouble();
- System.out.println("输入第二个数y:");
- double y = scanner2.nextDouble();
- System.out.println("================================");
- System.out.println("x - y = "+(x-y)+"\n");
- }
- public static void ride(){
- Scanner scanner2 = new Scanner(System.in);
- System.out.println("输入第一个数x:");
- double x = scanner2.nextDouble();
- System.out.println("输入第二个数y:");
- double y = scanner2.nextDouble();
- System.out.println("================================");
- System.out.println("x * y = "+(x*y)+"\n");
- }
- public static void divide(){
- Scanner scanner2 = new Scanner(System.in);
- System.out.println("输入第一个数x:");
- double x = scanner2.nextDouble();
- System.out.println("输入第二个数y:");
- double y = scanner2.nextDouble();
- System.out.println("================================");
- if (y==0){
- System.out.println("除数不能为0!"+"\n");
- }else {
- System.out.println("x / y = "+(x/y)+"\n");
- }
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |