Welcome to Dream.In.Code
Become a Java Expert!

Join 149,523 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,459 people online right now. Registration is fast and FREE... Join Now!




Creating a Diamond

 
Reply to this topicStart new topic

Creating a Diamond

Defcon2006
22 May, 2007 - 05:46 PM
Post #1

New D.I.C Head
*

Joined: 22 May, 2007
Posts: 1


My Contributions
Hey guys, An assignment was given that you allow the user to input an odd number. If it's not odd or greater than 0 they get a invalid number response. You are suppose to create a diamond structure with asteriks like this,
*
***
*
So far i've gotten the user input and printing. But i'm having problems with the spacing and allowing for more than 1 asterik to be shown on the line. Here's what I have to far
CODE

import java.util.Scanner;

public class DiamondPrinter
{

    public static void main ( String args[] )
    {

    Scanner input = new Scanner( System.in );

    System.out.print ( "Enter an odd number, 1 or more:");
    int diamonds = input.nextInt();

    int n = diamonds%2;
    int h = diamonds;
    int i;    
    
    while ( n == 0 || n < 0)
    
        {
    int invalid = diamonds;

    System.out.printf (" %s is not a valid number, please enter a new number:",invalid);
    diamonds = input.nextInt();
    n = diamonds%2;
    h = diamonds;
        }
    while (n != 0)
    {
        for (i = 1; i <= h; ++i)
        System.out.println( "*");
    
    }

    }

}



Any help on how to get multiple asteriks printed on a line with a user inputted number would help a ton. thanks smile.gif.

This post has been edited by Defcon2006: 22 May, 2007 - 05:47 PM
User is offlineProfile CardPM
+Quote Post

capty99
RE: Creating A Diamond
22 May, 2007 - 06:21 PM
Post #2

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
change the part where its printing the asterick, remove the ln.
so System.out.print("*");

then, after the while statement print a newline.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:45PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month