gpt4 book ai didi

java - 获取多个字符串数组并将其写入一行打印

转载 作者:行者123 更新时间:2023-12-02 10:14:03 25 4
gpt4 key购买 nike

我想知道是否可以将所有字符串数组的结果打印到一个 println 而不是多个中。

    String[] test1 = { "a", "b", "c" };  
String[] test2 = { "a", "B", "c", "D", "e" };
String[] test3 = { "A", "B", "C", "D" };
String[] test4 = { "$", "B", "c", "6", "D", "\n", "E" };

int upperCaseCount1 = countUppercase(test1); //Instead of printing this
System.out.println(upperCaseCount1);
int upperCaseCount2 = countUppercase(test2);
System.out.println(upperCaseCount2);
int upperCaseCount3 = countUppercase(test3);
System.out.println(upperCaseCount3);
int upperCaseCount4 = countUppercase(test4);
System.out.println(upperCaseCount4);

//am i able to use a single line print? like....my failed attempts here.
System.out.println(countUppercase("test1", "test2","test3", "test4");

最佳答案

您可以在 println 中连接结果

int upperCaseCount1 = countUppercase(test1);
int upperCaseCount2 = countUppercase(test2);
int upperCaseCount3 = countUppercase(test3);
int upperCaseCount4 = countUppercase(test4);

System.out.println( upperCaseCount1 +", "+upperCaseCount2 +", "+upperCaseCount3 +", "+upperCaseCount4);

如果你可以使用 printf 代替,你也可以这样做:

System.out.printf( "Results: %d, %d, %d, %d%n", upperCaseCount1, upperCaseCount2, upperCaseCount3, upperCaseCount4); 

%d 表示 int,%n 表​​示换行。 simple tutorial .

您还可以使用 StringBuilder。 tutorial .

StringBuilder builder = new StringBuilder("Results: ");
builder.append(countUppercase(test1)).appende(" ").
append(countUppercase(test2)).appende(" ").
append(countUppercase(test3)).appende(" ").
append(countUppercase(test4));
System.out.println( builder);

关于java - 获取多个字符串数组并将其写入一行打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54798591/

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