android中修饰void的类型,方法添加Android中

更多控制,尝试像

public float addition(float numa, float numb) {

// will return float

return numa + numb;

}

public int addition(int numa, float numb) {

// explicitly cast to int

return numa + (int) numb;

}

public int addition(float numa, int numb) {

// explicitly cast to int

return (int) numa + numb;

}

public int addition(int numa, int numb) {

// will return int

return numa + numb;

}

的重载方法基本OOP概念,考试系统的在放,尝试这样的事情......

public void examineInput(String input1, String input2) {

// For both are float

if (input1.indexOf(".") != -1 && input2.indexOf(".") != -1) {

float numa = Float.parseFloat(input1);

float numb = Float.parseFloat(input2);

float ans = addition(numa, numb);

Log.i(TAG, String.format("%f + %f = %f", numa, numb, ans));

}

// for first to be int and second to be float

else if (input1.indexOf(".") == -1 && input2.indexOf(".") != -1) {

int numa = Integer.parseInt(input1);

float numb = Float.parseFloat(input2);

int ans = addition(numa, numb);

Log.i(TAG, String.format("%d + %f = %d", numa, numb, ans));

}

// for first to be float and second to be int

else if (input1.indexOf(".") != -1 && input2.indexOf(".") == -1) {

float numa = Float.parseFloat(input1);

int numb = Integer.parseInt(input2);

int ans = addition(numa, numb);

Log.i(TAG, String.format("%f + %d = %d", numa, numb, ans));

}

// for both to be int

else if (input1.indexOf(".") == -1 && input2.indexOf(".") == -1) {

int numa = Integer.parseInt(input1);

int numb = Integer.parseInt(input2);

int ans = addition(numa, numb);

Log.i(TAG, String.format("%d + %d = %d", numa, numb, ans));

}

}

而且是输入测试此代码,输出

examineInput("5.2", "6.2"); // 5.200000 + 6.200000 = 11.400000

examineInput("5", "3.6"); // 5 + 3.600000 = 8

examineInput("1.6", "5"); // 1.600000 + 5 = 6

examineInput("5", "5"); // 5 + 5 = 10

注:你需要验证examineInput始终得到有效的数字,而不是非numaric字符的字符串...

希望这有助于提高OOP概念以及.. :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值