I was asked the following question on a job interview. I was asked to do a form of 'padded printing' using an * character. Here is the code I provided as my answer (in java):
edit:
something like this: User input 3:
x x x x x
x * * * x
x * * * x
x * * * x
x x x x x>
public class asterisk {
public static void main (String args[]){
int input,ast;
Scanner scan = new Scanner(System.in);
System.out.println("Enter number: ");
input = scan.nextInt();
if(input>0) {
topBottom(input);
for(int x=1; x<=input; x++){
System.out.print("x ");
for( ast=1; ast<=input; ast++) {
System.out.print("* ");
}
System.out.print("x ");
System.out.println();
}
topBottom(input);
} else {
System.out.print("x ");
}
}
public static void topBottom(int input) {
for(int top = 1; top<=input+2; top++ ) {
System.out.print("x ");
}
System.out.println();
}
}
Is there a better more efficient way to do this aside from my way? Also, what did i do poorly in my code?
It would really mean a lot to me. I am now practicing commonly asked interview coding questions.
Your code is good but a few suggestions.
By convention, methods should start with a verb. The use of the topBottom function is questionable. I find it makes the code more confusing than anything. Consider readability as well as efficiency.
An approach like this is easier to read and doesn't include the extra method.
For n+2 chars in n+2 rows
for(int i=0; i<input+2; i++) {
for(int j=0; j<input+2; j++) {
Always print X for the first and last rows
if(i == 0 || i == input+1) {
System.out.print("X ");
}
For all other rows print X for the first and last character, otherwise print *
else {
if(j == 0 || j == input+1) {
System.out.print("X ");
} else {
System.out.print("* ");
}
}
Final Result:
for(int i=0; i<input+2; i++) {
for(int j=0; j<input+2; j++) {
if(i == 0 || i == input+1) {
System.out.print("X ");
} else {
if(j == 0 || j == input+1) {
System.out.print("X ");
} else {
System.out.print("* ");
}
}
}
System.out.println();
}
Minor changes, #Michael code , to print next line and print the char inside inner loop
// y = column
for(int y=0; y < input; y++){
// x = row
for(int x=0; x< input; x++){
nextChar = (x == 0 || y == 0 || (x+1) == input
|| (y+1) == input) ? BORDER : FILLING;
System.out.print(nextChar);
}
System.out.println();
}
Related
I'm very new in Java Programming Language.
I asked to make something like this with nested loops method:
.
"Masukan Angka" is "Input Number" in Indonesian Language. So if we input 9, it will print out 9 lines of * and the amount of * decreased for each line.
I tried it with nested loops, this is what i made :
The code is :
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Input your number: ");
int x = in.nextInt();
for (int y = x; y > 0; y--) {
for (int z = 0; z < y; z++)
System.out.print("*");
System.out.println();
}
}
How can i make it doesn't filled up with * in the line 2-7, but instead filled with blank space like the example in the first picture?
Thanks in advance.
Expanding a bit on #Ringuerel solution:
for (int y = x; y > 0; y--) {
for (int z = 0; z < y; z++) {
// If it's first or last or first row print "*"
if( z == 0 || z == y-1 || y == x) {
System.out.print("*");
}
else {
// Otherwise print " "
System.out.print(" ");
}
}
System.out.println();
}
Add this after the second for, before the print statement, if( z == 0 || z == y-1 ), sorry I'm using my phone
public static void main(String[] args) {
int amount = 10;
String c = "*";
String message = "";
for (int i = 0; i < amount; i++) {
message += "*";
for (int j = 1; j < amount - i; j++) {
message += c;
}
message += "*";
c = " ";
System.out.println(message);
message = "";
}
System.out.println("*");
}
You can try this if you want. Haven't tested it but I'm fairly certain it will work.
I am trying to print a nested loop that will print two islands and scale depending on what the input is. The goal is to make Exclamation points(!) to make the left island, a line diagonally of asterisks(*), question marks to make the right island and tildas(~) to make the ocean. Any comments on my code would be helpful.
Example of what I am trying to do.
Input a size (must be larger than 1):
5
0 !!~~*
1 !!~*~
2 ~~*~~
3 ~*~??
4 *~~??
Here is my code:
import java.util.Scanner;
public class Two_Islands {
public static void main(String[] args) {
Scanner kbinput = new Scanner(System.in);
//Create Size variable
System.out.println("Input a size: ");
int n = 0; n = kbinput.nextInt();
for (int r = 0; r < n; r++) {
System.out.print(r);
for (int c = 0; c < n; c++) {
if (r+c == n-1) {
System.out.print("*");
} else if (r+c == n-2) {
System.out.print("!");
} else if (r+c == n+2) {
System.out.print("?");
} else {
System.out.print("~");
}
}
System.out.println();
}
kbinput.close();
}
}
Here is my current output.
Input a size:
5
0~~~!*
1~~!*~
2~!*~~
3!*~~?
4*~~?~
try the following:
else if(r+1 < n/2 && c+1 < n/2)
{
System.out.print("!");
}
else if(r+1 > n-n/2 && c+1 > n-n/2)
{
System.out.print("?");
}
I'm in a beginners java class and I have a quick question about the output statement on my array problem for week 5. So basically I have the core of the program down, but I'm supposed to output the result in lines of ten. I for some reason can not get it to work even with looking at similar posts on here. I'm a beginner and am pretty slow at putting 2 and 2 together when it comes to programming. Once I see it I have that ah-ha! moment and that's how this whole class has gone. I know I have to use the modulus, but in my trial and error I lost my way and have probably done more damage than good. Help would be appreciated.
Here is what I have and as you can tell I was trying something without modulus:
import java.util.*;
public class ArrayLoop
{
public static void main(String args[])
{
double alpha[] = new double[50];
*//Initialize the first 25 elements of the array (int i=0; i<25; i++)//*
for(int i = 0; i < 25; i++)
{
alpha[i]= i * i;
}
*//Initialize the last 25 elements of the array (i=25; i<50; i++)//*
for(int i = 25; i < 50; i++)
{
alpha[i]= 3 * i;
}
*//Print the element of the array*
System.out.println ( "The values are: " );
for (int i = 0; i < 50; i++)
System.out.println ( alpha[i] );
}
*//Print method to display the element of the array*
void print(double m_array[])
{
for(int i = 1; i < m_array.length; i++)
{
if(i % 10 == 0){;
System.out.println();
}else{
System.out.print(" ");
}
}
if (m_array.length % 10 != 0) {
System.out.println();
}
}
}
Um .. this isn't eloquent in the least but I tried to make the fewest changes to your existing code sample.
public class ArrayLoop {
public static void main(String args[]) {
double alpha[] = new double[50];
for (int i = 0; i < 25; i++) {
alpha[i] = i * i;
}
for (int i = 25; i < 50; i++) {
alpha[i] = 3 * i;
}
System.out.println("The values are: ");
for (int i = 0; i < 50; i++) {
System.out.print(alpha[i] + " ");
}
System.out.println();
System.out.println();
for (int i = 1; i < alpha.length; i++) {
if (i != 1 && i % 10 == 0) {
System.out.print(alpha[i - 1] + " ");
System.out.println();
} else {
System.out.print(alpha[i - 1] + " ");
}
}
System.out.print(alpha[49]);
}
}
Edit: A better condition would be ...
for (int i = 0; i < alpha.length; i++) {
if (i > 0 && i % 10 == 9) {
System.out.print(alpha[i] + " ");
System.out.println();
} else {
System.out.print(alpha[i] + " ");
}
}
You have to print the number first then decide whether to print space or newline by checking the modulus:
int arr[] = new int[50];
// Initialize array here
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i > 0 && (i + 1) % 10 == 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
You have a couple of % 10 snippets in your code so I'm not entirely certain how that's "trying something without modulus" :-)
Having said that, modulus is exactly what you need, as per the following psuedo-code:
count = 0
for each item in list:
if count > 0 and (count % 10) == 0:
print end of line
print item
print end of line
In Java, you would use something like:
public class Test {
static public void main(String args[]) {
for (int i = 0; i < 24; i++) {
if ((i > 0) &&((i % 10) == 0)) {
System.out.println();
}
System.out.print ("" + i * 3 + " ");
}
System.out.println();
}
}
In other words, immediately before you print an item, check to see if it should be on the next line and, if so, output a newline before printing it.
Note that arrays in Java are zero based, so you need to start with an index of zero rather than one in your loops.
Now that's pretty close to what you have so you're on the right track but, for the life of me, I cannot see in your print() method where you actually print the item! That should be number one on your list of things to look into :-)
I urge you to try and work it out from the above text and samples but, if you're still having troubles after more than half an hour or so, the below code shows how I'd do it.
public class Test {
static void print (double m_array[]) {
for (int i = 0; i < m_array.length; i++) {
if ((i > 0) && ((i % 10) == 0))
System.out.println();
System.out.print (m_array[i] + " ");
}
System.out.println();
}
static public void main(String args[]) {
double[] x = new double[15];
for (int i = 0; i < x.length; i++)
x[i] = i * 3;
print (x);
}
}
I'm currently doing a java memory game that makes the user guess duplicate letters by input the coordinate of either a 2x2, 4x4 or 6x6 square, which have 2 sets of repeating letters
ex. for 4x4 it would be
A B C D
E F G H
A B C D
E F G H
but randomized
If they guess correctly the letters would stay revealed, until all are found.
I have successfully randomized the square, and cover it with ( * ), and make them reveal based on the input coordinates
But I don't know how to make that once the 2 revealed letters are the same, the program will keep them revealed through out the game, until all duplicate letters are revealed.
The code right now (You can copy and paste whole thing below, all comments are commented out):
import java.util.Scanner;
import java.io.IOException;
public class a4{
// main method. DO NOT MODIFY
public static void main(String args[]) {
Scanner keyboard = new Scanner( System.in );
System.out.println("Welcome to Memory Game");
int board_side;
//this loop obtains the board size, or more specifically
// the length of the side of the board
do{
System.out.println("\n For 2x2 board game press 2"+
"\n For 4x4 board game press 4"+
"\n For 6x6 board game press 6");
board_side=keyboard.nextInt();
}while(board_side!=2 && board_side!=4 && board_side!=6);
char[][] board = createBoard(board_side);
// a call the the shuffle method
shuffleBoard(board);
// a call to the game playing method
playGame(board);
}
// The following method should shuffle the input 2D array caled board
public static void shuffleBoard(char[][] board)
{
// This creates a 1D array whose size is equal to the size of the board
int N = board.length*board.length;
char[] board1D = new char[N];
// Testing to see the printed square
for ( int i = 0; i < board.length; i++) {
for ( int j = 0; j < board[i].length; j++) {
System.out.print( board[i][j] + " " );
}
System.out.println();
}
System.out.println();
for (int i =0; i < board1D.length; i++) {
System.out.print(board1D[i] + " ");
}
System.out.println();
// Copy the elements of 2D array into that 1D array here
for (int m = 0; m < N; m++){
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board.length; j++, m++){
board1D [m] = board[i][j];
}
}
}
// Shuffle 1D array
// Shuffle the array
for (int i = 0; i < N; i++) {
// Generate an index randomly
int index = (int)(Math.random() * N);
char temp = board1D[i];
board1D[i] = board1D[index];
board1D[index] = temp;
}
//Testing to see the 1D array
System.out.println();
for (int i =0; i < board1D.length; i++) {
System.out.print(board1D[i] + " ");
}
System.out.println();
//Copy shuffled 1D array back into 2D array, i.e., back to the board
for (int m = 0; m < N; m++){
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board.length; j++, m++){
board[i][j] = board1D [m];
}
}
}
//Testing to print the shuffled square
for ( int i = 0; i < board.length; i++) {
for ( int j = 0; j < board[i].length; j++) {
System.out.print( board[i][j] + " " );
}
System.out.println();
}
System.out.println();
System.out.println();
}
// game playing method
public static void playGame(char[][] board)
{
Scanner keyboard = new Scanner( System.in );
// this createst a 2D array indicating what locations are paired, i.e., discovered
// at the begining none are, so default initializaiton to false is ok
boolean[][]discovered=new boolean[board.length][board[0].length];;
for ( int i = 0; i < board.length; i++) {
System.out.print(1 + i + " ");
for ( int j = 0; j < board[i].length; j++) {
System.out.print( "* " );
}
System.out.println();
}
System.out.print(" ");
for ( int x = 0; x < board.length; x++) {
System.out.print(1 + x + " ");
}
System.out.println();
System.out.println();
System.out.println("Enter a pair of undiscovered distinct locations on the board that you want revealed. i.e., a pair of integers in the range [1, 2]");
System.out.println();
int FirstLocationX;
int FirstLocationY;
int SecondLocationX;
int SecondLocationY;
do {
System.out.println("Enter the first location: ");
FirstLocationX=keyboard.nextInt();
FirstLocationY=keyboard.nextInt();
if (FirstLocationX > board.length && FirstLocationY > board.length){
System.out.println("The location is invalid. It is outside of the board. ");
}
} while(FirstLocationX > board.length && FirstLocationY > board.length);
System.out.println();
do {
System.out.println("Enter the second location: ");
SecondLocationX=keyboard.nextInt();
SecondLocationY=keyboard.nextInt();
if (SecondLocationX > board.length && SecondLocationY > board.length){
System.out.println("The location is invalid. It is outside of the board. ");
}
else if (SecondLocationX == FirstLocationX && SecondLocationY == FirstLocationY){
System.out.println("The location is invalid. The second location equal to the first. ");
}
} while(SecondLocationX > board.length && SecondLocationY > board.length && SecondLocationX == FirstLocationX && SecondLocationY == FirstLocationY);
//reveals the letters based on the coordinate user inputed
for ( int i = 0; i < board.length; i++) {
System.out.print(1 + i + " ");
for (int j = 0; j < board[i].length; j++) {
if (FirstLocationX == i+1 && FirstLocationY == j+1){
System.out.print( board[i][j] + " " );
}
else if (SecondLocationX == i+1 && SecondLocationY == j+1){
System.out.print( board[i][j] + " " );
}
/*This part is wrong, reveals the whole square instead of the found duplicates
else if (discovered[0][0] = true){
System.out.print( board[i][j] + " " );
}
else if (discovered[0][2] = true){
System.out.print( board[i][j] + " " );
}
*/
else {
System.out.print( "* " );
}
}
System.out.println();
}
System.out.print(" ");
for ( int x = 0; x < board.length; x++) {
System.out.print(1 + x + " ");
}
System.out.println();
System.out.println();
char[][] FirstInput = new char[FirstLocationX][FirstLocationY];
char[][] SecondInput = new char[SecondLocationX][SecondLocationY];
/*GETTING AN ERROR HERE when I try to trigger a duplicate letter found, since the array is shuffled I don't know how exactly, just testing with the first match right now (A and A), which suppose to be coordinate 0,0 and 0,2 in the non-sorted square matrix
*/
if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){
discovered[0][0] = true;
discovered[0][2] = true;
}
waitForPlayer();
do {
playGame(board);
}while(discovered[0][0] == false && discovered[0][2] == false);
}
// createBoard method. DO NOT MODIFY!
/* this method, createBoard, creates the board filled with letters of alphabet,
where each letter appears exactly 2 times
e.g., for 4 x 4, the returned board would look like:
A B C D
E F G H
A B C D
E F G H */
public static char[][] createBoard(int side)
{
char[][] tmp = new char[side][side];
int letter_count=0;
for (int row = 0; row < tmp.length/2; row++){
for(int col = 0; col < tmp[row].length; col++)
{
tmp[row][col]=(char)('A'+letter_count);
tmp[row+tmp.length/2 ][col]=tmp[row][col];
letter_count++;
}
}
return tmp;
}
// waitForPlayer method. Do not modify!
public static void waitForPlayer()
{
System.out.print("Press enter to continue");
try {
System.in.read();
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
}
The current part that is wrong is:
char[][] FirstInput = new char[FirstLocationX][FirstLocationY];
char[][] SecondInput = new char[SecondLocationX][SecondLocationY];
/*when I try to trigger a duplicate letter found, since the array is shuffled I don't know how exactly, just testing with the first match right now (A and A), which suppose to be coordinate 0,0 and 0,2 in the non-sorted square matrix
*/
if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){
discovered[0][0] = true;
discovered[0][2] = true;
}
waitForPlayer();
do {
playGame(board);
}while(discovered[0][0] == false && discovered[0][2] == false);
}
and
else if (discovered[0][0] = true){
System.out.print( board[i][j] + " " );
}
else if (discovered[0][2] = true){
System.out.print( board[i][j] + " " );
}
I would suggest making each square as an object instead of having a mere char. That way you can control if they are revealed or not when you display then
Edit
Without objects, you can just create another array of bool that will track what is displayed. Whenever a pay is revealed, set the values inside that alternative board to true, then in your display function check if it is true before showing it
I would do the following:
Create an char[][] array in which the amount of * equals the input characters.
e.g.:
ABCD
DACB
'*****'
'****'
Get the coordinates which the user enters and compare them logically, if both characters are the same, then replace the corresponding * with the character.
e.g:
if the user input would be 0,0 for the first char and 1,1 for the second (both A)
then update the array to:
ABCD
DACB
A***
*A**
all the lines will be in one array but in the game you would print only the second two rows. The offset between the first two rows and the second two rows is the same, so it should be no problem to do that.
I am working on printing a quasi-empty square that looks like the example below (10 asterisks across and 10 down for the 2 columns):
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
My code cannot dynamically generate squares as specified by the user's input for the number of rows and columns (it is working for 10 rows and 10 columns, but as soon as I change the number to 20, the number of the asterisks does not change. The following is my code:
String STAR = "*";
String star1 = "**********";
int MAX = 10;
for (int row = 0; row <= MAX; row += 1 ) {
for (int col = 0; col <= MAX ; col += 10) {
if (row == 0 && col == 0)
System.out.println(star1);
if (row >= 1 && row <= 4)
System.out.println(STAR + " " + STAR);
if (row == 10 && col == 10)
System.out.println(star1);
}
}
Any help/advice is welcomed regarding the dynamism of the code.
String star = "*";
String space = " ";
int MAX = xxx;
for (int row = 0; row < MAX; row++) {
for (int col = 0; col < MAX; col++) {
if (row == 0 || row == MAX - 1) {
System.out.println(star);
} else if (col == 0 || col == MAX - 1) {
System.out.println(star);
} else {
System.out.println(space);
}
}
}
Look at your nested loop:
for (int col = 0; col <= MAX ; col += 10) {
So when col is 10, you're really only just iterating once... you might as well not have the nested loop at all.
Additionally, both star1 and the string literal with spaces have a fixed number of characters in them, clearly related to the number of columns.
I'm assuming this is homework, so I won't give any more hints than that to start with, but hopefully that'll get you thinking along the right lines...
You should change the 3 occurrences of 10 in your two for loops by the MAX variable, so when the user define another size, your for loop will take his input instead of the 10 value.
Also take a look at your last if statement there where it says if (row == 10 && col == 10) and think about it for a second. Once you have hit 10 rows and 10 columns, you are just going to print your final horizontal line of star1 regardless of what MAX is set too.
Like mentioned above, the nested for loop is unnecessary and can be inefficient if you plan to create larger rectangles in the future (not saying you're going to have to but try to stay away from nested for loops if you can). Instead, just print star1 before your loop begins and after it exits. The body of the loop should be simple enough. Hope this helps.
class Square
{
public static void main(String[] args)
{
String tenStars="**********";
String oneStar="*";
int count=0;
System.out.println(tenStars);
count++;
while(count<=8)
{
System.out.println(oneStar+" "+oneStar);
count++;
}
System.out.print(tenStars);
}
}
this should work
public static void hallowSquare(int side)
{
int rowPos, size = side;
while (side > 0)
{
rowPos = size;
while (rowPos > 0)
{
if (size == side || side == 1 || rowPos == 1 || rowPos == size)
System.out.print("*");
else
System.out.print(" ");
rowPos--;
}
System.out.println();
side--;
}
}
you can use something like this with one user input ... this is working
public static void drawSquare(int size)
{
for(int i=1; i<size ;i++)
System.out.print("*");
System.out.println("");
for(int i=0; i<50 ;i++)
{
System.out.print("*");
for(int j =0; j<size-3; j++)
System.out.print(" ");
System.out.println("*");
}
for(int i=1; i<size ;i++)
System.out.print("*");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
drawSquare(50);
}
you should just create a class in put this inside your class and run it ... I hope this will help you ....
class Star8
{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i==2||i==3||i==4 )
{
System.out.print("* *");
break;
}
else
{
System.out.print("*");
}
}
System.out.println();
}
}
}
Hope this helps, simplify your thinking mate. Think about the axis x and y and work by that logic. Make a nested loop on ur for loop that passes lines, in each case loop the number of
the size of square and print a space, after the nested loop print the "*".
> for (int b=0;b<ans*2-3;b++)
This nested loop has the max value of b because:
remember that while ur printing, each "*" is distanced from the other by a space, and remember u are only counting space between the first and last column. Meaning all space
between x=0 and x=squaresize, therefore max b should be the space between these 2 coords.
which are: squaresize * 2 /the 2 is for the added spaces/ -3/* -3 because u leave out the first coord(x=0),last coord(x=squaresize), AND 1 space added from the former loop.
import java.util.Scanner;
public class AsteriksSquare {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input= new Scanner(System.in);
int ans;
System.out.print("Enter the size of the side of the square: ");
ans=input.nextInt();
String ast="*";
if (ans>0 && ans<21){
for(int i=0;i<=ans-1;i++){
System.out.print("* ");
}
System.out.println("");
for(int i=1;i<=ans-2;i++){
System.out.print("*");
for (int b=0;b<ans*2-3;b++){
System.out.print(" ");
}
System.out.println("*");
}
for(int i=1;i<=ans;i++){
System.out.print("* ");
}
}
}
}
class square1
{
public static void main(String[] args)
{
String star = "*";
String space = " ";
int MAX = 5;
for (int row = 0; row < MAX; row++)
{
for (int col = 0; col < MAX; col++)
{
if (row == 0 || row == MAX - 1)
{
System.out.print(star);
} else if (col == 0 || col == MAX - 1)
{
System.out.print(star);
} else {
System.out.print(space);
}
}
System.out.println();
}
}
}
This code should do the trick.
package javaPackage;
public class Square {
public static void main(String [] args)
{
for (int i=0;i<=10;i++)
{
for (int j=0;j<=10;j++)
{
if(i==0||i==10){
System.out.print("x");
}
else if(j==0||j==10){
System.out.print("x");
}
else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
If the interpreter sees that you're on the first and last line(i=0 and i=10), it will fill the row with x. Else, it will only print a x at the beginning and the end of the row.
you can use below two methods.
1) One with minimal line of code.
for (int i = 0; i <= 9; i++) {
if (i == 0 || i == 9) {
System.out.println("* * * *");
} else {
System.out.println("* *");
}
}
OR
2) With the help of two for loops
for (int i = 0; i <= 9; i++) {
for (int j = 0; j <= 9; j++) {
if (i == 0 || i == 9) {
System.out.print("*");
} else {
if (j == 0 || j == 9) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
Thanks,
Stuti
Here's another solution, a more versatile one. It lets you create a hollow rectangle of height "h" and width "w"
private static void hallowSquare(int h, int w)
{
for(int i=1; i<=h; i++)
{
for(int j=1; j<=w; j++)
{
if (j==1|| j==w || i==1 || i==h )
System.out.print("X");
else
System.out.print(" ");
}
System.out.println();
}
}
import java.util.Scanner;
class Star
{
public static void main(String...args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the row : ");
int row=sc.nextInt();
System.out.print("Enter the column : ");
int column=sc.nextInt();
for(int i=1;i<=column;i++)
{
System.out.print("*");
}
for(int i=row-2;i>=1;i--)
{
System.out.println();
System.out.print("*");
for(int k=1;k<=column-2;k++)
{
if(i<1)
{
break;
}
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
for(int i=1;i<=column;i++)
{
System.out.print("*");
}
}
}
I am hopeful the code below can help, used very simple coding and have the required result.
a=eval(input('Provide the height of the box: '))
b=eval(input('Provide the width of the box: '))
d=a-2
r=b-2
if a >= 1:
print('*'*b)
if a > 1:
for i in range(d):
print('*',end='')
for i in range(r):
print(' ',end='')
print('*')
print('*'*b,end='')
The result is: