I wrote some code that will print a diamond
static void printDiamond(int size) {
for (int i = 0; i < size; i++) {
for (int a = 0; a < (size - (i + 1)); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
for (int i = size-1; i >= 0; i--) {
for (int a = 0; a < (size - (i + 1)); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
}
The issue I'm having with the diamond is that it will print double for whatever I input. So, if the user were to input a 6 for the diamond, it should look like this:
XX
X X
X X
X X
X X
XX
With my code, if the user inputs 5, it prints out the following:
XX
X X
X X
X X
X X
X X
X X
X X
X X
XX
Instead of printing out 5 rows, it printed out 10. If I input 3, it will print out 6 rows instead of 3. It seems that for my diamond, it is outputting the number that it receives from the user, but then prints out that amount times 2. Is there a way I can half the size of what the printDiamond method outputs so it has the correct number of rows?
I was able to correct your code by tweaking the loop boundary conditions. First, you were printing the top portion of your diamond with a height of size and you were also printing the bottom potion with a height of size, for a total height of 2*size.
The other big problem was that you were not handling odd-numbered inputs, as all diamonds were coming out as even number height. I also corrected this problem. Have a look at the code below.
static void printDiamond(int size) {
for (int i = 0; i < (int)Math.ceil(size/2.0); i++) {
for (int a = 0; a < (int)Math.ceil(size/2.0) - (i + 1); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
for (int i = (int)Math.floor(size/2.0)-1; i >= 0; i--) {
for (int a = 0; a < (int)Math.ceil(size/2.0) - (i + 1); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
}
printDiamond(5);
System.out.print("\n");
printDiamond(6);
Output:
XX
X X
X X
X X
XX
XX
X X
X X
X X
X X
XX
Related
I just have one small issue with outputting a hyphen after every character (including the three dots shown in the code below)
Sample input
2 (option #)
disappear (phrase)
Expected output:
d-i-s-a-p-p-e-a-r-.-.-.
d-i-s-a-p-p-e-a-.-.-.
d-i-s-a-p-p-e-.-.-.
d-i-s-a-p-p-.-.-.
d-i-s-a-p-.-.-.
d-i-s-a-.-.-.
d-i-s-.-.-.
d-i-.-.-.
d-.-.-.
.-.-.
.-.
.
It outputs the "-" after every character excluding the last dot
I got the "-" to display after very word character but cant figure out displaying after the dots too, like it works but there has to be one less hypen:
My actual output:
d-i-s-a-p-p-e-a-r-.-.-.-
d-i-s-a-p-p-e-a-.-.-.-
d-i-s-a-p-p-e-.-.-.-
d-i-s-a-p-p-.-.-.-
d-i-s-a-p-.-.-.-
d-i-s-a-.-.-.-
d-i-s-.-.-.-
d-i-.-.-.-
d-.-.-.-
.-.-.-
.-.-
.-
I am partially done, I just need one less hyphen which would automatically also fulfill the requirement of not displaying a hyphen after the very last dot.
Code:
else if (option == 2){
for (int x = 0; x < phrase.length(); x++){
for (int y = 0; y < phrase.length() - x; y++){
char n = phrase.charAt(y);
System.out.print(n+"-");
}
for (int a = 0; a < 3; a++){
System.out.print("."+"-");
}
System.out.println("");
}
for (int j = 0; j < 3; j++){
for (int i = 0; i < 3 - j; i++){
System.out.print("."+"-");
}
System.out.println("");
}
}
One way you could remove the last - is to only print - when it's not the last iteration. You can check that it's not the last iteration by checking loopVariable != loopBound - 1.
So your code would be:
for (int x = 0; x < phrase.length(); x++){
for (int y = 0; y < phrase.length() - x; y++){
char n = phrase.charAt(y);
System.out.print(n+"-");
}
// You could just print the literal "--.-." instead
for (int a = 0; a < 3; a++){
System.out.print(".");
if (a != 2) { // Notice here!
System.out.print("-");
}
}
System.out.println();
}
for (int j = 0; j < 3; j++){
for (int i = 0; i < 3 - j; i++){
System.out.print(".");
if (i != 2 - j) { // and here
System.out.print("-");
}
}
System.out.println();
}
Here's how I would do it:
// pretend that the phrase is 3 characters longer than it actually is...
// because of the three dots at the end
for (int x = 0; x < phrase.length() + 3; x++){
for (int y = 0; y < phrase.length() + 3 - x; y++){
char n;
// Should we print the phrase or the dots?
if (y < phrase.length() - x) {
n = phrase.charAt(y);
} else {
n = '.';
}
System.out.print(n);
if (y != phrase.length() + 2 - x) { // same trick of checking if it is last iteration
System.out.print("-");
}
}
System.out.println();
}
How can i get this code to display the height correctly? So if the width of the diamond asterisk is 3, and the height is 5, then the diamond will look like this.
*
***
***
***
*
It just adds any excess height to the middle. Here is my code so far. It can do the width just fine.
public static void main(String[] args) {
int width, height, heightT;
Scanner scan = new Scanner(System.in);
while(true) {
System.out.println("Enter width of diamond (odd number between 0-19): ");
width = scan.nextInt();
if (width % 2 != 0) {
if (0 <= width && width <= 19) {
break;
}
}
}
while(true) {
System.out.println("Enter height of diamond (greater than or equal to width): ");
height = scan.nextInt();
if(height >= width) {
heightT = height - width;
break;
}
}
for(int i = 1; i < width + 1; i += 2) {
for(int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for(int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for(int i = width - 2; i > 0; i -= 2) {
for(int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for(int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
When solving this type of logic you have to always look at the things that you know never change. In this case, 3 things never change:
1. The first line. Always equal to one asterisk.
2. The middle line. Always equal to width in asterisks.
3. The last line. Always equal to one asterisk.
Now, split the diamond in two by the middle row. Notice the total height will always be the height of the top part (or bottom) times 2 plus 1 (the middle row). You get something more less like this:
for(int i = 0; ((i*2)+1) <= height; i++){
//Print top
}
// Print middle row outside of loops or inside if you must.
for(int i = 0; ((i*2)+1) <= height; i++){
//Print bottom.
}
EDIT: This would only be true for diamonds where height is greater than or equal to the width.
EDIT: This code solves your problem (given height is greater than or equal to width). However, it does not handle width = 0 or width = 1 properly. I trust that would be trivial enough for you to do.
public static void main(String[] args) {
int height = 9, width = 9;
int spaces = (width - 1) / 2, asterisks = 1;
for(int i = 0;((i*2)) < height; i++){
for(int ii = 0;ii < spaces;ii++){
System.out.print(" ");
}
for(int ii = 0;ii < asterisks;ii++){
System.out.print("*");
}
spaces--;
if(asterisks < width)asterisks += 2;
System.out.println();
}
spaces = 0;
for(int i = 0; ((i*2)+1) < height; i++){
if(width < height){
for(int ii = 0;ii < spaces;ii++){
System.out.print(" ");
}
for(int ii = 0;ii < asterisks;ii++){
System.out.print("*");
}
}else{
for(int ii = 0;ii < (spaces + 1);ii++){
System.out.print(" ");
}
for(int ii = 0;ii < (asterisks - 2);ii++){
System.out.print("*");
}
}
spaces++;
asterisks -= 2;
System.out.println();
}
}
You can calculate the difference between height and width and reuse your logic in a while loop.
for(int i = 1; i < width + 1; i += 2) {
for(int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for(int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
int dif = height-width;
while(dif !=0){
for(int i = width; i < width + 1; i += 2) {
for(int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for(int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
dif--;
}
for(int i = width - 2; i > 0; i -= 2) {
for(int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for(int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
I am trying to print something like this using for loops:
1
121
12321
1234321
123454321
int mid = 1;
System.out.println(" " + mid + " ");
mid++;
for(int i = 1; i <=4; i++){
//left spaces
for(int x = 4; x > i; x--){
System.out.print(" ");
}
//left diguts
for(int z = 1; z <= i; z++){
System.out.print(z);
}
//middle digit
System.out.print(mid);
mid++;
//right diguts
for(int b = 1; b <= i; b++){
System.out.print(b);
}
//right spaces
for(int y = 4; y > i; y--){
System.out.print(" ");
}
System.out.println();
}
But i kept on getting:
1
121
12312
1234123
123451234
For the right digits you will have to print the digits in reverse order so make the change as below:
//right diguts
for(int b = 1; b <= i; b++){
System.out.print(b);
}
change this to
//right diguts
for(int b = i; b > 0; b--){
System.out.print(b);
}
Actually there is no need to separate 1 out. You can include it in common logic:
public class Pyramid {
public static void main(String[] args) {
int mid = 1;
for (int i = 0; i <= 4; i++) {
// left spaces
for (int x = 4; x > i; x--) {
System.out.print(" ");
}
// left digits
for (int z = 1; z <= i; z++) {
System.out.print(z);
}
// middle digit
System.out.print(mid);
mid++;
// right digits
for(int b = i; b > 0; b--){
System.out.print(b);
}
// right spaces
for (int y = 4; y > i; y--) {
System.out.print(" ");
}
System.out.println();
}
}
}
This is actually nice golf puzzle!
I did this only using two loops! :)
int n = 5;
for(int i=0; i < n; i++){
for(int j=0; j < 2*n; j++){
int abs = Math.abs(n-j);
System.out.print(abs>i ? " " : i-abs+1);
}
System.out.println();
}
Output:
1
121
12321
1234321
123454321
I'm still working on my Java Diamond Problem.
Here is the current state of the diamond
I'm thinking the problem is the for loop right here,
all I need is for those spaces to be printed and then I'm finished.
Can anyone see any obvious reasons why the for loop I labeled with the problem isn't being entered or running?
//Bottom half of the diamond
int middleSpaces = sides + 2;
int downPreSpaces = 1;
int dRows = sides + 1;
for (int x = 1; x <= dRows; x++) {
if (x >= dRows) {
for (int z = 1; z <= sides + 1; z++) {
System.out.print(" ");
}
System.out.print("v");
}
if (x != dRows) {
for(int y = 1; y <= x; y++) {
System.out.print(" ");
}
System.out.print("\\");
//PROBLEM IS HERE
for (int e = middleSpaces - 2; e <= 0; e += 2) {
System.out.print(" ");
}
System.out.print("/\n");
}
Step through your code with a debugger and examine the state of the variables e and dRows at the location that is causing problems.
You were not decrementing middleSpaces also it was not initialized properly:
int middleSpaces = sides * 2; //NOTICE
int downPreSpaces = 1;
int dRows = sides + 1;
for (int x = 1; x <= dRows; x++) {
if (x >= dRows) {
for (int z = 1; z <= sides + 1; z++) {
System.out.print(" ");
}
System.out.print("v");
}
if (x != dRows) {
for(int y = 1; y <= x; y++) {
System.out.print(" ");
}
System.out.print("\\");
for (int e = middleSpaces - 2; e >= 0; e -= 1) {
System.out.print(" ");
}
middleSpaces-=2; //NOTICE
System.out.print("/\n");
}
middleSpaces - 2 might be less than dRows, or else one of the outer loops doesn't work.
Notice that the values of dRows, middleSpaces and dmSpaces do not change in the code you've shown. This means the inner loop in question will always print the same number of spaces.
You probably mean something like the following:
for(int e = middleSpaces - 2; e >= x; e -= 2) {
System.out.print(" ");
}
That might be off by one or something but is closer to the result you're trying to get.
How do I make this:
*******
-*****-
--***--
---*---
--***--
-*****-
*******
The following is my code that I have written to try to accomplish the above, but it is not working as expected:
public static void stars(/*int jmlBaris*/) {
for ( int i = 7; i >= 1; i-=2) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int i = 1; i <= 7; i+=2) {
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
stars();
}
}
This is how I might write it.
// three loops
public static void stars(int size) {
for (int y = 0; y < size; y++) {
for (int i = 0; i < y && i < size - y - 1; i++)
System.out.print(' ');
for (int i = Math.min(y, size - y - 1); i < Math.max(y + 1, size - y); i++)
System.out.print('*');
System.out.println();
}
}
or
// two loops
public static void stars(int size) {
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++)
System.out.print(
(x >= y && x < size - y) ||
(x >= size - y - 1 && x <= y) ? '*' : ' ');
System.out.println();
}
}
or
// one loop
public static void stars(int size) {
for (int i = 0; i < size * size; i++) {
int y = i / size, x = i % size;
System.out.print(
(x >= y && x < size - y) ||
(x >= size - y - 1 && x <= y) ? '*' : ' ');
if (x == size - 1)
System.out.println();
}
}
Note: Whether this uses one, two or three loops, the time complexity is O(N^2). A simple way to determine this is the number of stars produced is O(N^2) no matter how it is done.
I would do something like this with substrings.
String a = "*******"; //7 stars
String blank = " "; //7 spaces
int j = 7;
for (int i = 0; i < 7; i++) {
if (i > j){
System.out.print(blank.substring(0,i));
System.out.println(a.substring(i,j));
}
else{
System.out.print(blank.substring(0,j));
System.out.println(a.substring(j,i));
}
j--;
}
System.out.println(a);
**Previous edit wouldn't have worked. Changes made.
This works.
Try something like this code I compiled on IDEOne (it seems to work, though):
http://ideone.com/9xZ1YB
class Main
{
public static void main(String[] args)
{
stars();
}
static void stars()
{
final int MAX_WIDTH = 7;
for (int i = 0; i < 7; ++i)
{
int width;
if (i < 3) width = MAX_WIDTH - i * 2;
else if (i > 3) width = (i - 3) * 2 + 1;
else width = 1;
// Before spaces
for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
{
System.out.print(" ");
}
// Stars
for (int j = 0; j < width; ++j)
{
System.out.print("*");
}
// After spaces
for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
{
System.out.print(" ");
}
System.out.println();
}
}
}
For a beginner in algorithms I would recommend you to break down the structure in sub-parts and then try to solve the pattern.
For this specific pattern it could be broken down into several triangles. Each triangle is then solved by different for loops as shown in the image below.
public static void printPattern(int num) {
// this loop generates first 4 lines
for (int i = 0; i < num / 2 + 1; i++) {
// draws the red triangle of '-'
for (int j = 0; j < i; j++) {
System.out.print("-");
}
// draws the green triangle of '*'
for (int j = i; j < num / 2 + 1; j++) {
System.out.print("*");
}
// draws the blue triangle of '*'
for (int j = i + 1; j < num / 2 + 1; j++) {
System.out.print("*");
}
// draws the orange triangle of '-'
for (int j = 0; j < i; j++) {
System.out.print("-");
}
System.out.println();
}
/* this loop generates last 3 lines */
for (int i = 0; i < num / 2; i++) {
// draws the green triangle of '-'
for (int j = i + 1; j < num / 2; j++) {
System.out.print("-");
}
// draws the red triangle of '*'
for (int j = 0; j < i + 2; j++) {
System.out.print("*");
}
// draws the orange triangle of '*'
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
// draws the blue triangle of '-'
for (int j = i + 1; j < num / 2; j++) {
System.out.print("-");
}
System.out.println();
}
}
Using similar technique you could generate any pattern.
If I understood you right, your problem is to print indent in lines 2-7.
Imagine same problem with asterisk symbol replaced by 'x' and whitespace replaced by '-'. Then you need to draw
xxxxxxx
-xxxxx-
--xxx--
---x---
--xxx--
-xxxxx-
xxxxxxx
That means you should output 0, 1, 2 space(s) before asterisks in first, second, thrid strings respectively. I let details for you to figure them out.
public static void stars(/*int jmlBaris*/){
String starstr = "*";
String blank = "_";
int spaceBlank;;
for(int i=7; i>=1;i-=2){
spaceBlank = (7-i)*.5;
String starrep = StringUtils.repeat(starstr, i);
String blankrep = StrinUtils.repeat(blank, spacesBlank);
system.out.println(blankrep + starrep + blankrep);
}
for(int j=3 j<=7; j+=2){
spaceBlank = (7-j)*.5;
starrep = StringUtils.repeat(starstr, j);
String blankrep = StrinUtils.repeat(blank, spacesBlank);
system.out.println(blankrep + starrep + blankrep);
}
}
public static void main(String[] args){
stars();
}
You have little missing to put space on your code. I don't care about right space, who can see that? But left space is very important!!
Try this:
public static void stars(/*int jmlBaris*/) {
for ( int i = 7; i >= 1; i-=2) {
for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
System.out.print(" "); /* Missing Here */
} /* Missing Here */
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int i = 1; i <= 7; i+=2) {
for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
System.out.print(" "); /* Missing Here */
} /* Missing Here */
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println("");
}
}
int N = 7;
for (int y=0; y<N; y++)
{
for (int x=0; x<N; x++)
System.out.print( (y-x)*(N-y-x-1)<=0 ? '*' : '-');
System.out.println();
}
or, more symmetrically,
int n = 3;
for (int y=-n; y<=n; y++)
{
for (int x=-n; x<=n; x++)
System.out.print( y*y>=x*x ? '*' : '-');
System.out.println();
}