Using nested loops to create a box with stars in java - java

I am new to java and taking an intro course.
I have been able to figure out the majority of my question however i am stuck on the last step.
The end result is supposed to be this using four or less system.out.print or system.out.println statements:
*******
* *****
* ****
* ***
* **
* *
*******
and I have created this
*******
* *****
* ****
* ***
* **
* *
*
This is my code. Is there anything you guys can see that could help?
public class StarPatterns {
public static void main(String[] args) {
int col;
int row;
for (row = 6; row >= 0 ; row--) {
if (row >= 0)
System.out.print("*");
for (col = row; col < 6; col++) {
System.out.print(" ");
}
for (col = row; col > 0; col--) {
System.out.print("*");
}
System.out.println();
}
}
}

public class stars {
public static void main(String[] args) {
int row = 7;
int col = 7;
int count;
for (int i=0;i<row;i++)
{
count = i;
System.out.print("*");
for (int c=1;c<col;c++)
{
if (count == 0 || count == row-1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
count--;
}
}
System.out.println("");
}
}
4 System.out prints:
Update:
3 System.out prints:
public static void main(String[] args) {
int row = 7;
int col = 7;
int count;
for (int i=0;i<row;i++)
{
count = i;
//System.out.print("*");
for (int c=0;c<col;c++)
{
if (count == 0 || count == row-1 || c == 0)
{
System.out.print("*");
}
else
{
System.out.print(" ");
count--;
}
}
System.out.println("");
}
}
Probably not optimal (memory usage) but maybe good for logic:
Using 2 System.out prints:
public static void main(String[] args) {
int row = 7;
int col = 7;
int count;
String strNewLine = "*" + System.lineSeparator();
String str = "*";
String strToPrint;
for (int i=0;i<row;i++)
{
count = i;
for (int c=0;c<col;c++)
{
if (count == 0 || count == row-1 || c == 0)
{
if (c == row-1)
{
strToPrint = strNewLine;
}
else
{
strToPrint = str;
}
System.out.print(strToPrint);
}
else
{
System.out.print(" ");
count--;
}
}
}
}

EDIT: was missing by one : fixed
What do you think of that :
public static void main(String[] args) {
for (int row=0; row<7; row++) {
for (int col=0; col<7; col++) {
System.out.print((col == 0) || (row == 6) || (col > row) ? "*" : " ");
}
System.out.println();
}
}
Result is :
*******
* *****
* ****
* ***
* **
* *
*******

You can do it in 1 without ternary operators since I assume you haven't learned it yet (or at least your instructor wouldn't like you using them):
public class StarPatterns {
public static void main(String[] args) {
int col;
int row;
for (row = 0; row < 7; row++) {
for (col = 0; col < 7; col++) {
String symbol = " ";
if (row == 0 || col == 0 || col > row || row == 6) {
symbol = "*";
}
if (col == 6) {
symbol += System.getProperty("line.separator");
}
System.out.print(symbol);
}
}
}
}
Output:
*******
* *****
* ****
* ***
* **
* *
*******

Related

How to make a rectangle of characters?

How to make a rectangle of characters?
I need to make such a picture appear in the console
**********
* *
* *
* *
**********
But I get this:
*********
*********
*********
*********
*********
Sides a and b are entered from the console.
int a = requestNumber();
int b = requestNumber();
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (j == 0 || j < (b - 1))
System.out.print("*");
else {
System.out.print(" ");
}
}
System.out.println();
}
}
A pretty concise way to do it just a few lines:
int a = requestNumber();
int b = requestNumber();
System.out.println(" " + new String(new char[a]).replace("\0", "*"));
for (int i = 0; i < b; i++) {
System.out.println("*" + new String(new char[a]).replace("\0", " ") + "*");
}
System.out.println(" " + new String(new char[a]).replace("\0", "*"));
EDIT: Fixed typo, works now!
public class Rectangle {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.printRectangle(7,9);
}
private void printRectangle(int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if ((isFirstOrLastRow(i, row) && isFirstOrLastCol(j, col))
|| !(isFirstOrLastRow(i, row) || isFirstOrLastCol(j, col))) {
System.out.print(" ");
}
else {
System.out.print("*");
}
}
System.out.println();
}
}
private boolean isFirstOrLastRow(int currentRow, int row) {
return currentRow == 0 || currentRow == row - 1;
}
private boolean isFirstOrLastCol(int currentCol, int col) {
return currentCol == 0 || currentCol == col - 1;
}
}
The four corners and the middle position should output spaces, so we should judge whether it is the four corners or the middle position,I use isFirstOrLastRow and isFirstOrLastCol to help judge.
here is the result for input 7,9
*******
* *
* *
* *
* *
* *
*******

Print an hourglass pattern

I want to print the pattern in java and I have tried the following code. please guide me to solve my mistake.
import java.util.*;
class sp10
{
public static void main(String args[])
{
int i,j,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
n = sc.nextInt();
for(i=n;i>=1;i--)
{
for(j=i;j<n;j++)
{
System.out.print(" ");
}
for(j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1) ||i==n)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for(i=2;i<=n;i++)
{
for(j=i;j<n;j++)
{
System.out.print(" ");
}
for(j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1)|| i==n)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
I am trying it in Java. Can you please guide me? It should print 5 stars in the first and the last line. Given below is the exact pattern
*****
* *
* *
*
* *
* *
*****
Well, let's look at the pattern.
n = 5
*****
* *
* *
*
* *
* *
*****
The first line and the last line is just print * n times.
private static void printTop(int n) {
System.out.println("*".repeat(n));
}
private static void printBottom(int n) {
printTop(n);
}
Though I wrote two methods here, you can just define one method like printStar(int n) and reuse it on the first and last line.
The real problem is to implement the middle part. Well, this still can be implemented in a relatively simple way. You just need to move * from column 0 to right and from n-1 to left as the row increased.
private static void printMiddle(int n) {
for (int i = 0; i < n; i++) {
int p = i;
for (int j = 0; j < n; j++) {
if (j == p || j == n - 1 - p) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
p is the offset from column 0 to right or column n-1 to left.
Now, you have everything. You can write the main method like the following:
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
printTop(n);
printMiddle(n);
printBottom(n);
}
I suggest the following solution:
import java.util.*;
public class sp10 {
public static void main(String args[]) {
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
n = sc.nextInt();
System.out.print(" ");
for (i = 0; i < 2*n - 3; i++) {
System.out.print("*");
}
System.out.println("");
for (i = n-1; i >= 1; i--) {
for (j = i; j <n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if (j==1 || j == (2 * i - 1) || i == n ) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
for (i = 2; i <= n-1; i++) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1) || i == n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
System.out.print(" ");
for (i = 0; i < 2*n - 3; i++) {
System.out.print("*");
}
}
}
I hope is what you're looking for, because you question was a little bit difficult to understand...
With such solution the output is (for n = 4):
*****
* *
* *
*
* *
* *
*****
Probably, what you intended to do is as follows:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of n: ");
n = sc.nextInt();
for (i = n; i >= 1; i--) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1) || (i == n && j % 2 == 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for (i = 2; i <= n; i++) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1) || (i == n && j % 2 == 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
A sample run:
Enter value of n: 4
* * * *
* *
* *
*
* *
* *
* * * *
However, if you insist that you need to print exactly what you have shown in the question, you can do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of n: ");
n = sc.nextInt();
for (i = n; i >= 1; i--) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if ((j == 1 && i != n) || (j == (2 * i - 1) && i != n) || (j != 1 && i == n && j != (2 * i - 1)))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for (i = 2; i <= n; i++) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if ((j == 1 && i != n) || (j == (2 * i - 1) && i != n) || (j != 1 && i == n && j != (2 * i - 1)))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
Output:
Enter value of n: 4
*****
* *
* *
*
* *
* *
*****

java checkerboard pattern with asterisks

I've looked over lots of posts already and they have helped a lot, but none have covered my issue. I'm trying to print out an alternating checkerboard pattern for a class assignment. My output starting on the first line and every odd line has an extra print at the end. It should be repeating a 8x8 pattern basically. Here is my code and a screenshot of my output.
I need to know how to alter the code so that I only get 8 asterisks in the odd lines instead of the 9 that are showing now.
public class Checkerboard {
public static void main(String[] args) {
int length = 16;
int height = 8;
for (int i = 0; i <= height; i++)
{
if (i % 2 == 0)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
for (int j = 0; j <= length; j++)
{
if (j % 2 == 0)
{
System.out.print(" ");
}
else
{
System.out.print("* ");
}
}
System.out.println("");
}
}
}
output
This works for me. I changed the last else statement to an else if with the condition j != length || i % 2 != 0 so now if it is an odd number row it will not print out an extra '*' at the end.
public class mainTest {
public static void main(String[] args) {
int length = 15;
int height = 8;
for (int i = 0; i <= height; i++)
{
if (i % 2 == 0)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
for (int j = 0; j <= length; j++)
{
if (j % 2 == 0)
{
System.out.print(" ");
}
else if (j != length || i % 2 != 0)
{
System.out.print("* ");
}
}
System.out.println("");
}
}
}
Although below snippet is not optimized, but it should work for you. There is scope of simplification. Try that.
public static void main(String[] args) {
int length = 16;
int height = 8;
for (int i = 0; i <= height; i++) {
char first = ' ';
char second= '*';
if (i % 2 == 0) {
first = '*';
second = ' ';
}
for (int j = 0; j < length; j++) {
if (j % 2 == 0) {
System.out.print(first);
} else {
System.out.print(second);
}
}
System.out.println("");
}
}

Print a Z shape pyramid using * stars

I am trying to write a program that outputs a Z pattern that is n number of * across the top, bottom, and connecting line using for loops.
Example:
Enter a number: 6
******
*
*
*
*
******
This is my current code, it's producing a half pyramid upside down.
import java.util.*;
public class ZShape {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int x = 0; x <= n; x++) {
for (int y = n; y >= 1; y--) {
if (y > x) {
System.out.print("* ");
}
else
System.out.print(" ");
}
System.out.println();
}
}
}
This is the logic in the following code:
Loop over each row of the output (so from 0 to n excluded so that we have n rows)
Loop over each column of the output (so from 0 to n excluded so that we have n columns)
We need to print a * only when it is the first row (x == 0) or the last row (x == n - 1) or the column is in the opposite diagonal (column == n - 1 - row)
Code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
if (row == 0 || row == n - 1 || column == n - 1 - row) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Sample output for n = 6:
******
*
*
*
*
******
(Note that this output has trailing white-spaces for each row, you did not specify whether they should be included, but it is easy to remove them by adding another check).
How about using three loops instead?
for (int x = 0; x < n; x++) {
System.out.print("*");
}
System.out.println();
for (int x = n-3; x >= 0; x--) {
for (int y = x; y >= 0; y--) {
System.out.print(" ");
}
System.out.println("*");
}
for (int x = 0; x < n; x++) {
System.out.print("*");
}
public class Star {
public static void main(String[] args) {
for (int i = 0; i <=4; i++) {
for (int j = 0; j <=4; j++)
{
if (i==4 || (i+j)==4 || i==0)
{
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
Here is a logic to print Z shape:
when i = 1, then the First line will print only "#"
when i = n, then the Last line will print in same way by "#" only
when i = j, that means it will execute one diagonal line by "#" only
Code:
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
int n = scr.nextInt();
for (int i = 1; i <= n; i++) {
for(int j = n; j >= 1; j--) {
if (i == 1 || i == n || i == j) {
System.out.print("# ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}

create border only triangle using Java loops

Hi there i want to create a triangle using java loops. But what i need is only the borders. I've tried using this syntax but it always shows error. any advice?
int lines = 5;
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
for (int j = i; j < lines; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
{
System.out.print(" ");
}
else
{
if (k == 0 || k == lines - 1) {
System.out.print("*");
}
}
}
System.out.print("\n");
c -= 2;
}
can you kindly help me . thanks
There's a lot of things wrong with your code...
for (int k = 1; k <= c; k++)
// and then:
if (k == 0 || k == lines - 1) {
k will never be 0
if (k % 2 == 0)
// and then
else
{
if (k == 0 || k == lines - 1) {
a even if k was 0, then 0%2 == 0, so k==0 can never occur
As of right now, your app only prints spaces.
If you want to draw a triangle, the (possibly) simplest way would be to do
public static void main(String[] args) {
int lines = 6;
int cols = lines+1;
for (int line =0; line < lines-1; line++) {
for (int col = 0; col<cols; col++) {
if (col == 0 || col == line) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int col=0; col<cols; col++) {
System.out.print("*");
}
System.out.println();
}
Triangle.java
public class Triangle {
private int lines;
public Triangle(int lines) {
this.lines = lines;
display();
}
public void display() {
int spaces = lines - 1;
int last;
int min = 1;
int max;
for (int i = 0; i < lines; i++) {
last = 2 * i + 1;
max = spaces + last;
for (int j = min; j <= max; j++) {
if (j == spaces + 1 || j == max || i == lines - 1)
System.out.print('*');
else
System.out.print(' ');
}
System.out.println();
spaces--;
}
}
public static void main(String[] args) {
new Triangle(3);
new Triangle(4);
new Triangle(5);
new Triangle(6);
}
}
Output
*
* *
*****
*
* *
* *
*******
*
* *
* *
* *
*********
*
* *
* *
* *
* *
***********
Ex:
*
* *
* *
* *
*********
Code:
int numberOfRows = 5;
for(int i=0; i<numberOfRows; i++)
{
for(int a=0; a<=numberOfRows; a++)
{
int x = numberOfRows - i;
if (a > x) {
if (x==(a-1) || i==(numberOfRows-1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
} else {
System.out.print(" ");
}
}
for(int b=0; b<=i; b++)
{
if(b==i || i==(numberOfRows-1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
I have used two methods and repeated those two methods using for loop.
public class OpenWord {
int i;
String k = " ";
public void space(){
System.out.println("*"+k+"*");
}
public void star(){
System.out.println("*");
}
public void trainagle(){
star();
for ( i=1; i<=10;i++){
space();
k =k+" ";
if(i==10){
k=k.replace(" ", "*");
space();
}
}
}
public static void main(String args[]) {
OpenWord a = new OpenWord();
a.trainagle();
}
}
Output:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
******************

Categories

Resources