英文:
Box pattern made of Xs and Os in java
问题
public static String textBoxString(int rows, int cols, char c1, char c2) {
    char temp = 0;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= cols; j++) {
            if (i == 1 || i == rows) {
                System.out.print(c1);
                temp = c1;
            } else if (j == 1 || j == cols) {
                System.out.print(c1);
                temp = c1;
            } else {
                System.out.print(" ");
                temp = c2;
            }
        }
        System.out.println();
    }
    String sideString = Integer.toString(rows) + Integer.toString(cols);
    return sideString;
}
Outputs:
xoxox
o   x
oxoxo
英文:
I'm trying to make a box pattern in java with a pattern that looks like
xoxox
o   x
oxoxo
I can get close with alternating x and o on the top and bottom, but there is a line of Os in between that I'm struggling to get rid of. Here is my code so far:
public static String textBoxString(int rows, int cols, char c1, char c2) {
		char temp = 0;
		for (int i = 1; i <= rows; i++) {
			for (int j = 1; j <= cols; j++) {
				if (i == 1 || i == rows) {
					System.out.print(c1);
					temp = c1;
				}
				if (temp == c1) {
					System.out.print(c2);
					
				}
				else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		String sideString = Integer.toString(rows, cols);
		return sideString;
	}
Outputs:
xoxoxoxoxo
ooooo
xoxoxoxoxo
答案1
得分: 0
以下是使用repeat()方法减少重复代码的替代实现。
此代码在边框周围正确打印交替字符。在结尾处的测试证明它适用于行数和列数的奇/偶数任意组合。
适用于 Java 11+ 的解决方案:
public static void textBoxString(int rows, int cols, char c1, char c2) {
    System.out.println((String.valueOf(c1) + c2).repeat(cols / 2 + 1).substring(0, cols));
    String spaces = " ".repeat(cols - 2);
    for (int i = 2; i < rows; i++)
        System.out.println((i % 2 == 0 ? c2 : c1) + spaces + ((i + cols) % 2 == 0 ? c1 : c2));
    System.out.println((rows % 2 == 0 ? String.valueOf(c2) + c1 : String.valueOf(c1) + c2).repeat(cols / 2 + 1).substring(0, cols));
}
相同的逻辑,但适用于任何 Java 版本,使用本地的 repeat() 助手方法:
public static void textBoxString(int rows, int cols, char c1, char c2) {
    System.out.println(repeat(String.valueOf(c1) + c2, cols / 2 + 1).substring(0, cols));
    String spaces = repeat(" ", cols - 2);
    for (int i = 2; i < rows; i++)
        System.out.println((i % 2 == 0 ? c2 : c1) + spaces + ((i + cols) % 2 == 0 ? c1 : c2));
    System.out.println(repeat(rows % 2 == 0 ? String.valueOf(c2) + c1 : String.valueOf(c1) + c2, cols / 2 + 1).substring(0, cols));
}
private static String repeat(String s, int count) {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < count; i++)
        buf.append(s);
    return buf.toString();
}
测试
textBoxString(3, 5, 'x', 'o');
textBoxString(4, 5, 'x', 'o');
textBoxString(4, 6, 'x', 'o');
textBoxString(5, 6, 'x', 'o');
输出
xoxox
o   o
xoxox
xoxox
o   o
x   x
oxoxo
xoxoxo
o    x
x    o
oxoxox
xoxoxo
o    x
x    o
o    x
xoxoxo
英文:
Here is an alternate implementation that uses a repeat() method to reduce repetitive code.
This code correctly prints alternating characters around the border. See tests at the end for proof that it works for any combination of odd/even numbers of rows and columns.
Solution for Java 11+:
public static void textBoxString(int rows, int cols, char c1, char c2) {
	System.out.println((String.valueOf(c1) + c2).repeat(cols / 2 + 1).substring(0, cols));
	String spaces = " ".repeat(cols - 2);
	for (int i = 2; i < rows; i++)
		System.out.println((i % 2 == 0 ? c2 : c1) + spaces + ((i + cols) % 2 == 0 ? c1 : c2));
	System.out.println((rows % 2 == 0 ? String.valueOf(c2) + c1 : String.valueOf(c1) + c2).repeat(cols / 2 + 1).substring(0, cols));
}
Same logic, but for any Java version, using a local repeat() helper method:
public static void textBoxString(int rows, int cols, char c1, char c2) {
	System.out.println(repeat(String.valueOf(c1) + c2, cols / 2 + 1).substring(0, cols));
	String spaces = repeat(" ", cols - 2);
	for (int i = 2; i < rows; i++)
		System.out.println((i % 2 == 0 ? c2 : c1) + spaces + ((i + cols) % 2 == 0 ? c1 : c2));
	System.out.println(repeat(rows % 2 == 0 ? String.valueOf(c2) + c1 : String.valueOf(c1) + c2, cols / 2 + 1).substring(0, cols));
}
private static String repeat(String s, int count) {
	StringBuilder buf = new StringBuilder();
	for (int i = 0; i < count; i++)
		buf.append(s);
	return buf.toString();
}
Tests
textBoxString(3, 5, 'x', 'o');
textBoxString(4, 5, 'x', 'o');
textBoxString(4, 6, 'x', 'o');
textBoxString(5, 6, 'x', 'o');
Outputs
xoxox
o   o
xoxox
xoxox
o   o
x   x
oxoxo
xoxoxo
o    x
x    o
oxoxox
xoxoxo
o    x
x    o
o    x
xoxoxo
答案2
得分: -1
这个流程将解决您的问题:
import java.util.*;
import java.lang.*;
import java.io.*;
// 主方法必须在名为 "Main" 的类中。
class Main {
    public static void main(String[] args) {
        var result = textBoxString(4, 6, 'x', 'o');
        System.out.println("Hello world!");
    }
    public static String textBoxString(int rows, int cols, char c1, char c2) {
        char temp = 0;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                char toPrint = j % 2 == 0 ? c1 : c2;
                boolean isEmpty = true;
                if ((i == 1 || i == rows) || (i > 1 && i < rows && (j == 1 || j == cols))) {
                    isEmpty = false;
                }
                if (!isEmpty) {
                    System.out.print(toPrint);
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        String sideString = Integer.toString(rows, cols);
        return sideString;
    }
}
英文:
This flow will solve your problem :
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
                var result = textBoxString(4,6,'x','o');
        System.out.println("Hello world!");
    }
    
      
    public static String textBoxString(int rows, int cols, char c1, char c2) {
        char temp = 0;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                char toPrint = j%2==0?c1:c2;
                boolean isEmpty=true;
                if ((i == 1 || i == rows) || (i >1 && i < rows &&(j==1 || j==cols))) {
                    isEmpty=false;
                }
                
                
                if (!isEmpty) {
                    System.out.print(toPrint);
                    
                }
                else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        String sideString = Integer.toString(rows, cols);
        return sideString;
    }
}
答案3
得分: -1
我希望你需要类似这样的东西
    public static String textBoxString(int rows, int cols, char c1, char c2) {
        char temp;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                if (1 == i || rows == i) {
                    System.out.print(c1);
                    System.out.print(c2);
                } else if (1 == j) {
                    System.out.print(c1 + " ");
                } else if (cols == j) {
                    System.out.print(" " + c2);
                } else {
                    System.out.print("  ");
                }
            }
            temp = c1;
            c1 = c2;
            c2 = temp;
            System.out.println();
        }
        return Integer.toString(rows) + Integer.toString(cols);
    }
英文:
I hope you need something like this
    public static String textBoxString(int rows, int cols, char c1, char c2) {
        char temp;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                if (1 == i || rows == i) {
                    System.out.print(c1);
                    System.out.print(c2);
                } else if (1 == j) {
                    System.out.print(c1 + " ");
                } else if (cols == j) {
                    System.out.print(" " + c2);
                } else {
                    System.out.print("  ");
                }
            }
            temp = c1;
            c1 = c2;
            c2 = temp;
            System.out.println();
        }
        return Integer.toString(rows, cols);
    }
答案4
得分: -1
public static String textBoxString(int rows, int cols, char c1, char c2) {
    String box = "";
    boolean firstChar = true;
    for (int row = 1; row <= rows; row++) {
        for (int col = 1; col <= cols; col++) {
            box = box + ((row == 1 || row == rows || col == 1 || col == cols) ? (firstChar ? c1 : c2) : " ");
            firstChar = !firstChar;
        }
        box = box + System.lineSeparator();
    }
    return box;
}
英文:
I'd write it as:
  public static String textBoxString(int rows, int cols, char c1, char c2) {
    String box = "";
    boolean firstChar = true;
    for(int row=1; row<=rows; row++){
      for(int col=1; col<=cols; col++) {
        box = box + ((row==1 || row==rows || col==1 || col==cols) ? (firstChar ? c1 : c2) : " ");
        firstChar = !firstChar;
      }
      box = box + System.lineSeparator();
    }
    return box;
  }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论