Pyramids with java - java

i need help.
i try to make a pyramids that use two alphabet like this:
O
OO
OXO
OXXO
OXXXO
OXXXXO
OOOOOOO
well, because i still new with java
i'm stuck here
O
OX
OXX
OXXX
OXXXX
OXXXXX
OXXXXXX
Here is my code:
import java.util.Scanner;
class Xx {
public static void main(String[] args) {
int x;
Scanner Sc = new Scanner(System.in);
System.out.print("Line of Pyramid : ");
x = Sc.nextInt();
for (int y = 0; y <= x; y++) {
System.out.print("O");
for (int z = 1; z <= y; z++) {
System.out.print("X");
}
System.out.println();
}
}
}

I would start with a method to repeat a given character n times, something like
static String repeat(char ch, int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(ch);
}
return sb.toString();
}
then you can call it to build your pyramid. Something like
int x = Sc.nextInt();
System.out.println("O");
System.out.println("OO");
for (int y = 1; y <= x; y++) {
System.out.print("O");
char ch = 'X';
if (y == x) {
ch = 'O';
}
System.out.print(repeat(ch, y));
System.out.println("O");
}
Which (when I run it with 5) generates (the requested),
O
OO
OXO
OXXO
OXXXO
OXXXXO
OOOOOOO

int x;
Scanner Sc = new Scanner(System.in);
System.out.print("X : ");
x = Sc.nextInt();
for (int y = 0; y <= x; y++)
{
System.out.print("O");
for (int z = 1; z <= y; z++)
{
System.out.print("X");
if(z == y)
System.out.print("O");
}
System.out.println();
}
for(int i=0; i<= x+2; i++)
{
System.out.print("O");
}
Output:
X : 5
O
OXO
OXXO
OXXXO
OXXXXO
OXXXXXO
OOOOOOOO

import java.util.Scanner;
class Xx {
public static void main(String[] args) {
int x;
Scanner Sc = new Scanner(System.in);
System.out.print("X : ");
x = Sc.nextInt();
for (int y = 0; y <= x; y++) {
System.out.print("O");
for (int z = 1; z <= y; z++) {
if(z==y||y==x)
System.out.print("O");
else
System.out.print("X");
}
System.out.println();
}
}
}
Explanation
you can identify the coloumn edge using z==y condition, and base coloumn should fill with 'O' , for check that y==x condition added in your code , will give your expected output

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;
}
}

I need help repairing my histogram

I am trying to make a array that generates random numbers and saves how many times each number was rolled. Then I am trying to create a histogram of the results but the consoles goes blank. I couldn't think of way to cleverly use a loop so I had to use a very crude solution.
import java.util.Random;
import java.util.Arrays;
public class DiceRolls {
public static void main(String[] args){
int[] diceRolls = new int [9];
Random rand = new Random();
for(int numberOfRolls = 0; numberOfRolls <= 20; numberOfRolls++){
int individualRolls = rand.nextInt(9);
diceRolls[individualRolls] ++;
}
for(int y = 0; y >= diceRolls[0]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[1]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[2]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[3]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[4]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[5]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[6]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[7]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[8]; y++){
System.out.print("*");
}
}
}
public static void main(String[] args){
int[] diceRolls = new int [9];
Random rand = new Random();
for(int numberOfRolls = 0; numberOfRolls <= 20; numberOfRolls++){
int individualRolls = rand.nextInt(9);
diceRolls[individualRolls] ++;
}
for(int j : diceRolls) {
for(int i = 0; i<j; i++) {
System.out.print("*");
}
System.out.println();
}
}
Any Questions?
You inverted you loop conditions. They should be:
y < diceRolls[?]

Simple System.out printing

Ok guys I have this problem how do I code this
but without empty line between
x
xy
xxy
xxyy
xxxyy
xxxyyy
Here is my code so far
public static void main(String[] args) {
System.out.print("x");
for(int i = 0;i<6;i++){
for(int j = 0;j<i;j++){
System.out.print("x");
}
System.out.println();
}
}
The pattern is as follows:
1x, 0y
1x, 1y
2x, 1y
2x, 2y...
So your loop should look something like this:
int xCount = 0;
int yCount = 0;
int total = 3;
do {
if (xCount == yCount) xCount++;
else yCount++;
for (int x = 0; x < xCount; x++) System.out.print("x");
for (int y = 0; y < yCount; y++) System.out.print("y");
System.out.println();
} while (yCount < total);

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

Java string triangle

import java.util.Scanner;
public class DrawTriangle
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a height");
while (!scan.hasNextInt()) // while non-integers are present
{
scan.next();
System.out.println ("Bad input. Enter an integer.");
}
int input = scan.nextInt();
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < input; y++)
{
System.out.print(" ");
for (int z = 1; z < y; z++)
{
System.out.print("x");
}
System.out.println();
}
}
}
}
I have to make a triangle of x's relating to the height specified by the user. Can't get it to work at all, any help would be appreciated.
Thanks!
Sorry should have clarified
I need it to look like this -
x
xxx
xxxxx
You don't need nested loop upto 3 levels. Just 2 levels are needed. One for traversing along columns, and one for traversing along rows.
So, change your loop to: -
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < x; y++)
{
System.out.print("x ");
}
System.out.println();
}
UPDATE : -
For equilateral triangle, you would need to add one more loop to print spaces before x on the starting rows. Here's the code: -
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < input - x; y++) {
System.out.print(" ");
}
for (int y = 0; y < x; y++) {
System.out.print("x ");
}
System.out.println();
}

Categories

Resources