Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编写更短的if-else代码 public class numbertowords 2{ 公共静态void main(字符串[]args){ //TODO自动生成的方法存根 int n=30001; 数字字(n); } 公共静态字符串numberToWords(int n){ 字符串温度=整数。toString(n); int[]myArr=新int[temp.length()]; 对于(int i=0;i_Java - Fatal编程技术网

Java 编写更短的if-else代码 public class numbertowords 2{ 公共静态void main(字符串[]args){ //TODO自动生成的方法存根 int n=30001; 数字字(n); } 公共静态字符串numberToWords(int n){ 字符串温度=整数。toString(n); int[]myArr=新int[temp.length()]; 对于(int i=0;i

Java 编写更短的if-else代码 public class numbertowords 2{ 公共静态void main(字符串[]args){ //TODO自动生成的方法存根 int n=30001; 数字字(n); } 公共静态字符串numberToWords(int n){ 字符串温度=整数。toString(n); int[]myArr=新int[temp.length()]; 对于(int i=0;i,java,Java,我试图学习如何改进我的代码编写,使用更少更优雅的方法来执行条件if-else语句。有没有什么方法可以改进这段代码,使它像专业人士一样更简短易读

我试图学习如何改进我的代码编写,使用更少更优雅的方法来执行条件if-else语句。有没有什么方法可以改进这段代码,使它像专业人士一样更简短易读
stringbasestr;
如果(n/10000==1){
//处理10001、10002、70024、80099等数字
baseStr=个(n/1000);
}否则{
baseStr=两位数(n/1000);
}
返回baseStr+“千”((n%1000<100)和“两位数(n%1000):+三位数(n%1000));

我想你可以像下面这样简写它

String baseStr;
if(n/10000 == 1) {
    //Handle numbers like 10001, 10002, 70024, 80099 etc 
    baseStr = ones(n/1000);
} else {
    baseStr = twoDigits(n/1000);
}
return baseStr + " Thousand " + ((n%1000 < 100) ? "And " twoDigits(n%1000) : + threeDigits(n%1000));
if(n/10000==1&&n%1000<100){
返回一(n/1000)+“千和”+两位数(n%1000);
}否则,如果(n%1000<100){
返回两位数字(n/1000)+“千和”+两位数字(n%1000);
}否则{
返回两位数(n/1000)+“千”+三位数(n%1000);
}

通过重构逻辑并逐段构建结果,您可以获得更简单的代码:

if(n/10000 == 1 && n%1000 < 100) {
    return ones(n/1000) + " Thousand And " + twoDigits(n%1000);
}else if(n%1000 < 100) {
    return twoDigits(n/1000)  + " Thousand And " + twoDigits(n%1000);
}else{
    return twoDigits(n/1000) + " Thousand " + threeDigits(n%1000);           
}
字符串结果=”;
如果(n/10000==1){
//处理10001、10002、70024、80099等数字
结果+=一(n/1000)+“千”;
}否则{
结果+=两位数(n/1000)+“千”;
}
如果(n%1000<100){
结果+=”和“+两位数(n%1000);
}否则{
结果+=“”+三位数(n%1000);
}
返回结果;
我希望它能帮助你

    String result = "";
    if (n/10000 == 1) {
        //Handle numbers like 10001, 10002, 70024, 80099 etc 
        result += ones(n/1000) + " Thousand";
    } else {
        result += twoDigits(n/1000)  + " Thousand";
    }
    if (n%1000 < 100) {
        result += " And " + twoDigits(n%1000);
    } else {
        result += " " + threeDigits(n%1000);
    }
    return result;
int numfodigits=n/1000;
字符串结果=String.valueOf(numOfDigits==1?个(numOfDigits):
    String result = "";
    if (n/10000 == 1) {
        //Handle numbers like 10001, 10002, 70024, 80099 etc 
        result += ones(n/1000) + " Thousand";
    } else {
        result += twoDigits(n/1000)  + " Thousand";
    }
    if (n%1000 < 100) {
        result += " And " + twoDigits(n%1000);
    } else {
        result += " " + threeDigits(n%1000);
    }
    return result;
    int numOfDigits = n/1000;
    String result = String.valueOf(numOfDigits == 1? ones(numOfDigits) : twoDigits(numOfDigits));

    String resultString = result + " Thousand " + twoDigits(n%1000);

    if(n%1000 < 100){
        resultString = result + " Thousand And " + threeDigits(n%1000);
    }

    return resultString;
int q = n / 1000;
int r = n % 1000;
return (q / 10 == 1 ? ones(q) : twoDigits(q)) + " Thousand "
        + (r < 100 ? "And " + twoDigits(r) : threeDigits(r));
    int nOverThousand = n / 1000;
    int nPercThousand = n % 1000;

    int twoDigitsTerm = twoDigits(nPercThousand);
    int threeDigitsTerm = threeDigits(nPercThousand);

    if(nOverThousand / 10 == 1){

        //Handle numbers like 10001, 10002, 70024, 80099 etc 
        int term1 = ones(nOverThousand);
        return (nPercThousand < 100) ?
                (term1 + " Thousand And " + twoDigitsTerm) :
                (term1 + " Thousand " + threeDigitsTerm);
    }
    else{
        int term1 = twoDigits(nOverThousand);
        return (nPercThousand < 100) ?
                (term1 + " Thousand And " + twoDigitsTerm) :
                (term1 + " Thousand " + threeDigitsTerm);
    }