java - java中如何用字符替换整数数组

标签 java arrays integer character

int=0111254

将 0 全部替换为“z”

用“p”替换奇数

用“q”替换偶数

输出应为 zpppqpq

我的部分代码......

public static void main(String[] args) {
    int num;
    Scanner sc=new Scanner(System.in);
    num=sc.nextInt();
    int temp;
    int b[]=new int[10];
    char a[]=new char[10];


    for(int i=0;i<b.length;i++) {
        while(num!=0)
        {
            temp=num%10;

            b[i]=temp;

            num=num/10;
        }
    }

    for(int i=0;i<b.length;i=i+2)
    {
        if(b[i]==0)
        {
            b[i]=115;
        }

        else if(b[i]%2!=0)
        {
            b[i]=113;
        }
        else if(b[i]%2==0) {
            b[i]=112;
        }
    }

    for(int i=0;i<a.length;i++)
    {
        a[i]=(char)b[i];
    }

    for(int i:a)
    {
        System.out.print((char)i);
    }

它给出了错误的输出 q s s s s

最佳答案

您可以将该整数转换为字符串,然后使用 String.replace()

String numberString = ""+0111254;
// Replace all 0 chars with z
numberString.replace('0','z');
// etc...

关于java - java中如何用字符替换整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61411096/

相关文章:

java - Esper XML dom 文档错误

c++ - 为什么 MSVC 编译器不检测 std::array 越界访问

javascript - 数组中的随机文本字符串

c# - 在 C# 中处理整数溢出的最佳方法?

C编程-unsigned int溢出问题

python - python 中的整数自增

java - Firebase 用户注册问题

java - 使用 guice 为客户端 sdk/library 设计模式进行依赖注入(inject)

java - getContentLength() 仅在 WiFi 中返回 -1?

arrays - 迭代数据数组/哈希