英文:
Given a rectangular matrix of characters, add a border of asterisks(*) to it
问题
picture = ["abc",
"ded"]
输出应为:
addBorder(picture) = ["*****",
"*abc*",
"*ded*",
"*****"]
我尝试对另一个数组进行操作,并在第i个元素上附加'*'
for (int i=0; i<arrlen+2; i++)
if(i==0 || i==arrlen+1){
for(int j = 0; j<len+2; j++)
pictures[i] = pictures[i].append("*")
还有
else {
for(int j = 0; j<len+2; j++)
if(j==0 || i==len+1)
pictures[i] = pictures[i].append("*");
但是它在.append()
处显示“无法找到符号”。
英文:
Example
For
picture = ["abc",
"ded"]
the output should be
addBorder(picture) = ["*****",
"*abc*",
"*ded*",
"*****"]
I tried to another array and append the '*' ith elements
for (int i=0; i<arrlen+2; i++)
if(i==0 || i==arrlen+1){
for(int j = 0; j<len+2; j++)
pictures[i] = pictures[i].append("*")
and
else {
for(int j = 0; j<len+2; j++)
if(j==0 || i==len+1)
pictures[i] = pictures[i].append("*");
But it says "cannot find symbol" at the .append()
答案1
得分: 2
你可以在不使用.append
的情况下简化这个过程。使用一个for
循环来打印一行星号,该行的长度应为字符串的长度加2(每侧额外增加1个)。要获取字符串的长度,请使用.length()
。然后使用以下代码在字符串前后打印一个星号:
System.out.println("*" + string + "*");
并且打印与第一部分相同的星号行。
对于打印字符串数组,同样使用一个for
循环来打印每个值,并使用 System.out.println("*" + string[i] + "*");
,然后跟上用于顶部边框的相同星号行。
根据你的问题,我认为你想要将结果存储在一个新数组中。我开始编写一个示例代码,并且不禁将整个代码都写出来了:
public static void main(String[] args) {
String text[] = {"abc", "def", "ghi", "jkl"};
String framedString[] = {"", "", "", "", "", "", ""}; // Java数组的大小是固定的,
// 所以我必须定义6个空值
String asterisks = "**";
int lineCounter = 0;
for (int i = 0; i < text[1].length(); i++) { // 创建星号字符串
asterisks = asterisks + "*";
}
framedString[lineCounter] = asterisks; // 第一个值变成星号字符串
for (int j = 0; j < text.length; j++) { // 在每个“text”字符串前后添加一个星号,并将其存储在“framedString”中
lineCounter = j + 1;
framedString[lineCounter] = "*" + text[j] + "*";
}
framedString[lineCounter + 1] = asterisks; // 最后一个值也变成星号字符串
for (int k = 0; k < framedString.length; k++) { // 打印结果
System.out.println(framedString[k]);
}
}
(以上内容仅为代码翻译,不包含其他内容)
英文:
You can simplify this without using .append
. Use a for
loop to print a row of asterisks, which should be the length of the string plus 2(1 extra for each side). To get the length of the string use .length()
Then print an asterisk before and after the string using
System.out.println("*" + string + "*");
and print the same row of asterisks as in the first part.
The same goes for printing an array of strings, just use a for loop to print each value with System.out.println("*" + string[i] + "*");
then follow it with the same row of asterisks used for the top border.
Edit: After reading your question again closely I think you wanted the result stored in a new array. I started writing an example code and couldn't help but write the whole thing:
public static void main(String[] args) {
String text [] = {"abc", "def", "ghi", "jkl"};
String framedString [] = {"","","","","","",""}; //java arrays are fixed in size,
//so I had to define 6 blank values
String asterisks = "**";
int lineCounter = 0;
for (int i = 0; i < text[1].length(); i++) { //creates string of asterisks
asterisks = asterisks + "*";
}
framedString[lineCounter] = asterisks; //first value becomes asterisk string
for (int j = 0; j < text.length; j++) { //adds an asterisk before and after each string of "text" and stores it in "framedString"
lineCounter = j+1;
framedString[lineCounter] = "*" + text[j] + "*";
}
framedString[lineCounter+1] = asterisks; // last value becomes asterisk string as well
for (int k = 0; k < framedString.length; k++) { //prints the result
System.out.println(framedString[k]);
}
}
答案2
得分: 1
这个解决方案可以分解为3个简单的步骤,第1步是在数组顶部添加一系列 *,第2步是通过循环遍历数组的元素,在图片数组中的元素开头和结尾处添加 *,最后一部分是在数组底部添加一系列 *,其长度与数组的最后一个元素长度相同。
let picture = ["abc", "ded"];
function addBorder(picture) {
let top = '*'.repeat(picture[0].length);
picture.unshift(top);
for(let i = 0; i < picture.length; i++){
picture[i] = '*' + picture[i] + '*';
}
let bottom = '*'.repeat(picture[1].length);
picture.push(bottom);
return picture;
}
英文:
This solution can be broken down into 3 simple steps, 1st is to add the list of * at the top of the array, 2nd to add the * at the start and end of the element in the picture array by looping via the elements of the array and the last part is to add the list of * to the bottom of the array as the length of the last element of the array.
let picture = ["abc", "ded"];
function addBorder(picture) {
//adding the * on the top of the picture array as equal to the lenght of the top element of the array.
let top = '*'.repeat(picture[0].length);
picture.unshift(top);
//looping through the array and concat the * at the end and the start of the array
for(let i = 0; i < picture.length; i++){
picture[i] = picture[i] + '*';
picture[i] = '*' + picture[i];
}
//adding the * on the bottom of the picture array as equal to the lenght of the last element of the array.
let bottom = '*'.repeat(picture[1].length);
picture.push(bottom);
return picture;
}
答案3
得分: 0
String[] addBorder(String[] picture) {
final int additionalCount = 2;
int elementLength = picture[0].length() + additionalCount; // 第一个元素的长度,用于确定需要多少个 *。
int loopLength = picture.length + additionalCount; // 循环迭代次数,额外增加两次循环用于在数组开头和结尾添加 ***。
String[] output = new String[loopLength];
String border = "";
for (int i = 1; i <= elementLength; i++) {
border += "*";
}
for (int i = 0; i < loopLength; i++) {
if (i == 0) {
output[i] = border;
} else if (i == loopLength - 1) {
output[i] = border;
} else {
String s = "*" + picture[i - 1] + "*";
output[i] = s;
}
}
return output;
}
英文:
String[] addBorder(String[] picture) {
final int additionalCount = 2;
int elementLength = picture[0].length() + additionalCount; // first element length which used to determine howmany * required.
int loopLength = picture.length + additionalCount; // extra two iteration for loop to add *** at the begining and end of array.
String [] output = new String[loopLength];
String border = "";
for( int i = 1; i <= elementLength; i++){
border += "*";
}
for (int i = 0; i < loopLength; i++){
if(i == 0){
output[i] = border;
} else if(i == loopLength-1){
output[i] = border;
} else {
String s = "*"+picture[i-1]+"*";
output[i] = s;
}
}
return output;
}
答案4
得分: 0
function addBorder(picture) {
let maxlength = 0;
for (let i = 0; i < picture.length; i++) {
if (picture[i].length > maxlength) {
maxlength = picture[i].length;
}
}
let border = "*".repeat(maxlength + 2);
let resultArr = [border, border];
for (let i = 0; i < picture.length; i++) {
resultArr.splice(i + 1, 0, "*" + picture[i] + "*");
}
return resultArr;
}
英文:
function addBorder(picture) {
let maxlength = 0
for (let i=0;i<picture.length;i++){
if(picture[i].length>maxlength){
maxlength= picture[i].length
}
}
let border = "*".repeat(maxlength+2);
let resultArr =[border,border]
for (let i=0;i<picture.length;i++){
resultArr.splice(i+1,0,"\*"+picture[i]+"\*")
} return resultArr
}
答案5
得分: 0
function solution(picture) {
const verticalBorder = '*'.repeat(picture[0].length);
picture.unshift(verticalBorder);
picture.push(verticalBorder);
return picture.map((el) => '*' + el + '*');
}
英文:
function solution(picture) {
const verticalBorder = '*'.repeat(picture[0].length);
picture.unshift(verticalBorder);
picture.push(verticalBorder);
return picture.map( (el) => '*' + el + '*' );
}
答案6
得分: 0
function addBorder(picture) {
const wall = "*".repeat(picture[0].length + 2);
picture.unshift(wall);
picture.push(wall);
for (let i = 1; i < picture.length - 1; i++) {
picture[i] = `*${picture[i]}*`;
}
return picture;
}
console.log(addBorder(["abc", "def", "ghi"]));
英文:
function addBorder(picture) {
const wall = "*".repeat(picture[0].length + 2);
picture.unshift(wall);
picture.push(wall);
for (let i = 1; i < picture.length - 1; i++) {
picture[i] = `*${picture[i]}*`;
}
return picture;
}
console.log(addBorder(["abc", "def", "ghi"]));
答案7
得分: 0
function solution(picture) {
let len = picture[0].length;
let toReturnArray = [];
toReturnArray[0] = '*'.repeat(len + 2);
for (let i = 0; i < picture.length; i++) {
toReturnArray.push(`*${picture[i]}*`);
}
toReturnArray.push(toReturnArray[0]);
return toReturnArray;
}
英文:
function solution(picture) {
let len=picture[0].length;
let toReturnArray=[];
toReturnArray[0]='*'.repeat(len+2);
for(let i=0;i<picture.length;i++){
toReturnArray.push(`${'*'+picture[i]+'*'}`);
}
toReturnArray.push(toReturnArray[0]);
return toReturnArray;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论