gpt4 book ai didi

java - 如何分割我的用户字符串并在给定分割大小的情况下将它们排列在列和行中?

转载 作者:行者123 更新时间:2023-12-01 18:02:44 25 4
gpt4 key购买 nike

我几乎完成了这项 Activity ,我只需要根据用户的分割大小将它们排列在列和行中,并且我坚持只打印字符串本身的输出。这是我到目前为止的代码。

    Scanner lagoScan = new Scanner(System.in);

System.out.println("Enter String:");
String letters = lagoScan.next();



final int numInLetters = letters.length(); // converted string to number length

System.out.println("Enter Split Size:");
int splitSize = lagoScan.nextInt();




if (numInLetters % splitSize == 0) {

System.out.println("The Given String is: "+letters);
System.out.println("The Split String are:");

//My Split here

String []in_array;

in_array = letters.split(""); //Note this there is no delimiter
for(int k=0; k < in_array.length; k++){


System.out.print(" "+in_array[k]);

}


// Split ENdss
}

else {
System.out.println("Given input is not divisible by input size.");
}

}

这是我试图遵循的输出 enter image description here

最佳答案

这是一个可能的解决方案:

for (int i = 1; i <= inArray.length; i++) {
// print the current char.
System.out.print(inArray[i - 1]);

// if it's over the split size then print a line break
// for split size = 3
// i = 1 - print the char, i = 2, print the char, i = 3, print the char + line break
if (i % splitSize == 0) {
System.out.println();
}

}

关于java - 如何分割我的用户字符串并在给定分割大小的情况下将它们排列在列和行中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60602108/

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