如何将数组中的0值更改为其他值 [英] How to change value of 0 in array to other value

查看:98
本文介绍了如何将数组中的0值更改为其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当子串值大于0时,如何更改代码,但如果小于或等于0将自动转为值3



我尝试了什么:



  double  rulesfpadouble = 1.45779894373497397E20; 
String [] too = String .valueOf(rulesfpadouble).replaceAll( \\ D )。split( );
int [] remainingders = new int [too.length];
int count;

for int i = 0 ; i< too.length; i ++)
{
if (too [i] == 0){

remainingders [i] =((Integer.parseInt(也是) [i])+ 1)* 3 );

}
else if (too [i]!= 0
{
remainingders [i] =(Integer.parseInt (太[i])% 2 );
}
// System.out.println(剩余人数:\t+剩余人数[i]);
}

// 系统。 out.println(Arrays.toString(remainingders));

str = Arrays.toString(remainingders).replaceAll( \\\\ +
.replace( // 删除逗号
.replace( [ // 删除右括号
.replace( ] // 删除左括号
.trim(); // 从部分初始化的数组中删除尾随空格

System.out.println ( 下一代规则:\t + str);

解决方案

我现在指的是你的例子。在你的例子中,你测试的数字是

 double rulesfpadouble = 1.45779894373497397E20; 



所以你的变量看起来像

 = [1,4,5,7,7,9,8,9,4,3,7,3,9,2,0]; 



所以我猜你希望你的余数是:

余数= [1,0,1,1,1,1,0,1,0,1,1,1,0 ,1,1,1,1,0,3]; 



您可以使用以下代码实现此目的(我稍微简化了您的代码):

 double rulesfpadouble = 1.45779894373497397E20; 
String [] too = String.valueOf(rulesfpadouble).replaceAll(\\ D,)。split();
int [] remainingders = new int [too.length];
for(int i = 0; i< too.length; i ++){
int numberToTest = Integer.parseInt(too [i]);
if(numberToTest == 0){
remainingders [i] = 3;
} else {
remainingders [i] =(Integer.parseInt(too [i])%2);
}
}



在您的代码中,零测试失败。那是因为你使用了 String.valueOf()方法。如果你想直接测试字符串,你必须使用来[i] .equals(0)

希望这会有所帮助。


How to change code when the substring value more than 0 will be modulo 2 but if less or equal than 0 will automatically turn to value 3

What I have tried:

double rulesfpadouble =1.45779894373497397E20;
   String[] too = String.valueOf(rulesfpadouble).replaceAll("\\D", "").split("");
              int[] remainders = new int[too.length];
            int count;

               for (int i = 0; i < too.length; i++)
               {
                   if(too[i]=="0"){

                   remainders[i] = ((Integer.parseInt(too[i])+1)*3);

                   }
                   else if (too[i]!="0")
                   {
                 remainders[i] = (Integer.parseInt(too[i]) % 2);
                   }
              //System.out.println("Remainders :\t"+ remainders[i]);
               }

              //System.out.println(Arrays.toString(remainders));

               str = Arrays.toString(remainders).replaceAll("\\s+","")
              .replace(",", "")  //remove the commas
              .replace("[", "")  //remove the right bracket
              .replace("]", "")  //remove the left bracket
              .trim();           //remove trailing spaces from partially initialized array

            System.out.println("Rule of next gen:\t"+ str);

解决方案

I'm referring to your example now.In your example the number you test is

double rulesfpadouble =1.45779894373497397E20;


so your variable looks like

too = [1, 4, 5, 7, 7, 9, 8, 9, 4, 3, 7, 3, 9, 2, 0];


So I guess you expect your remainder to be:

remainder = [1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 3];


You can achive this with the following code (I smiplified your code a little bit):

double rulesfpadouble = 1.45779894373497397E20;
String[] too = String.valueOf(rulesfpadouble).replaceAll("\\D", "").split("");
int[] remainders = new int[too.length];
for (int i = 0; i < too.length; i++) {
    int numberToTest = Integer.parseInt(too[i]);
    if (numberToTest == 0) {
	remainders[i] = 3;
    } else {
	remainders[i] = (Integer.parseInt(too[i]) % 2);
    }
}


In your code the test for zero failed. That is because you yoused the String.valueOf() method. If you want to test the string directly you must use too[i].equals("0").
Hope this helps.


这篇关于如何将数组中的0值更改为其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆