problem with spacing..

I need help with the spacing..

Page 1 of 1

2 Replies - 502 Views - Last Post: 18 March 2010 - 07:04 AM Rate Topic: -----

#1 mvowashere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-July 09

problem with spacing..

Posted 18 March 2010 - 04:23 AM

My problem is with the spacing.
The diamond is crooked...any suggestions? :]

*Height and width should be 20

import javax.swing.JOptionPane;
public class diamond
{
public static void main(String args[])
{
String msg=" ";
int r,c;
for(r=1;r<=20;r +=2)
{
 for(c=0;c<19-r/2;c++)
msg += "    ";

for(c=0; c<r; c++)
msg +=" ^ ";
msg +="\n";
}
 for(r=21;r>0;r-=2)
{
  for(c=0;c<21-r/2;c++)
msg+="    ";

for(c=0;c<r; c++)
msg +=" v ";
msg +="\n";
}	
JOptionPane.showMessageDialog(null,msg);
}
}


This post has been edited by mvowashere: 18 March 2010 - 04:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with spacing..

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: problem with spacing..

Posted 18 March 2010 - 05:56 AM

Just need some extra spacing:
import javax.swing.JOptionPane;
public class Test
{
	public static void main(String args[])
	{
		String msg=" ";
		int r,c;
		for(r=1;r<=20;r +=2)
		{
			 msg += "     "; //added this line
			 for(c=0;c<19-r/2;c++)
				msg += "    ";

			for(c=0; c<r; c++)
				msg +=" ^ ";
			msg +="\n";
		}
		for(r=21;r>0;r-=2)
		{
			for(c=0;c<21-r/2;c++)
				msg+="    ";

			for(c=0;c<r; c++)
				msg +=" v ";
			msg +="\n";
		}       
		JOptionPane.showMessageDialog(null,msg);
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 chirag.jain18  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 68
  • Joined: 14-December 09

Re: problem with spacing..

Posted 18 March 2010 - 07:04 AM

make r=20 in palce of 21

import javax.swing.JOptionPane;
public class diamond
{
public static void main(String args[])
{
String msg=" ";
int r,c;
for(r=1;r<=20;r +=2)
{
 for(c=0;c<19-r/2;c++)
msg += "    ";

for(c=0; c<r; c++)
msg +=" ^ ";
msg +="\n";
}
 for(r=20;r>0;r-=2)
{
  for(c=0;c<20-r/2;c++)
msg+="    ";

for(c=0;c<r; c++)
msg +=" v ";
msg +="\n";
}       
JOptionPane.showMessageDialog(null,msg);
}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1