gpt4 book ai didi

java - 打印出一个字符串数组,但每次替换一个元素。 java

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:00 24 4
gpt4 key购买 nike

如何多次打印一个字符串数组,并且每次都用“-”替换其中一个单词?如果数组有“Hello”“Hi”“Hey”那么它应该打印

“-嗨嘿”

“你好 - 嘿”

“你好嗨-”

这就是我目前所拥有的

 public class SkipArgs
{
public static void main(String[] args)
{
int capacity = 1;
capacity += args.length;
String[] str = new String[capacity];

for(int i=0; i<args.length; i++)
{
for (int j=0; j<args.length; j++)
{str[j] = args[j];
printExceptOne(str[j], i);
}


}
}

public static void printExceptOne(String str, int j)
{

System.out.print(str+" ");
}
}

我不知道如何用“-”替换

最佳答案

外部循环应控制不打印哪个数组元素。所以它可能看起来像这样:

for (int i = 0; i < args.length; i++) {
for (int j = 0; j < args.length; j++) {
// first print a separator if not the first element on the line
if (j > 0) System.out.print(" ");

// now print either the j-th arg or a "-"
if (j == i) {
System.out.print("-");
} else {
System.out.print(args[j]);
}
}
System.out.println();
}

关于java - 打印出一个字符串数组,但每次替换一个元素。 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com