How to draw X using arrays? - java

Does anyone have an idea how to print X from given number?
e.g.:
given number 5.
So, I should print
X000X
0X0X0
00X00
0X0X0
X000X
This is my code, still missing something on it
public static void drawX(int number){
int[][] draw = new int[number][number];
for(int i = 0; i< number; i++){
for(int k = 0; k<=i; k++){
System.out.print(" ");
}
for(int j = number-1; j>i; j--){
if(j == number-1 || j == i+1)
System.out.print("X ");
else
System.out.print(" ");
}
System.out.println();
}
for(int i = 0; i< number; i++){
for(int v = number; v>i; v--){
System.out.print(" ");
}
for(int j = 0; j<i; j++){
System.out.print("X ");
}
System.out.println();
}
}

See comments on your code below, highlighting some issues I've noticed. It probably won't solve everything straight away, but it will give you a push in the right direction I hope.
public static void drawX(int number){
// draw is never used.
int[][] draw = new int[number][number];
for(int i = 0; i< number; i++){
for(int k = 0; k<=i; k++){
// Here we print a " " even for k == i.
// Are you sure you want k <= i ?
// If you change it, dont forget to also change
// stop clause in next loop.
System.out.print(" ");
}
for(int j = number-1; j>i; j--){
if(j == number-1 || j == i+1)
// j == number -1 prints X only in the last column
// Maybe you wanted number - i - 1?
// j == i+1 means you "skip" the ith element.
// Why the extra space after X?
System.out.print("X ");
else
// Why two spaces here?
System.out.print(" ");
}
System.out.println();
}
for(int i = 0; i< number; i++){
for(int v = number; v>i; v--){
System.out.print(" ");
}
for(int j = 0; j<i; j++){
// here you need to do very similar logic to
// what you did in previous loop, when printing first
// 'number' lines.
System.out.print("X ");
}
System.out.println();
}
}

Related

Why is my program printing the next line at a location i didnt ask for?

Basically i have to print 2 traingles, top up upside down, buttom one upside up. They are both the same legnth, my program works fine yet for some reason my second triangle gets slighty tilted to the right.
Can anyone please explain to me how to fix and why this bug happens?
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter number");
int num = s.nextInt();
for (int i = 0; i < num; i++) {
for (int j = num; j > i; j--) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
for (int k = 0; k <= i; k++) {
System.out.print(" ");
}
}
// second part
for (int i = 0; i < num; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
for (int k=num; k>i; k-=2) {
System.out.print(" ");
}
}
}
}
* * *
* *
*
*
* *
* * *
A couple of small tweaks:
in the end of the first outer for loop
in the second outer for-loop at the end
See code comments below
for (int i = 0; i < num; i++) {
for (int j = num; j > i; j--) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
for (int k = 0; k < i; k++) { // stop condition changed
System.out.print(" ");
}
if (i < num -1) { // this was added
System.out.print(" ");
}
}
// second part
for (int i = 0; i < num; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
for (int k=num-1; k>i+1; k-=1) { // stop condition change
System.out.print(" ");
}
}

Drawing a tree in JAVA with asterisks and tabs - can't include floor in the code

My code print a tree with a trunk size of 2 spaces in an array, in the last space it needs also a floor, but i don't know how to implement this, so far my code works like this:
public static void main(String[] args) {
//up part
for(int i=1; i<=5; i++){
for(int k=1; k<=6-i+1; k++){
System.out.print(" ");
}
for(int j=1; j<=2*i-1; j++){
System.out.print("*");
}
System.out.println();
}
// trunk
for(int i=1; i<=2; i++){
for(int k=1; k<=5; k++){
System.out.print(" ");
}
for(int j=1; j<=1; j++){
System.out.print("||");
}
System.out.println();
}
System.out.println("_____||_____");
}
}
//floor
for (int i = 1; i <= 2 * 6; i++) {
System.out.print(i == 6 ? "||" : "_");
}

How do i draw an Arrow in Java?

Here is what I am trying to create:
*
**
***
****
*********
****
***
**
*
Here is what i have created:
*
**
***
****
**********
****
***
**
*
Here is my code:
for(int i = 1; i <= 5; i++){
for(int j = 1; j<=i; j++){
System.out.print("*");
}
System.out.println();
if(i == 4){
for(int f = 0; f < 5; f++){
System.out.print("*");
}
}
}
for(int i = 1; i <= 5; i++){
for(int j = 4; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
I dont know how to indent the tail part, please dont give me the answer, just tell me where the problem is and I will try to do it. Thank you!
Given the shape you have provided, and assuming it's going to be output in a monospace font, we can draw it on a grid.
Splitting it visually into 2 sections, we can see that there are 2 main modes.
The tail (green), and the head (red).
There's also a second mode. where the number of stars in the head increase and decrease.
The size of the tail could potentially vary, as could the size of the head, and it would still be a shape we recognise as an arrow.
When outputting text, the easiest iteration order is generally left to right, up to down, unless dealing with right to left languages, or vertical reading, I'm going to assume western culture for the output, as that is what's been popularised in programming output streams.
So, the question is, how best to build up the strings required for output.
Given the format that the output is going to be on a stream,
width is going to be your outside loop, and tail/length your inside loop.
The code you provided uses 10 for width, by splitting it into 2 groups of 5, with the second one being offset by one.
for(int i = 1; i <= 5; i++){
for(int j = 1; j<=i; j++){
System.out.print("*");
}
System.out.println();
if(i == 4){
for(int f = 0; f < 5; f++){
System.out.print("*");
}
}
}
for(int i = 1; i <= 5; i++){
for(int j = 4; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
so translating your code back to the drawings, you loop over width, and on the last iteration output the line of stars for the tail.
What you need to do, is output a space on all other lines.
There are a variety of ways to do so, you could output the character, or use one of the fixed width formatting functions using printf.
But given your current code, the minimal difference will be to output a space character, when the width iteration for the increasing mode is not 4, and for the loop of width - 1.
Looking at your end result, your tail is being printed after your head. That needs to move earlier.
Your tail is too long, need to offset that by 1.
And you need to insert pink and brown sections by printing spaces, the same amount of times as you output the tail, whenever you do not output the tail.
You should add another inner for-loop that prints " " before the for-loop that prints "*".
You can use printf method to tell Java print in specific format. For example, you need 5 spaces prefixed then print your text:
System.out.printf("%5s","hello");
To get your desired output, try the below code. Note: There may be better solutions.
for (int i = 1; i <= 5; i++) {
if (i != 5) // middle line should not be prefix with spaces
// empty 5 spaces before starting the loop
System.out.printf("%5s", "");
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
}
for (int i = 1; i <= 5; i++) {
// print 5 space before
System.out.printf("%5s", ""); // <== note the printf here with empty string
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
Here's a method you can use to print the arrow:
private static void printArrow (final int min, final int max, final int tip)
{
final int numSpaces = tip - max - 1;
// print the top
for (int i = min; i <= max; i++)
{
for (int j = 0; j < numSpaces; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.println();
}
// print the tip
for (int i = 0; i < tip; i++)
{
System.out.print("*");
}
System.out.println();
// print the bottom
for (int i = max; i >= min; i--)
{
for (int j = 0; j < numSpaces; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.println();
}
}
Call it from main with:
public static void main(String[] args)
{
// this will print the arrow in your question
printArrow(1, 4, 9);
}
Another Example:
printArrow(1, 10, 19);
Outputs:
*
**
***
****
*****
******
*******
********
*********
**********
*******************
**********
*********
********
*******
******
*****
****
***
**
*
Here is my answer. You just need to first insert the space and then print * until you reach the middle line. If you want to make it dynamic, just replace the boundary value for k dynamically the same as the boundary for the f:
public static void main(String[] args) {
int k = 1;
int m = 0;
for (int i = 1; i <= 5; i++) {
m = i - 1;
while (k != 6 && m != 4) {
System.out.print(" ");
k++;
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
k = 1;
}
k = 1;
for (int i = 1; i <= 5; i++) {
while (k != 6) {
System.out.print(" ");
k++;
}
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
k = 1;
}
}
EDIT
I realized the output is one* behind. So just need to change the boundary value of k from 5 to 6 (from the value of boundary for f to the value of boundary for f+1)
The output looks like this:
for (int i = 1; i <= 5; i++) {
if (i!=5) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
}
for (int i = 1; i <= 5; i++) {
System.out.print(" ");
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}

Inverse parallelogram

I'm trying to make the output something like this:
I know the problem is in the third loop, but I dont know what to do to make the spacing work for this.
import java.util.Scanner;
public class Tester {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int x, y;
System.out.print("Enter the number of rows: ");
x = in.nextInt();
System.out.print("Enter the number of stars: ");
y = in.nextInt();
//loop for x lines
for(int i = 0; i < x; i++){
//loop for y stars
for(int j = 0; j < y; j++){
System.out.print("* ");
}
System.out.println();
for(int l = 0; l <= i; l--){
System.out.print(" ");
}
}
}
}
You need to do a couple things.
The first is move your last nested for loop (the one that prints spaces) to the beginning of the first for loop. You will also want to remove the space you have added to the for loop that prints the asterisks.
Then, for the output you have showed us, you need to start you main for loop at the end and go backwards.
Try the following:
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int x = in.nextInt();
System.out.print("Enter the number of stars: ");
int y = in.nextInt();
//loop for x lines
//This starts at x and goes toward 0
for(int i = x; i > 0; i--){
//Insert spaces based on line number
//This is at the beginning now
for (int s = 0; s < i; s++)
System.out.print(" ");
//Print y stars
//Removed the space after the asterisk
for(int j = 0; j < y; j++)
System.out.print("*");
System.out.println();
}
}
Tested here and matches the output in your first image
for (int i = 0; i < x; i++){
for (int j = x-1; j > i; j--) {
System.out.print(" ");
}
for(int j = 0; j < y; j++){
System.out.print("*");
}
System.out.println();
}
You have to reorder your for loops. Please note the change in condition in the second for loop in given ode below:
for(int i = 0; i < x; i++){
for(int l = 0; l <= x-i; ++l){
System.out.print(" ");
}
//loop for y stars
for(int j = 0; j < y; j++){
System.out.print("* ");
}
System.out.println();
}
Another way:
String stars = String.format("%0" + y + "d", 0).replace('0', '*');
for (int i=x; i > 0; i--)
{
System.out.println(String.format("%0$"+i+ "s", ' ')+stars);
}
System.out.println(stars);

Upside down right triangle in Java

I need to do this:
*****
****
***
**
*
and I have this code:
for (int i=0; i<5; i++)
{
for (int j=5; j>i; j--)
{
System.out.print("*");
}
System.out.println("");
which outputs this:
*****
****
***
**
*
I cant figure out how to implement the spaces. Any help appreciated.
You need to use two for-loops: one for the number of spaces and one for the number of *:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = i; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
Java 8 solution:
IntStream.range(0, 5).forEach(i -> {
IntStream.range(0, i).forEach(j -> System.out.print(" "));
IntStream.range(i, 5).forEach(j -> System.out.print("*"));
System.out.println();
});
Here's a solution with one less loop than the other answers, if you really want to wow the person who's grading your assignment:
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
System.out.print((x >= y) ? "*" : " ");
}
System.out.println();
}
Just print k number of spaces before start of every line.
To solve this kind of problems, it will be easy if you break it down and observe the pattern.
***** 0 space
**** 1 space
*** 2 spaces
** 3 spaces
* 4 spaces
After taking note of this pattern, you ask yourself will you be able to print this?
0*****
1****
2***
3**
4*
We see that the number is similar to the start of every line. Hence we could make use of variable i. (your outer loop counter) and we have..
for (int i=0; i<5; i++){
System.out.println(i);
for (int j=5; j>i; j--){
System.out.print("*");
}
System.out.println("");
}
Now, you just have to convert your numbers at the start of every line to the number of spaces and you have..
for (int i=0; i<5; i++){
for(int k=0; k<i; k++) //using i to control number of spaces
System.out.println(" ");
for (int j=5; j>i; j--){
System.out.print("*");
}
System.out.println("");
}
int count = 6;
for(int i=0; i<=count; i++) {
for(int j=0; j<i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= (count - i); j ++) {
System.out.print("*");
}
System.out.println();
}
you can pass your value into count, its works pretty nice.

Categories