1 package com.Class_And_Object; 2 3 public class ZuoYeA01 { 4 5 int a; //定义两个整数 6 int b; 7 String fang; 8 public void jia(){ 9 System.out.println("结果"+(a+b)); 10 } 11 public void jian(){ 12 System.out.println("结果"+(a-b)); 13 } 14 public void cheng(){ 15 System.out.println("结果"+(a*b)); 16 } 17 public void chu(){ 18 System.out.println("结果"+(a/b)); 19 } 20 21 }
1 package com.Class_And_Object; 2 3 import java.util.Scanner; 4 5 public class ZuoYeA02 { 6 /** 7 * 用面向对象思想,编写一个计算器,可以实现两个整数的加、减、乘、除运算。 8 * 9 * 首先从该问题中抽象出类,然后找到它具有的属性和方法 10 * 11 * */ 12 public static void main(String[] args) { 13 Scanner input=new Scanner(System.in); 14 ZuoYeA01 me = new ZuoYeA01(); 15 System.out.println("输入一个整数"); 16 me.a=input.nextInt(); 17 System.out.println("输入另一个整数"); 18 me.b=input.nextInt(); 19 System.out.println("输入算数方式"); 20 me.fang=input.next(); 21 switch (me.fang) { 22 case "+": 23 me.jia(); 24 break; 25 case "-": 26 me.jian(); 27 break; 28 case "*": 29 me.cheng(); 30 break; 31 case "/": 32 me.chu(); 33 break; 34 35 default: 36 break; 37 } 38 39 } 40 41 }
时间: 2024-11-04 17:26:34