gpt4 book ai didi

Java二维输出矩阵

转载 作者:行者123 更新时间:2023-12-02 06:21:22 27 4
gpt4 key购买 nike

我希望你帮助我,这是我的代码:

public static void main(String[]args)
{
Scanner input = new Scanner(System.in);

System.out.print("Type your text: ");
String text = input.nextLine();

int counter = text.length();
if(text.length()> 16)
{
System.out.println("Error: input text is greater than 16 characters");
System.exit(0);
}
else
{
while(counter < 16)
{
text = text.concat("x");
counter++;
}

char[][] k = new char[4][4];

//int push = 0;

int push;

for (int i = 0; i < k.length; i++) {
push = i;
for (int j = 0; j < k[i].length; j++) {
k[i][j] = text.charAt(push);
System.out.print(k[i][j] + " ");
push = push + 4;
}
System.out.println();
}
}

}

现在,当我输入: abcdefghijklmn 时,输出为:

a e i m
b f j n
c g k x
d h l x

这很完美,当我打印时它看起来像矩阵。但现在我必须以其他方式打印:

aeim, bfjn, cgkx, dhlx

不是矩阵,而是行内(带逗号)

最佳答案

只需将 System.out.println(); 替换为 System.out.print(", ");

关于Java二维输出矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20978412/

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