Android资源文件里复数使用

这个比较偏门,在英语系国家里有复数的概念,中文里则没有。

比如: 一人喜欢我们可以说“one like”,多人喜欢则是“serveral likes”,like变成了复数。 Android可以把这种复数的操作预定义在strings文件里,具体如下:

<resources>
    <plurals name="like_num">
        <item quantity="one"> %d like</item>
        <item quantity="other"> %d likes</item>
    </plurals>
</resources>

在代码里,我们可以通过String pluralsText = getResources().getQuantityString(R.plurals.like_num,13,13);来调用,getQuantityString有两个重载方法,两个参数的是直接去Strings文件里选择返回的相应项目里的语句,三个参数的犯法则会返回第三个参数代替‘%d’产生的语句,注意:数字‘1’对应quantity的‘one’,数字和英文一一对应,other则代表需要实现复数的内容。

转载于:https://my.oschina.net/lengwei/blog/551485

以下是使用Android Studio设计计算器的示例代码,用于进行复数或非运算: 复数运算: ```java public class Complex { private double real, imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public double getReal() { return real; } public double getImaginary() { return imaginary; } public Complex add(Complex other) { double resultReal = this.real + other.getReal(); double resultImaginary = this.imaginary + other.getImaginary(); return new Complex(resultReal, resultImaginary); } public Complex subtract(Complex other) { double resultReal = this.real - other.getReal(); double resultImaginary = this.imaginary - other.getImaginary(); return new Complex(resultReal, resultImaginary); } public Complex multiply(Complex other) { double resultReal = this.real * other.getReal() - this.imaginary * other.getImaginary(); double resultImaginary = this.real * other.getImaginary() + this.imaginary * other.getReal(); return new Complex(resultReal, resultImaginary); } public Complex divide(Complex other) { double denominator = other.getReal() * other.getReal() + other.getImaginary() * other.getImaginary(); double resultReal = (this.real * other.getReal() + this.imaginary * other.getImaginary()) / denominator; double resultImaginary = (this.imaginary * other.getReal() - this.real * other.getImaginary()) / denominator; return new Complex(resultReal, resultImaginary); } public String toString() { String result = ""; if (real != 0) { result += real; } if (imaginary > 0) { result += "+" + imaginary + "i"; } else if (imaginary < 0) { result += imaginary + "i"; } if (result.equals("")) { result = "0"; } return result; } } ``` 非运算: ```java public class Not { public static boolean not(boolean value) { return !value; } } ``` 这些代码可以在Android Studio中的Java文件中使用,例如一个名为"MainActivity.java"的文件。在此文件中,您可以创建UI元素,例如按钮和文本视图,然后将它们与上述代码连接,以便当用户点击按钮时执行所需的计算操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值