Pine tree in java with for loop - java

I wrote a pinetree drawer in java. First it asks for how tall the tree and after that, asks for how many times draw it under each other and at the end it draws the tree's trunk. If the first input is <= 0 it need to stop the whole program and print a message. If the first input is good, but the second input is also <= 0 then stop the program. What is the order to make it operate? Thanks in advance!
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int height;
int stars;
int level;
System.out.println("Fenyőfarajzoló program.");
System.out.print("Kérem a magasságot: ");
height = sc.nextInt();
System.out.print("Kérem a szintek számát: ");
level = sc.nextInt();
int szelesseg = height - 1;
if (height <= 0) {
System.out.println("A magasság csak pozitív lehet.");
} else if (level <= 0) {
System.out.println("A szintek száma csak pozitív lehet.");
} else {
for (int h = 0; h < level; h++) {
stars = 1;
for (int i = 0; i < height; i++) {
for (int j = szelesseg; j > i; j--) {
System.out.print(" ");
}
for (int k = 0; k < stars; k++) {
System.out.print("*");
}
stars += 2;
System.out.println();
}
}
}
for (int talp = 1; talp <= 3; talp++) {
System.out.println(" ***");
}
}

Hello may be this can help? throw an exception to tell that you have insert negative values
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int height;
int stars;
int level;
System.out.println("Fenyőfarajzoló program.");
System.out.print("Kérem a magasságot: ");
height = sc.nextInt();
System.out.print("Kérem a szintek számát: ");
level = sc.nextInt();
int szelesseg = height - 1;
if (height <= 0) {
System.out.println("A magasság csak pozitív lehet.");
throw new Exception("height is negative");
}
if (level <= 0) {
System.out.println("A szintek száma csak pozitív lehet.");
throw new Exception("level is negative");
}
for (int h = 0; h < level; h++) {
stars = 1;
for (int i = 0; i < height; i++){
for (int j = szelesseg; j>i; j-- )
{
System.out.print(" ");
}
for (int k = 0; k < stars; k++){
System.out.print("*");
}
stars += 2;
System.out.println();
}
}
for (int talp = 1; talp <= 3; talp++) {
System.out.println(" ***");
}
}

Related

Print star pattern based on coordinate

I am new to java, I want to print a reversed star pattern based on the coordinate. After I set the coordinate, it will then print the star pattern
import java.util.Scanner;
public class coorTest {
public static void main(String[] args) {
int max = 5;
for (int y = 0; y < max; y += 2) {
int left_spacing = (int) Math.floor(y * 1.0 / 2.0);
for (int space = 0; space < left_spacing; space++) {
System.out.print(" ");
}
for (int x = 0; x < (max - y); x++) {
System.out.print("x");
}
System.out.println();
}
}
}
Try this. I am using (0,0) as a first coordinate.
public static void printStar(int x, int y) {
int starCount = 5;
int space = 0;
int count = 0;
// y-axis line move
for(int i=0; i<y; i++) {
System.out.println();
}
for (int i = 0; i < starCount; i++) {
// x-axis space move
for(int xAxis=0; xAxis<x; xAxis++) {
System.out.print(" ");
}
for (int j = 0; j < starCount; j++) {
if (count < space) {
System.out.print(" ");
count++;
continue;
} else if (j >= starCount - count) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
space++;
count = 0;
}
}

Write a program that produces images of Christmas trees as output. I don't know what the error is

My code is resulting in an infinite loop when i run it. Don't know whats wrong.
!(https://d2vlcm61l7u1fs.cloudfront.net/media%2F1aa%2F1aa1af9f-41ff-48d8-89e9-7b093a909045%2Fphpbrt5Fy.png)
I have tried the code below. It compiles properly.
public class Project3_1 {
//declare SEGMENTS and HEIGHT
public static final int SEGMENTS = 4;
public static final int HEIGHT = 4;
public static final int TOTAL = (2*(SEGMENTS)) + (2*(HEIGHT)) - 3;
public static void TopTree(int SEGMENTS, int HEIGHT) {
for (int s = 1; s <= SEGMENTS; s++) {
for (int h = 1; h <= HEIGHT; h++) {
int ASTERISKS = ((2*s) + (2*h) - 3);
int SPACES = ((TOTAL - ASTERISKS)/2);
//spaces
for (int b = 1; b <= SPACES; b++) {
System.out.print(" ");
}
//Asterisks
for (int a = 1; a <= ASTERISKS; a++) {
System.out.print("*");
}
System.out.println();
}
}
}
public static void TreeBase() {
int x = (TOTAL - 7)/2;
for (int i = 1; i <= (TOTAL-1)/2; i++){
System.out.print(" ");
}
System.out.println("*");
for (int i = 1; i <= (TOTAL-1)/2; i++){
System.out.print(" ");
}
System.out.println("*");
for (int i = 1; i <= x; x++){
System.out.print(" ");
}
for (int i = 1; i <= 7; i++) {
System.out.print("*");
}
for (int i = 1; i <= (TOTAL - (x + 7)); i++) {
System.out.print(" ");
}
System.out.println("");
}
public static void main (String[] args){
TopTree(SEGMENTS, HEIGHT);
TreeBase();
}
}
Check your third for loop in TreeBase(), you increment the wrong variable. The code you posted runs fine for me, it just doesn't make that bottom line of asterisks due to that loop I mentioned not being correct. Once you fix that, you should be good

3 Dimensional Tic Tac Toe prints only one dimension

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(" ");

Exception array out of bounds

My code needs to multiply each row of an array by each column of the next. The user inputs the height and width and the program will randomly generate the array values. It doesn't give any errors if the arrays are equal for example (3x3)(3x3) or even if you do (3x2)(2x3). if however you enter something like (3x3)(3x2) it gives an out of bounds exception.
public class Multiply {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int width = -1;
int height = 0;
int width2 = 0;
int height2 = 0;
while (width != height2) {
while (height <= 0) {
System.out.print("Enter a height for array1: ");
height = checkInt(scan);
}
while (width <= 0) {
System.out.print("Enter a width for array1: ");
width = checkInt(scan);
}
while (height2 <= 0) {
System.out.print("Enter a height for array2: ");
height2 = checkInt(scan);
}
while (width2 <= 0) {
System.out.print("Enter a width for array2: ");
width2 = checkInt(scan);
}
if (width != height2) {
System.out
.println("Error! Dimensions of matrices not compatible. Try again.");
width = -1;
height = 0;
width2 = 0;
height2 = 0;
}
}
int array1[][] = randomMatrix(height, width);
int array2[][] = randomMatrix(height2, width2);
int product[][] = matrixMultiply(array1, array2, height, width2);
printArray(product);
}// end of main method
public static int[][] randomMatrix(int height, int width) {
int array1[][] = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
array1[i][j] = (int) (-10 + Math.random() * 20);
}
}
return array1;
}// end of randomMatrix method
public static int[][] matrixMultiply(int[][] array1, int[][] array2, int height, int width) {
int[][] product = new int[height][width];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
int prod = 0;
for (int k = 0; k < array2.length; k++) {
prod = prod + array1[i][k] * array2[k][j];
}
product[i][j] = prod;
}
}
return product;
}// end of matrixMultiply method
public static void printArray(int[][] array) {
System.out.println(" ");
System.out.println("Row Major Representation:");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println(" ");
System.out.println("Column Major Representation:");
for (int i = 0; i < array[1].length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[j][i] + " ");
}
System.out.println();
}
}
public static int checkInt(Scanner scan) {
int width = 0;
if (scan.hasNextInt()) {
width = scan.nextInt();
return width;
} else {
scan.next();
return 0;
}
}
}// end of public class Multiply
The value of the variable "j" (Line 82) in its final iteration is causing the Out of Bounds Exception. The error occurs for (3x3)(3x2) because in the final "j" iteration, j equals 2, making the value higher than 1 (the highest index value for j's location). Adding some logic to check for when j > the highest index will prevent the issue.

Java - Printing an unfilled diamond using nested for loops

I am new to learning Java and I am trying to print an unfilled diamond using asterisks. The height of the diamond has to be based on user input. My code currently prints out a filled diamond based on user input but I cannot figure out how to print an unfilled one. Every time I change one of the loops it messes something else up. All help is appreciated!
public static void diamond(){
int i = 0;
int j = 0;
int k = 0;
int height = 0;
Scanner in = new Scanner(System.in);
System.out.println("How tall do you want the diamond to be: ");
height = in.nextInt();
for (k = 1; k <= (height + 1) / 2; k++) {
for (i = 0; i < height - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
System.out.print("* ");
}
System.out.println("");
}
for (k = ((height + 1) / 2); k < height; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < height - k; j++) {
System.out.print(" *");
}
System.out.println("");
}
}
Here is the method modified for printing an unfilled diamond. An if condition has been added to the inner loops.
public static void diamond() {
int i = 0;
int j = 0;
int k = 0;
int height = 0;
Scanner in = new Scanner(System.in);
System.out.println("How tall do you want the diamond to be: ");
height = in.nextInt();
for (k = 1; k <= (height + 1) / 2; k++) {
for (i = 0; i < height - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
if (j == 0 || j == (k - 1)) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
for (k = ((height + 1) / 2); k < height; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < height - k; j++) {
if (j == 0 || j == (height - k - 1)) {
System.out.print(" *");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
}

Categories

Resources