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();
}
}
Related
b) drawChristmasTree(int n) which takes as an input one integer value n
and then output on console a Christmas tree in which last part height equals n. The tree consists of pyramids of heights from 1 to n. The shape have to be as presented below (for
n=4):
X
X
XXX
X
XXX
XXXXX
X
XXX
XXXXX
XXXXXXX
I tried to do it but the output was:
X
X
XXX
X
XXX
XXXXX
X
XXX
XXXXX
XXXXXXX
My coding:
public static void drawChristmassTree(int n) {
n=1;
while(n<=4){
int rowCount =1;
for(int i = n ; i>0 ; i--) {
for(int j=1; j<=i; j++) {
System.out.print(" ");
}
for(int j=1; j<=rowCount; j++ ) {
System.out.print("X");
}
for(int j=2; j<=rowCount; j++ ) {
System.out.print("X");
}
System.out.println();
rowCount++;
}
n++;
}
}
Does anyone have any idea how to move it to the right?
My approach
In order to solve this problem, I started out writing out each case (e.g. n=1, n=2, n=3...) and ended up with something like this:
if (n < 1) {
return;
}
if (n == 1) {
System.out.println(" X ");
}
else if (n == 2) {
System.out.println(" X "); // n-1 spaces
System.out.println(" X ");
System.out.println("XXX"); // 1, 3, 5, 7, ... n+2 pattern
}
else if (n == 3) {
System.out.println(" X "); // n-1 spaces
System.out.println(" X ");
System.out.println(" XXX "); // 3
System.out.println(" X ");
System.out.println(" XXX ");
System.out.println("XXXXX"); // 5
}
Once I had a handle of the pattern, I was able to build the first function to handle drawing a single tier on the tree:
public static void drawTier(int currentTier, int numberOfTiers) {
if (currentTier < 1) {
return;
}
for (int i = 0; i < currentTier; i++) {
for (int j = i; j < numberOfTiers - 1; j++) {
System.out.print(" ");
}
int width = i + (i % currentTier);
for (int k = 0; k <= width; k++) {
System.out.print("X");
}
System.out.println();
}
}
Now we can build a wrapper to build n number of tiers:
public static void drawTree(int numberOfTiers) {
if (numberOfTiers < 1) {
return;
}
for (int i = 1; i <= numberOfTiers; i++) {
drawTier(i, numberOfTiers);
}
}
and finally, we can give it a try:
public static void main(String[] args) {
drawTree(4);
}
Final output:
X
X
XXX
X
XXX
XXXXX
X
XXX
XXXXX
XXXXXXX
I am having difficulty getting the desired output. I know there are problems that are similar to mine that is already posted, but I find it hard to relate my code to their solutions without a massive overhaul.
The solution for my class assignment:
Supposed to continue until every direction of pyramid equals 1
My second method "spaces" is redundant and I am not sure why. Any help would be appreciated.
Blockq
public static void main(String[] args) {
numPar();
spaces();
}
private static void spaces() {
int x = 0;
if(x > 0 && x < 10) {
System.out.print(" ");
} else if (x > 10 && x < 99) {
System.out.print(" ");
} else if (x > 99) {
System.out.print(" ");
}
}
private static void numPar() {
int spaces = 14;
for(int i = 0; i<= 7; i++) {
for(int u = 0; u<spaces; u++) {
System.out.print(" ");
}
spaces--;
spaces--;
for(int j = 0 ; j <i ; j++) {
System.out.print(""+ (int) Math.pow(2,j)+" ");
}
for(int k = i ; k >=0 ; k--) {
System.out.print(""+ (int) Math.pow(2,k)+" ");
}
System.out.println("");
}
}
}
I made every number take 3 places using String.format("%3s", (int) Math.pow(2, j)). You can make it dynamic by replacing the number 3 here with the length of the largest number you'll print. I also changed the number of spaces in your print statements. Here is the full code that prints an evenly spaced pyramid:-
public static void main(String[] args) {
numPar();
spaces();
}
private static void spaces() {
int x = 0;
if (x > 0 && x < 10) {
System.out.print(" ");
} else if (x > 10 && x < 99) {
System.out.print(" ");
} else if (x > 99) {
System.out.print(" ");
}
}
private static void numPar() {
int spaces = 14;
for (int i = 0; i <= 7; i++) {
for (int u = 0; u < spaces; u++) {
System.out.print(" ");
}
spaces--;
spaces--;
for (int j = 0; j < i; j++) {
System.out.print("" + String.format("%3s", (int) Math.pow(2, j)) + " ");
}
for (int k = i; k >= 0; k--) {
System.out.print("" + String.format("%3s", (int) Math.pow(2, k)) + " ");
}
System.out.println("");
}
}
String.format explanation:-
String.format("%3s", str) will print the string str, padding it with spaces, to make the total length 3 if it's less than 3. Note that you can write anything instead of 3 - I used 3 because your biggest number was of length 3.
So "A" will be printed as "_ _ A" (2 spaces), and "Ab" will be printed as "_ Ab" (1 space).
I just replaced str with your Math.pow(2, j).
I'm starting on a program to make a 3D tic tac toe game, and am running into a few problems. The game is supposed to be 4x4x4. I'm not sure why the multiple dimensions are not being printed, and I'm also not sure why my entered value isn't appearing on the one level that shows. Code below. Any help would be awesome. Thanks!
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int draw = 64;
int n = 0;
int board[][][] = new int[4][4][4];
while (n < draw) {
System.out.println("Type your move as one three digit number(lrc)");
int input = scan.nextInt();
int level = input / 100;
int row = input % 100 / 10;
int column = input % 10;
System.out.println(level);
System.out.println(row);
System.out.println(column);
board[level][row][column] = 1;
for (int i = 3; i >= 0; i--) { //level
for (int h = 3; h >= 0; h--) { //row
for (int temp = h; temp >= 0; temp--) {
System.out.print(" ");
}
System.out.print(i + "" + h + " ");
for (int j = 0; j <= 3; j++) { //column
if (board[i][h][j] == 0) {
System.out.print("_ ");
}
if (board[i][h][j] == 1) {
System.out.print("X ");
n++;
}
if (board[i][h][j] == 5) {
System.out.print("O ");
n++;
}
}
System.out.println();
}
System.out.println();
}
System.out.println("\n 0 1 2 3");
}
}
}
In this line
for (int temp = h; h > 0; h--) {
you are decrementing h rather than temp so in the loops below the value of h will always be 1
After this change
change for (int temp = h; temp >= 0; temp--) {
System.out.println(" ");
}
to simply System.out.println(" ");
I should Write a program Square.java that declares and initalizes a variable n (with n ≥ 2) and prints out a two dimensional n-by-n triangular pattern as shown below (where n=6).
public class Square {
public static void main (String[]args){
for (int Line=1; Line<= 6; Line ++){
for (int n=1; n<= (Line-1*1); n++) {
System.out.print ("# ");
}
for (int n=1; n<= (7-Line); n++){
System.out.print ("$ ");
}
System.out.println ("");
}
}
}
I can't manage to write the code with n>=6 or n=2
I am guessing you are forgetting to update the 7 when you change the 6. Put your size in a variable as shown below. Seems to work for me for 2 and any other size
public static void main(String[] args) {
int size = 6;
for (int Line = 1; Line <= size; Line++) {
for (int n = 1; n <= (Line - 1 * 1); n++) {
System.out.print("# ");
}
for (int n = 1; n <= ((size + 1) - Line); n++) {
System.out.print("$ ");
}
System.out.println("");
}
}
The size of the square must be an attribut of the class
The result :
public class Square {
public int size;
public Square(int size) {
this.size = size;
}
public void goSquare(){
for (int line=1; line<= size; line++){
for (int n=1; n<= (line-1); n++) {
System.out.print ("# ");
}
for (int n=1; n<= ((size+1)-line); n++){
System.out.print ("$ ");
}
System.out.println ("");
}
}
public static void main (String[]args){
Square square = new Square(6);
square.goSquare();
}
}
In your actual code 7-Line is what blocks values >=6 and for value =2 your code works great but it only prints one element(point).
Another thing if you use Line-1*1 you mean (Line-1)*1 which is equivalent to write Line-1.
Also in 7-Line the value 7 should be an attribute to be used in the first loop too in Line <= 7, you can see it in this working code:
int max = 2;
for (int Line = 1; Line <= max; Line++) {
for (int n = 1; n <= (Line - 1); n++) {
System.out.print("# ");
}
for (int n = 1; n <= (max - Line); n++) {
System.out.print("$ ");
}
System.out.println("");
}
And this is an Ideone Working Example of the code.
How to print a star triangle pattern using java. Pattern is something like this
* ====> row 1
* *
* *
* *
* * * * *
it can be n number of rows and from second row onwards there is odd number of white spaces between 2 stars like, 1,3,5, and last row have all the stars each separated by one white space.
Below is the Code I was working on to print the triangle?
public class Triangle
{
public static void main(String[] args)
{
int row = 4;
int space =0;
System.out.println("*");
for (int i=1;i<row;i++)
{
System.out.print("*");
for(space=0;space<i;space = space+i)
{
System.out.print(" ");
}
System.out.print("*");
System.out.println();
}
enter code here
for(int i=0;i<=4;i++)
{
System.out.print("* ");
}
}
}
How do I proceed?
public class Triangle {
public static void DrawWithStars(int dimension)
{
if(dimension < 0)
{
//Assuming that a triangle with dimension = 0 is a dot....
System.out.println("No valid dimension");
}
else
{
//To print the first dimension - 1 rows
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension - i; j++) {
System.out.print(" ");
}
//Print the dot of the row 1 at the end
if(i != 0)
System.out.print("*");
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print(" ");
}
System.out.println("*");
}
//To print the last row
for (int i = 0; i < dimension; i++)
{
System.out.print("* ");
}
System.out.println("*");
}
}
}
package apple;
public class Triangle
{
public static void main(String...strings){
int midspace = -1;
int row = 4;
String star = "";
for(int y=row-1; y>0; y--){
for(int space = 1;space <= y ; space++){
System.out.print(" ");
}
System.out.print("*");
for(int i = midspace; i>0; i--)
System.out.print(" ");
midspace += 2;
star = (y!=row-1) ? "*":"";
System.out.println(star);
}
for(int y=((row*2)-1); y>0; y--){
System.out.print("*");
}
}
}