Writing a static method in Java to return a string using loops

半世苍凉 提交于 2020-06-16 06:39:31

问题


The full assignment is:

Write two public classes (named exactly), TextBox and TextBoxTester. TextBox contains the following overloaded static methods called textBoxString. This method returns a String value.

public static String textBoxString (int side)

The returned String value, when printed, displays as the outline of a square of side characters. The character you use is up to you. Don't forget that '\n' will force a newline character into the returned String. For example, let's assume I want to use * as the character for my box:

     String s =  textBoxString(3); 

     System.out.println(s);

will print

xxx  
x x  
xxx  

public static String textBoxString(int side, char bChar)

The returned String value, when printed, displays the outline of a square of side characters using bChar as the box character. For example,

     String s = textBoxString(4, '+'); 

     System.out.println(s);

will print

++++   
+  +  
+  +  
++++

So far, this is what I have:

public class TextBox {

    public static String textBoxString(int side) {
        int n = side;
        String string = "";
        for (int i = 0; i < n; i++) {
            if (i == 0 || i == n - 1) {
                string = "***";
            }
            else {
                string = "*    *";
            }
        }
        return string;

    }

    public static String textBoxString(int side, char bChar) {
        int n = side;
        char c = bChar;
        String string = "";
        for (int i = 0; i < n; i++) {
            if (i == 0 || i == n -1) {
                string = c + " " + c + " " + c + " " + c;
            }
            else {
                string = c + "      " + c;
            }
        }
        return string;
    }
}

My issue is returning the result. I don't know how to return the result that I need, that I would get from running this in a main method (for the first method):

int n = 3;
for (int i = 0; i < n; i++) {
    if (i == 0 || i == n - 1) {
        System.out.println("***");
    }
    else {
        System.out.println("*  *");
    }
}

回答1:


Welcome to StackOverflow!

To make textBoxString work for varying values for side, you cannot use the hardcoded string "***" because that will only work when side equals 3.

You'll need to use nested loops to accomplish this.

I'm not gonna write the complete code for you, but I'm gonna give you a hint.

if (i == 0 || i == n - 1) {
    // Use a for loop here to construct the top and bottom rows
}
else {
    string = "*    *";
}



回答2:


You can use nested loops to achieve this.

public static String textBoxString(int n, char c) {
 String output = "";
 for(int i = 0; i < n; i++) { // constructing all rows
  output += c;
  for(int j = 1; j < n-1; j++) { // constructing a single row
     if(i == 0 || i == n - 1)
       output += " " + c; // use character to fill up each element in the row
     else
       output += " "; // use space to fill up
  }
  output += " " + c +"\n";
 }
return output;
}

Run and print

x x x x x
x       x
x       x
x       x
x x x x x



回答3:


You could add "\n" to your string, it will start a new line.

for (int i = 0; i < n; i++) {
        if (i == 0 || i == n -1) {
            string += c + " " + c + " " + c + " " + c + "\n";
        }
        else {
            string += c + "      " + c + "\n";
        }
    }
}

After adding that in, return string




回答4:


Your function should be as follows:

public static String textBoxString(int side, char bChar) {
    String string = "";

    for (int i = 0; i < side; i++) {
        // First character of the line
        string += bChar;

        // Characters between the first and the last character
        for (int j = 0; j < side - 2; j++) {
            if (i == 0 || i == side - 1) {
                string += bChar;
            } else {
                string += ' ';
            }
        }

        // Last character of the line with line-break
        string += bChar + "\n";
    }

    return string;
}

Note that you should use StringBuilder instead of String for string concatenation inside a loop but I'm not sure if you are allowed to do so.

By using the ternary operator, you can reduce the code to fewer lines as follows:

public static String textBoxString(int side, char bChar) {
    String string = "";

    for (int i = 0; i < side; i++) {
        // First character of the line
        string += bChar;

        // Characters between the first and the last character
        for (int j = 0; j < side - 2; j++) {
            string += (i == 0 || i == side - 1) ? bChar : ' ';
        }

        // Last character of the line with line-break
        string += bChar + "\n";
    }

    return string;
}

How to use it?

public class TextBoxTester {
    public static void main(String[] args) {
        String s = TextBox.textBoxString(4, '+');
        System.out.println(s);
    }
}


来源:https://stackoverflow.com/questions/62349930/writing-a-static-method-in-java-to-return-a-string-using-loops

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!