hi. This is my first topic here. i was searching what i need on internet. bu i didn't find what exactly i want. so i decided to register and ask here.
i want to make a program that show number from 0 to 100 in pyramid form.
like this;
(forget dots. i put them to make pyramid shape. just spacebar is not working)
....................1
..............2....3....4
....... 5....6....7....8....9
.10...11..12...13..14..15...16
.
.
.
.
.
i know i am gonna use nested 'for' loops but i cant imagine algorithm that i need.
can anyone help me please?
How can i make pyramid at Java
Page 1 of 11 Replies - 6502 Views - Last Post: 02 November 2009 - 02:48 PM
Replies To: How can i make pyramid at Java
#2
Re: How can i make pyramid at Java
Posted 02 November 2009 - 02:48 PM
I have a much more complicated program sitting around I wrote a while ago (like a whiiiiiilllleeee ago) that does something similar, it will probably point you in the right direction:
Now before anyone goes getting pissed at me for providing code without seeing code the code doesn't do what he needs it to and it has a lot of my comments in it, and it was written by me, and you may use portions, but you cannot copy my code.
import java.util.Scanner;
/**
* A program to print a number of stars according to user input
* @author Mike Magnus
*
*/
public class StarProgram
{
public static void main(String[] args)
{
int user = 0;
Scanner input = new Scanner(System.in);
//A while loop that simply validates input
while (((user%2) == 0) || (user < 1) || (user > 19))
{
System.out.print("Please enter an odd integer:");
user = input.nextInt();
}
for(int lines = 1;lines <= ((user/2)+1);lines++)
{
/*line*2 is because lines will only be half the value of user when the loop is
* at its maximum, so we want the logic to max out halfway.
*
* -1 gives us a value that is still odd because doubling it gives us an even
* number that we don't want
*
* user- gives us an inverse in our logic, that gets lower as lines gets higher.
*
* the /2 is because we only need enough spaces for the first half of the lines
*/
for(int spaces = 0;spaces<(user-((lines*2)-1))/2;spaces++)
System.out.print(" ");
/*
* For the first lines loop, the number of stars will be directly proportional,
* giving us a simple comparison of
* lines*2 because we want it to max out once it is half way and this first lines
* loop is complete, and the -1 because we want an odd value still.
*/
for(int stars=0;stars<((lines*2)-1);stars++)
System.out.print("*");
System.out.print("\n");
}
for(int lines=(user/2)+2;lines<=user;lines++)
{
/*
* since lines will start one past half, it will now have to function in the opposite
* way to get a similar pattern in reverse, but it needed to be edited
*
* (lines-1) compensates for (user/2) always being rounded down
*/
for(int spaces=0;spaces<(lines-1)-(user/2);spaces++)
System.out.print(" ");
/*
* Now the number of stars has to be proportional in a similar fashion to how
* spaces were, but they function in a slightly different manner.
*
* (user-lines) will give us how many stars need to be on either side of the
* central star
*
* the *2 will give us both sides of the central star
*
* the +1 gives us back the central star
*/
for(int stars=0;stars<((user-lines)*2)+1;stars++)
System.out.print("*");
System.out.print("\n");
}
}
}
Now before anyone goes getting pissed at me for providing code without seeing code the code doesn't do what he needs it to and it has a lot of my comments in it, and it was written by me, and you may use portions, but you cannot copy my code.
This post has been edited by TriggaMike: 02 November 2009 - 02:48 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|