Printing A Diamond Shape In Java, Based On User Input

后端 未结 1 1815
长发绾君心
长发绾君心 2021-01-29 13:08

I have a problem with a program I am trying to write. A user inputs a positive odd integer, otherwise the program prompts the user until they do. When they do, the program print

相关标签:
1条回答
  • 2021-01-29 13:30

    A user inputs a positive odd integer, otherwise the program prompts the user until they do

    You need to scan in a loop

    do
    {
        userInput = keyboard.nextInt(); 
        if (userInput % 2 == 0)
            System.out.println("That is not an Odd Integer!");            
        if(userInput < 0)
            System.out.println("That is not a Positive Odd Integer");
    } while(userInput < 0 || userInput %2 == 0);
    

    Now you can remove that validation else if (userInput%2 == 1){

    Now the first thing I realize when checking at your loop is if (row+col==userInput-1 || ) which won't compile as you have a comparison operator with nothing following.

    Note that you can replace System.out.print("\n"); with System.out.println("") but that's not really important...

    Now replace your loop condition so that they start as 0

    for (int row = 0; row <= userInput; row++){
            for (int col = 0; col <= userInput; col++ ){
    

    Now since you want a Diamond, you need to have 4 diagonal, so 2 loops (one for top and bottom)...

    for (int i = 1; i < userInput; i += 2)//Draw the top of the diamond
    {
        for (int j = 0; j < userInput - 1 - i / 2; j++)//Output correct number of spaces before
        {
            System.out.print(" ");
        }
        for (int j = 0; j < i; j++)//Output correct number of asterix
        {
            System.out.print("*");
        }
    
        System.out.print("\n");//Skip to next line
    }
    
    for (int i = userInput; i > 0; i -= 2)//Draw the bottom of the diamond
    {
        for (int j = 0; j < userInput -1 - i / 2; j++)
        {
            System.out.print(" ");
        }
    
        for (int j = 0; j < i; j++)
        {
            System.out.print("*");
        }
    
        System.out.print("\n");
    }
    

    So the final code would look like this

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Welcome to the drawing program:");
        System.out.println("Please Input a Positive Odd Integer:");
        char userAnswer;
        int userInput;
        do
        {
            userInput = keyboard.nextInt();
            if (userInput % 2 == 0)
            {
                System.out.println("That is not an Odd Integer!");
            }
            if (userInput < 0)
            {
                System.out.println("That is not a Positive Odd Integer");
            }
        } while (userInput < 0 || userInput % 2 == 0);
    
        for (int i = 1; i < userInput; i += 2) //This is the number of iterations needed to print the top of diamond (from 1 to userInput by step of two for example with 5 = {1, 3, 5} so 3 rows.
        {
            for (int j = 0; j < userInput - 1 - i / 2; j++)//write correct number of spaces before, example with 5 = j < 5 - 1 -i / 2, so it would first print 4 spaces before, with 1 less untill it reach 0
            {
               System.out.print(" ");//write a space
            }
            for (int j = 0; j < i; j++)
            {
                System.out.print("*");//write an asterix
            }
    
            System.out.println("");
        }
    
        // Same logic apply here but backward as it is bottom of diamond
        for (int i = userInput; i > 0; i -= 2)
        {
            for (int j = 0; j < userInput -1 - i / 2; j++)
            {
                System.out.print(" ");
            }
    
            for (int j = 0; j < i; j++)
            {
                System.out.print("*");
            }
    
            System.out.print("\n");
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题