我正在练习Java,但对在String数组中打印所有值有疑问。我将需要打印所有已创建的String数组中的内容。
我似乎无法使“ Arrays.toString”在我的代码中正常工作,并且我不想指出数组中的每个值。有什么办法可以一次打印全部?
public class MyCode {
public static void main(String[] args) {
String[] majority = {"Congrats, you may enter!", " Awesome!"};
String[] minority = {"I'm sorry, you are too young.", " Ah that's too bad!"};
int permition = 17;
boolean authorization = (permition >= 18);
if (authorization == true) {
System.out.println(majority[0] + majority[1]);
} else {
System.out.println(minority[0] + minority[1]);
}
}
}
因此,如果该人大于或等于18,则结果应为“恭喜,您可以输入!太棒了!”,如果小于18,则结果应为“对不起,您太年轻。这太糟糕了! “。
答案 0 :(得分:0)
您可以尝试
List<String>majorities =
Arrays.asList(majority);
List<String>minorities =
Arrays.asList(minority);
String str_majority = String.join("",
majorities);
String str_minority = String.join("",
minorities);
String final = str_majority +
str_minority