练习:39

老师要求张浩使用面向对象的思想编写一个计算器类(Calculator)
可以实现两个整数的加,减,乘,除运算
这是书本上的第231页,第2题

参考代码如下:

/*
* 老师要求张浩使用面向对象的思想编写一个计算器类(Calculator)
* 可以实现两个整数的加,减,乘,除运算
* 这是书本上的第231页,第2题
*/
import java.util.Scanner;
public class Calculator {
//定义属性
int Add; //加
int Minus; //减
int By ; //乘
int InAddItion; //除
int num,num1; //录入的2个数字
int sum ; //选择计算方式
//定义输出方法
public void show() {
Scanner input = new Scanner (System.in);
System.out.println("请输入第1个整数:");
num = input.nextInt();
System.out.println("请输入第2个整数:");
num1 = input.nextInt();
System.out.println("*********************");
System.out.println("1.加");
System.out.println("2.减");
System.out.println("3.乘");
System.out.println("4.除");
System.out.println();
System.out.println("请选择:");
sum = input.nextInt();
System.out.println("*********************");
switch (sum){
case 1 : //计算加法
Add = num+num1 ;
System.out.println("结果是:"+Add);
break;
case 2 : //计算减法
Minus = num-num1 ;
System.out.println("结果是:"+Minus);
break;
case 3 : //计算乘法
By = num*num1;
System.out.println("结果是:"+By);
break;
case 4 : //计算除法
InAddItion = num/num1;
System.out.println("结果是:"+InAddItion);
break;
default: //switch的默认配置,数字小于1或者大于4输出错误,结束程序
System.out.println("数字输入错误!程序结束!");
break;
}

}

public static void main(String[] args) {
Calculator center = new Calculator (); //定义对象
center.show(); //输出show方法
}

}

《练习:39》上有1条评论

  1. The energy of developing utilization of co-registration to create your report Authentic UGG Boots Clearance will in all probability be the actuality that you’re increasing your protection with the quantity of guys and ladies ugg moccasins who are subscribing for the partner’s ezine. So, the extra guys and ladies you take on as co-registration partners, the extra guys and ladies who will see your provide and who will Cheap UGG Dakota Moccasins indicator up for the ezine.Research exhibits which you can have an standard of 10 partners preceding to registrations start to fall off. think concerning the protection you’ll get for all those that companion UGG Dakota Slippers with just 5 other people within your current market you receive all their specific site visitors plus they get yours it’s UGG Classic Short Toddler Boots a amazing win-win.For that reason, and granted that that is primarily a ‘set it and overlook it’ tactic, this has swiftly turn into one of my selected marketing UGG Classic Tall Boots Kids strategies. Go forward and try it I’m specific you’ll appear throughout the identical for getting true.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据