Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: I need help

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help

    Hello, I need to create a pyramid of numbers. In advance I got "public static void printNumPyramid (int numPyramidLvl)" Because it is not main, it gets lost completely and if I do a loop, I can't throw it into main to get the program copied. The guidelines I have are the number of rows (levels of the pyramid), given as a parameter to the function example, printPyramid (int lvlNum) {...}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help

    Please post your code here so we can see what you have done.
    Be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help

    package Pyramid;
     
    import java.util.Scanner;
     
    public class PyramidTest {
     
        public static void main(String[] args){
            System.out.println("State how many levels the pyramid is to consist of: ");
            Scanner n = new Scanner(System.in);
            System.out.println(PyramidPattern());
        }
     
        public static void PyramidPattern(){
            int n = 5;
            String b = "3";
            for (int i = 1; i <= n; ++i){
                for (int j = 1; j <= i; ++j){
                    System.out.print(b);
                }
                System.out.println();
            }
     
        }
     
        public static void printNumberPyramid(int numberOfPyramidLevels) {
     
     
     
        }
     
    }
     
    // if we type the number 5, it should display a pyramid with 5 levels
    // write out the pyramid of digits on the console, we will write out digit 3 in string or int form, i.e. in the first line one digit, in the second line two digits, in the third line 3 digits, etc.
    //example of outputting to the console
    //3
    //33
    //333
    //itd
    //up to the number of rows (levels of the pyramid), given as a parameter to the function example, printPyramid(int lvlNumber) {...}
    //TIP split the problem into smaller parts, the result does not have to be a single function but a fucktivity, i.e. if I call one function it will display a pyramid

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help

    Do you have any questions about the program?
    What happens when you compile and execute the program?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help

    package Pyramid;
     
    import java.util.Scanner;
     
    public class PyramidTest {
     
        public static void main(String[] args){
            System.out.println("State how many levels the pyramid is to consist of: ");
            Scanner n = new Scanner(System.in);
            System.out.println();
        }
     
        public static void PyramidPattern(){
            int n = 5;
            String b = "3";
            for (int i = 1; i <= n; ++i){
                for (int j = 1; j <= i; ++j){
                    System.out.print(b);
                }
                System.out.println();
            }
     
        }
     
        public static void printNumberPyramid(int numberOfPyramidLevels) {
     
     
     
        }
     
    }

    Console:
    State how many levels the pyramid is to consist of: 
     
     
    Process finished with exit code 0

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help

    Ok, did you have any questions about the code?

    My comments:
    The PyramidPattern method is not called. A method needs to be called for it to be executed.
    What is the empty println method called in the main method for?
    n is a poor name for a variable that refers to an instance of the Scanner class.
    A better name would be something like: readInput
    Then one of the Scanner class's methods needs to be called to get the input from the user.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2024
    Posts
    40
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: I need help

    Sure! Here's a solution in Java for creating a pyramid of numbers based on the provided method signature:

    ```java
    public class Pyramid {
    public static void main(String[] args) {
    // Example usage:
    printNumPyramid(5); // Change 5 to the desired number of levels
    }

    public static void printNumPyramid(int numPyramidLvl) {
    for (int i = 1; i <= numPyramidLvl; i++) {
    // Print spaces to center-align the numbers
    for (int j = 1; j <= numPyramidLvl - i; j++) {
    System.out.print(" ");
    }

    // Print numbers in ascending order
    for (int j = 1; j <= i; j++) {
    System.out.print(j + " ");
    }

    // Print numbers in descending order
    for (int j = i - 1; j >= 1; j--) {
    System.out.print(j + " ");
    }

    System.out.println(); // Move to the next line for the next row
    }
    }
    }
    ```

    This program defines a method `printNumPyramid` which takes the number of levels of the pyramid as an argument. Inside this method, it iterates through each row of the pyramid and prints the appropriate numbers in ascending and descending order to form the pyramid shape. Finally, in the `main` method, you can call `printNumPyramid` with the desired number of levels to see the pyramid output.

    Finally, in the main method, you can call printNumPyramid with the desired number of levels to see the pyramid output. If you find yourself needing further assistance or guidance with your Java assignment, there are resources available online where you can seek help with Java assignment, such as websites like ProgrammingHomeworkHelp.com offering programming support and homework assistance.