基于用户输入在Java中打印钻石形状 [英] Printing A Diamond Shape In Java, Based On User Input

查看:264
本文介绍了基于用户输入在Java中打印钻石形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写的程序出现问题。用户输入正奇数,否则程序会提示用户,直到他们这样做。当它们执行时,程序打印对应于用户输入的菱形。

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 prints a diamond shape corresponding to the user input.

到目前为止我有这件作品打印出这样一个人物的左手对角线,但无法弄清楚如何打印其余部分。以下是代码:

I have this piece so far that prints the left hand diagonal of such a figure, but cannot figure out how to print the rest of it. Here is the code:

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

    }

    }
}


推荐答案


用户输入一个正奇数,否则程序会提示
用户直到

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

你需要在循环中扫描

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

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

现在我在循环检查时发现的第一件事是 if(row + col == userInput -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.

请注意,您可以替换 System.out.print(\ n); System.out.println()但这不是真的重要...

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

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

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++ ){

既然你想要一个钻石,你需要有4个对角线,所以有2个循环(一个用于顶部和底部)......

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

}

这篇关于基于用户输入在Java中打印钻石形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆