当前位置: 首页 > 面试题库 >

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

齐昊
2023-03-14
问题内容

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

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

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

    }

    }
}

问题答案:

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

您需要循环扫描

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

}


 类似资料:
  • 需要一些专家的建议。首先,这里是我的代码: 我的问题是:当我输入一个用户号码如“3”时,提示我的代码只打印一个__当我希望它打印指定的号码。我无论如何也想不出来。 我是英语专业的,所以这不是我的强项:(形状应该是菱形的。)

  • 问题内容: 我想在Python 3.5中打印以下模式(我是编码新手): 但是我只知道如何使用下面的代码打印以下内容,但不确定如何将其反转以使其成为完整的菱形: 任何帮助,将不胜感激! 问题答案: 由于中排和最大排的星星有9颗星,因此您应该使之等于9。您可以打印出一半的菱形,但是现在您必须尝试制作一个可以打印特定数量的空格的函数,然后具体数量的星星。因此,请尝试开发一种模式,使每行中的空格和星星数量

  • 我试图用两种不同的方式做阶乘方法。第一个是迭代方法,它非常适合打印出来。我很难打印出递归方法,它是一个int方法而不是val。我将如何通过使用与迭代方式相同的输入来打印递归方法?非常感谢任何帮助。我正在尝试在我的main中调用每个方法。 Java代码:

  • 本文向大家介绍java打印出菱形图案实例详解,包括了java打印出菱形图案实例详解的使用技巧和注意事项,需要的朋友参考一下 第一步:首先对图像进行解析 想要打印该图形必须要进行多层循环嵌套,分两个部分进行打印。 第一部分为上半部分前四行,他们是递增的关系,后半部分后三行为递减关系,由此可以得出我们需要写两个打的循环。并且由于“*”位置的关系,我们必须带入空格同时打印。所以每个部分需要两个循环控制,

  • 问题内容: 我只是想知道如何在Java中打印平方根(√)字符?我假设您使用其unicode或什么? 问题答案: 只是 资料来源:Google的首次比赛。

  • 问题内容: 如何在Java用户输入中打印出单个单词?示例:用户输入:“我们爱妈妈,她是最好的”。该程序假设打印“妈妈”,因为第一个字符和最后一个字符相同。我的代码最后没有显示任何内容。这是我的代码: 问题答案: 无需解析字符串的每个字母,您可以将输入拆分成单词数组并分别检查每个单词。 您可以保持循环,但只需要检查是否与处的循环相同 这是一个工作示例。请注意,我已经删除了扫描仪部件,以使其在我正在使