Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中由Xs和Os组成的Box模式_Java - Fatal编程技术网

java中由Xs和Os组成的Box模式

java中由Xs和Os组成的Box模式,java,Java,我正在尝试用java制作一个盒子模式,这个模式看起来像 xoxox o x oxoxo 我可以在顶部和底部交替使用x和o,但在两者之间有一系列操作系统,我正努力摆脱它们。以下是我目前的代码: public static String textBoxString(int rows, int cols, char c1, char c2) { char temp = 0; for (int i = 1; i <= rows; i++) {

我正在尝试用java制作一个盒子模式,这个模式看起来像

xoxox
o   x
oxoxo
我可以在顶部和底部交替使用x和o,但在两者之间有一系列操作系统,我正努力摆脱它们。以下是我目前的代码:

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;
    }

此流程将解决您的问题:

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;
    }
}
import java.util.*;
导入java.lang.*;
导入java.io.*;
//main方法必须位于名为“main”的类中。
班长{
公共静态void main(字符串[]args){
var result=textBoxString(4,6,'x','o');
System.out.println(“你好,世界!”);
}
公共静态字符串textBoxString(int行、int列、字符c1、字符c2){
字符温度=0;

对于(inti=1;i我希望您需要这样的东西

publicstaticstringtextboxstring(int行、int列、charc1、charc2){
焦炭温度;
对于(inti=1;i我将其写成:

  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;
  }
publicstaticstringtextboxstring(int行、int列、charc1、charc2){
字符串框=”;
布尔值firstChar=true;

对于(int row=1;row,这里有一个替代实现,它使用
repeat()
方法来减少重复代码

此代码正确地在边框周围打印交替字符。请参阅末尾的测试,以证明它适用于奇数/偶数行和列的任意组合

Java 11+的解决方案:

publicstaticvoidtextboxstring(int行、int列、charc1、charc2){
System.out.println((String.valueOf(c1)+c2).重复(cols/2+1).子字符串(0,cols));
字符串空格=“”。重复(cols-2);
for(int i=2;i
相同的逻辑,但对于任何Java版本,使用本地
repeat()
helper方法:

publicstaticvoidtextboxstring(int行、int列、charc1、charc2){
System.out.println(重复(字符串值(c1)+c2,cols/2+1).子字符串(0,cols));
字符串空格=repeat(“,cols-2);
for(int i=2;i
测验

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
氧代
xoxo
氧
xO
奥克斯
xoxo
氧
xO
氧
xoxoxo

我的意思是真诚的:要解决这些bug,你能做的最重要的事情就是学习如何在调试器中运行代码。你是否使用任何类型的IDE来编写代码?如果是这样,它很可能有一个调试器。如果你不使用and IDE(IntelliJ或Eclipse等)那么我强烈建议你开始使用一个。祝你好运。如果在第一行或最后一行(你有这个)或第一列或最后一列,只打印
c1
c2
。否则打印空格。或使用单行
System.out.print((1!=I&&rows!=I)?1!=j?cols!=j?”:(+c2):(c1+):(c1+):(c1+):(c1+)+c2));
结果不正确,因为它打印的列数是原来的两倍,这会阻止它打印奇数列,如问题中所示的5列。结果不正确,因为字符不会沿两边交替。仅供参考:您可能应该使用普通的
\n
@Andreas来代替硬编码。它是怎么“不正确”的对于偶数列?规范没有非常具体地说明“交替”应该如何发生,如果你看看他们的例子,上/下交替是不一致的。我只是继续上一行到下一行的交替。=)@Andreas感谢
lineSeparator()
tip,我会做出改变。我告诉过你偶数是错误的,所以你自己试过看会发生什么吗?我不必解释,因为当你运行它时,代码会清楚地显示问题!!!例如,你可以使用中显示的4个测试用例,或者用偶数列做你自己的测试用例。--当
cols
值是奇数,此代码将边打印为
o
x
的交替行,但当
cols
值为偶数时,边都是
x
,没有交替。好吧,你告诉我“再看一下最上面原始帖子中的框”是的,我在我的回答中没有遵循这一点,但在这个回答中,你也没有遵循这一点,那么你实际上打算用这个参考来说什么呢?我们都应用了解释,但让所有行打印相同的内容,没有交替,对我来说似乎是非常错误的(是的,我的意见)对于一个关于交替的问题。
  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;
  }