create border only triangle using Java loops - java

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:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
******************

Related

i want to figure out what's wrong in my code

I want to make a java pattern like this using while loops
* * * *
* * *
* *
*
and here is my code
int i = 4;
int j = 0;
while (i >= 0) {
while (j < i) {
System.out.print(" * ");
j++;
}
System.out.print("\n");
i--;
}
but its giving output like this:
* * * *
Does anyone knows what to do....?
TRY CONSIDERING THIS ONE MIGHT HELP YOU !!
Using FOR Loop
public class DownwardTrianglePattern {
public static void main(String[] args) {
int rows = 4;
//outer loop
for (int i = rows-1; i >= 0 ; i--) {
//inner loop
for (int j = 0; j <= i; j++) {
//prints star and space
System.out.print("*" + " ");
}
//throws the cursor in the next line after printing each line
System.out.println();
}
}
}
Using While Loop
public class Itriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter N : ");
int n = sc.nextInt();
System.out.print("Enter Symbol : ");
char c = sc.next().charAt(0);
int i = n, j;
while(i > 0) {
j = 0;
while(j++ < i) {
System.out.print(c);
}
System.out.println();
i--;
}
}
}
After i--;
you can j=0;
The problem in your code is that in first iteration after completing both the while loops the value of variables is i=3 and j=4. So for the next iteration the first while loop will run as the condition is true i.e. 3>=0 but for the next while loop the condition will be false as j i.e. 3 not less than i i.e. 3. So that's why the next loop does not executes and you are getting that output.
The j needs to be initialized inside loop.
I changed the code as below. it's giving correct output
int i = 4;
while (i >= 0) {
int j = 0;
while (j < i) {
System.out.print(" * ");
j++;
}
System.out.print("\n");
i--;
}
please reset j after j loop
int i = 4;
int j = 0;
while (i >= 0) {
while (j < i) {
System.out.print(" * ");
j++;
}
j = 0;
System.out.print("\n");
i--;
}
when you're finished with inner loop you have to reset j value else it's value will remain 4 which is not less then i value. you can reset the value before starting inner while
int i = 4;
int j = 0;
while (i >= 0) {
j = 0;
while (j < i) {
System.out.print(" * ");
j++;
}
System.out.println(""); // println will work same as System.out.print("\n") in this scenario
i--;
}
By the way your problem can be nicely solved using recursion
public static void main(String[] args) {
printLine(4);
}
private static void printLine(int numberOfStars) {
if(numberOfStars == 0) {
return;
}
System.out.println("* ".repeat(numberOfStars));
printLine(numberOfStars - 1);
}
Output
* * * *
* * *
* *
*

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
*****
* *
* *
*
* *
* *
*****

Using nested while loop to print pyramid of stars

I'm trying to print a pyramid of stars using nested while loops. I know I am able to achieve this using for loops but I want to do it with while loop instead. This is my code so far:
public class WhileNest
{
public static void main(String[]args)
{
int rows = 5, i = 1, j = 1;
while(i <= rows)
{
while(j <= i)
{
System.out.print("*");
j++;
}
System.out.print("\n");
i++;
}
}
}
The output has to be like this:
*
**
***
****
*****
But my output is this:
*
*
*
*
*
Any help is appreciated, thanks.
you have to reset j like this:
public class test {
public static void main(String[] args) {
int rows = 5, i = 1, j = 1;
while (i <= rows) {
while (j <= i) {
System.out.print("*");
j++;
}
System.out.print("\n");
i++;
j = 1;
}
}
}
You have forgotten to assign 1 to j at the end of the outer while loop.
public class WhileNest {
public static void main(String[] args) {
int rows = 5, i = 1, j = 1;
while (i <= rows) {
while (j <= i) {
System.out.print("*");
j++;
}
System.out.print("\n");
i++;
j = 1;
}
}
}
int rows = 5, i = 1, j = 1;
while(i <= rows)
{
while(j <= i)
{
System.out.print("*");
j++;
}
j=1;
System.out.println();
i++;
}
You are not reassigning j to start from beginning. Your i and j were always staying same. Re-initialize j to 1 after the inner while loop as shown. It will work
Pyramid using two for loops:
String STAR = "*";
String SPACE = " ";
int SIZE = 10;
for(int i=0;i<SIZE;i++) {
int start = SIZE-i;
int end = (SIZE*2) - SIZE + i;
for(int j = 0; j<SIZE*2; j++) {
if(j>=start && j<=end && j%2 == i%2) {
System.out.print(STAR);
} else {
System.out.print(SPACE);
}
}
System.out.println();
}
output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
public static void main(String[] args) {
for(int i=0;i<10;i++){
for(int k=0;k<i;k++){
System.out.print("*");
}
System.out.println();
}
}
*Instead of initializing "j" at the beginning, include it in the first while loop that'll do the work.( * will be printed at the beginning of each row)
public class WhileNest
{
public static void main(String[]args)
{
int rows = 5, i = 1;
while(i <= rows)
{
int j = 1;
while(j <= i)
{
System.out.print("*");
j++;
}
System.out.print("\n");
i++;
}
}
}

Making an ASCII rhombus with loops

I got a problem to create a rhombus, my code here:
package random;
public class asd {
public static void main(String args[]) {
for (int j = 1; j <= 4; j++) {
for (int kong = 4 - j; kong >= 1; kong--) {
System.out.print(" ");
}
for (int xing = 1; xing <= 2 * j - 1; xing++) {
System.out.print("*");
}
System.out.println();
}
for (int a = 1; a <= 3; a++) {
for (int b = 1; b <= a; b++) {
System.out.print(" ");
}
for (int c = 5; c >= 1; c -= 2) { // <==== here
System.out.print("*");
}
System.out.println();
}
}
}
However, the output is:
*
***
*****
*******
***
***
***
I think the problem is in the code which I highlighted.
java-11
By using String#repeat, introduced as part of Java-11, you can do it with a single loop.
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
By increasing the amount of space by one character, you can also print a variant of the shape:
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "* ".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
You are right in indicating the possible problematic line. Surprised that you did it right in first half:
for (int c = 5; c >= 2 * a - 1; c -= 1) { // <==== here
System.out.print("*");
Using Math.abs will make it a lot easier:
import java.util.Scanner;
public class MakeDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Let's Creat Diamonds");
System.out.println("If number increases Diamonds gets bigger. " +
"Please input number lager than 1 : ");
int user_input = sc.nextInt(); //gets user's input
System.out.println("");
int x = user_input;
int front_space = -5;
for (int i = 0; i < 2 * user_input + 1; i++) {
for (int a = front_space; a < Math.abs(i - user_input); a++) {
System.out.print(" ");
}
if (i < user_input + 1) {
for (int b = 0; b < 2 * i + 1; b++) {
System.out.print("* ");
}
} else if (i > user_input) {
for (int c = 0; c < 2 * x - 1; c++) {
System.out.print("* ");
}
x--;
}
System.out.print('\n');
}
System.out.println("\nRun Again? 1 = Run, 2 = Exit : ");
int restart = sc.nextInt();
System.out.println("");
if (restart == 2) {
System.out.println("Exit the Program.");
System.exit(0);
sc.close();
}
}
}
}
You can simplify your code by using two nested for loops and one if else statement.
int n = 4;
for (int i = -n; i <= n; i++) {
for (int j = -n; j <= n; j++)
if (Math.abs(i) + Math.abs(j) <= n)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
See also: Output an ASCII diamond shape using loops

Categories

Resources