小编典典

根据用户输入在Java中打印菱形

java

我要编写的程序有问题。用户输入正整数,否则程序会提示用户直到他们输入。完成后,程序将打印与用户输入相对应的菱形形状。

到目前为止,我有这张照片可以打印出该图形的左对角线,但无法弄清楚如何打印其余的图形。这是代码:

import java.util.Scanner; 
public class DrawingProgram {

    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;
        userInput = keyboard.nextInt(); 
        if (userInput%2 == 0){
            System.out.println("That is not a Positive Odd Integer!");
        }

        else if (userInput < 0){
            System.out.println("That is not a Positive Odd Integer");

        }

        else if (userInput%2 == 1){
                for (int row = 1; row<= userInput; row++){
                    for (int col = 1; col<= userInput; col++ ){
                        if (row+col==userInput-1 )

                            System.out.print( "*");
                        else
                            System.out.print( " ");
            }
                System.out.print("\n");
        }

    }

    }
}

阅读 342

收藏
2020-12-03

共1个答案

小编典典

用户输入正整数,否则程序会提示用户,直到他们输入

您需要循环扫描

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);

现在您可以删除该验证 else if (userInput%2 == 1){

现在,我在检查循环时意识到的第一件事是if (row+col==userInput-1 || )不会编译,因为您有一个比较运算符,后面没有任何内容。

请注意,您可以替换为System.out.print("\n");System.out.println("")但这并不是很重要…

现在替换循环条件,使它们从0开始

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

现在,由于您想要钻石,因此需要有4个对角线,因此需要2个循环(一个用于顶部和底部)。

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");
}

所以最终的代码看起来像这样

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");
    }

}
2020-12-03