Java字符串操作 x行,每行含有y个元素。

huangapple go评论120阅读模式
英文:

Java string manipulation x number of lines with y element

问题

以下是您提供的内容的翻译:

示例

  1. x=3 y=3
  2. 用户输入:
  3. 101
  4. 100
  5. 000

然后如何将字符串分离以将每个值输入不同的单元格。

编辑 1:

  1. import java.util.Scanner;
  2. public class RedVsGreen {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. int x, y;
  6. String line;
  7. String[] lineVector = new String[3];
  8. while (lineVector.length != 2 || (Integer.parseInt(lineVector[0]) >= 1000 || Integer.parseInt(lineVector[0]) <= 1)
  9. || (Integer.parseInt(lineVector[1]) <= 1 || Integer.parseInt(lineVector[1]) >= 1000)) {
  10. System.out.print("请输入x和y,用逗号分隔(大于1且小于1000):");
  11. // 读取x和y
  12. line = scanner.nextLine();
  13. // 通过逗号分隔所有值
  14. lineVector = line.split("\\s*,\\s*");
  15. }
  16. // 解析值为整数
  17. x = Integer.parseInt(lineVector[0]);
  18. y = Integer.parseInt(lineVector[1]);
  19. for (int i = 0; i < x; i++) {
  20. }
  21. }
  22. }

请注意,代码中可能存在其他错误,需要根据具体需求进行调试和修复。

英文:

Easiest way to explain my problem is to give you an example. Lets say I have 2 values X and Y. I wan't to ask the user to enter X lines with Y elements and they should be only 0s and 1s and then enter that values in array.

Example

  1. x=3 y=3
  2. User input:
  3. 101
  4. 100
  5. 000

And then how to separate string to enter each value in different cell.

EDIT 1:

  1. import java.util.Scanner;
  2. public class RedVsGreen {
  3. public static void main(String[] args) {
  4. Scanner scanner=new Scanner(System.in);
  5. int x,y;
  6. String line;
  7. String[] lineVector = new String[3];
  8. while ( lineVector.length !=2 || (Integer.parseInt(lineVector[0]) &gt;= 1000 || Integer.parseInt(lineVector[0]) &lt;= 1)
  9. || (Integer.parseInt(lineVector[1]) &lt;= 1 || Integer.parseInt(lineVector[1]) &gt;= 1000)){
  10. System.out.print(&quot;Please enter x and y, comma separated (more than 1 and less than 1000):&quot;);
  11. //read x,y
  12. line = scanner.nextLine();
  13. //separate all values by comma
  14. lineVector = line.split(&quot;\\s*,\\s*&quot;);
  15. }
  16. //parsing the values to Integer
  17. x = Integer.parseInt(lineVector[0]);
  18. y = Integer.parseInt(lineVector[1]);
  19. for (i = 0 ; i &lt; x; i++){
  20. }
  21. //
  22. // int[][] field = new int[x][y];
  23. // for (int row = 0; row &lt; field.length; row++) {
  24. // System.out.println(&quot;&quot;);
  25. // for (int col = 0; col &lt; field[row].length; col++) {
  26. // field[row][col] = 9; //dummy value
  27. // System.out.print(field[row][col] + &quot; &quot;);
  28. // }
  29. // }
  30. //
  31. }
  32. }

答案1

得分: 1

  1. **接收 x y**
  2. 在这个问题中你的 while 条件过于复杂使用 do while 格式更适合因为你至少会获取一次值而且代码更可读
  3. 另外你可以使用 x 来代替 `Integer.parseInt(lineVector[0])`y 也是同样的道理),而不是重复这个过程这样可以缩短条件判断
  4. ```java
  5. String line;
  6. String[] lineVector;
  7. int x = -1, y = -1;
  8. do {
  9. System.out.print("请输入 x 和 y,以逗号分隔(大于1且小于1000):");
  10. line = scanner.nextLine();
  11. lineVector = line.split("\\s*,\\s*");
  12. if (lineVector.length != 2)
  13. continue;
  14. x = Integer.parseInt(lineVector[0]);
  15. y = Integer.parseInt(lineVector[1]);
  16. } while (!((x > 1 && x < 1000 && y > 1 && y < 1000)));

首先,我删除了 lineVector 的初始值,因为在这种情况下初始化是不必要的(你之前需要这样做是因为它出现在 while 条件中)。

我将 xy 初始化为 -1(任何不在我们范围内的数字都可以),以确保 do-while 条件在为这两个数字提供适当值之前得到满足。

检查二进制值的函数

创建一个函数来检查字符串值是否为二进制。

  1. public static boolean isBinary(String s) {
  2. for (int i = 0; i < s.length(); ++i) {
  3. char c = s.charAt(i);
  4. if (c != '0' && c != '1')
  5. return false;
  6. }
  7. return true;
  8. }

接收二进制值

  1. String[] binaries = new String[x];
  2. System.out.println("输入长度为 " + y + " 的二进制值:");
  3. // 获取二进制值
  4. for (int i = 0; i < x; ++i) {
  5. binaries[i] = scanner.nextLine();
  6. while (!isBinary(binaries[i]) || binaries[i].length() != y) {
  7. System.out.println("无效的二进制值。请重新输入新值:");
  8. binaries[i] = scanner.nextLine();
  9. }
  10. }

接收二进制值,并在无效时继续要求输入。

填充二维数组

  1. int[][] field = new int[x][y];
  2. for (int i = 0; i < x; ++i) {
  3. System.out.println();
  4. for (int j = 0; j < y; ++j) {
  5. char c = binaries[i].charAt(j);
  6. if (c == '0')
  7. field[i][j] = 0;
  8. else
  9. field[i][j] = 1;
  10. System.out.print(field[i][j] + " ");
  11. }
  12. }

在这里,Integer.parseInt 是不必要的,因为只有两个可能的值(0 和 1)。

  1. <details>
  2. <summary>英文:</summary>
  3. **Receiving x and y**
  4. while ( lineVector.length !=2 ...)
  5. Your while condition is too complicated, a do while format matches this problem much better since you are going to get the values at least once, and the code is more readable.
  6. Also you could have used x instead of `Integer.parseInt(lineVector[0])` (same for y) instead of repeating the process, this would have shortened the condition.
  7. String line;
  8. String[] lineVector;
  9. int x = -1, y = -1;
  10. do {
  11. System.out.print(&quot;Please enter x and y, comma separated (more than 1 and less than 1000):&quot;);
  12. line = scanner.nextLine();
  13. lineVector = line.split(&quot;\\s*,\\s*&quot;);
  14. if(lineVector.length != 2)
  15. continue;
  16. x = Integer.parseInt(lineVector[0]);
  17. y = Integer.parseInt(lineVector[1]);
  18. } while (!((x &gt; 1 &amp;&amp; x &lt; 1000 &amp;&amp; y &gt; 1 &amp;&amp; y &lt; 1000)));
  19. First of all I removed the initial value of `lineVector` as it is unnecessary to initialize it in this case (you needed to do it because of it being present in your while condition).
  20. I initialized `x` and `y` to -1 (Any number not in our range would work) in order to make sure the do-while condition is fulfilled until proper values are offered for both of the numbers.
  21. **Function to check binary values**
  22. Create a function to check if string values are binary.
  23. public static boolean isBinary(String s) {
  24. for(int i = 0; i &lt; s.length(); ++i) {
  25. char c = s.charAt(i);
  26. if(c != &#39;0&#39; &amp;&amp; c != &#39;1&#39;)
  27. return false;
  28. }
  29. return true;
  30. }
  31. **Receive binaries**
  32. String[] binaries = new String[x];
  33. System.out.println(&quot;Enter binaries with length &quot; + y + &quot; :&quot;);
  34. //Get binaries
  35. for(int i = 0; i &lt; x; ++i) {
  36. binaries[i] = scanner.nextLine();
  37. while(!isBinary(binaries[i]) || binaries[i].length() != y) {
  38. System.out.println(&quot;Invalid binary value. Re-enter new value:&quot;);
  39. binaries[i] = scanner.nextLine();
  40. }
  41. }
  42. Receive binary values and continue asking if invalid.
  43. **Filling the 2D array**
  44. int[][] field = new int[x][y];
  45. for(int i = 0; i &lt; x; ++i) {
  46. System.out.println();
  47. for(int j = 0; j &lt; y; ++j) {
  48. char c = binaries[i].charAt(j);
  49. if(c == &#39;0&#39;)
  50. field[i][j] = 0;
  51. else
  52. field[i][j] = 1;
  53. System.out.print(field[i][j] + &quot; &quot;);
  54. }
  55. }
  56. `Integer.parseInt` is not necessary here as there are only two possible values (0 and 1)
  57. </details>

huangapple
  • 本文由 发表于 2020年7月25日 19:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63087796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定