简体   繁体   English

打印钻石-无法输入输入?

[英]Printing a diamond - can't enter input?

I'm trying to write a program that outputs a diamond pattern like this: 我正在尝试编写一个程序,输出如下所示的菱形图案:

   *
  ***
 *****
  ***
   *

I've started by trying to first get it to print the top half of the diamond. 我首先尝试首先将其打印在钻石的上半部分。

I can input the 'totalLines' into the console, but I can't type anything when it prompts for 'character'. 我可以在控制台中输入'totalLines',但是当提示输入'character'时我什么也不能输入。 Why would this be happening? 为什么会这样呢?

We've been using JOptionPane for most of our assignments, so it makes sense that I'd be having trouble with this, but from what I can tell from reading the book, this is correct. 我们一直在使用JOptionPane进行大多数分配,因此我遇到了麻烦,但是从阅读本书可以看出,这是正确的。

(And if you have time to talk to me about the for-loops, I'm pretty sure they need work. I'd be very grateful.) (如果您有时间与我讨论for循环,我很确定他们需要工作。我将非常感激。)

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int totalLines, lines, currLine = 1, spaces, maxSpaces, minSpaces, numCharacters, maxCharacters, minCharacters;

    String character;

    System.out.print("Enter the total number of lines: ");
    totalLines = input.nextInt();
    System.out.print("Enter the character to be used: ");
    character = input.nextLine();

    lines = ((totalLines + 1)/2);
    // spaces = (Math.abs((totalLines + 1)/2)) - currLine;
    maxSpaces = (totalLines + 1)/2 - 1;
    minSpaces = 0;
    // numCharacters = (totalLines - Math.abs((totalLines +1) - (2*currLine)));
    maxCharacters = totalLines;
    minCharacters = 1;
    spaces = maxSpaces;

    for (currLine = 1; currLine<=lines; currLine++) {
        for (spaces = maxSpaces; spaces<=minSpaces; spaces--){
            System.out.print(" ");
            }
        for (numCharacters = minCharacters; numCharacters>= maxCharacters; numCharacters++){
            System.out.print(character);
            System.out.print("\n");
        }
        }


    }

尝试使用next()代替nextLine()

I'm creating a separate answer in regards to your for loops. 我正在为您的for循环创建一个单独的答案。 This should be pretty close to what you need. 这应该非常接近您的需求。

StringBuilder might be new to you. StringBuilder对您来说可能是新手。 Because StringBuilder is mutable and String is not, if you're doing multiple concatenations onto an existing string it's usually preferable to use StringBuilder instead of String . 由于StringBuilder是可变的,而String不是,因此,如果对现有字符串进行多个串联,通常最好使用StringBuilder而不是String You don't have to use StringBuilder for this exercise but it's a good habit to start. 您不必为此练习使用StringBuilder ,但这是一个良好的开始习惯。

//Get user input here
int lines = totalLines;

if(totalLines % 2 != 0)
    lines += 1;

int i, j, k;
for (i = lines; i > 0; i--) 
{
    StringBuilder spaces = new StringBuilder();
    for (j = 1; j < i; j++)
    {
        spaces.append(" ");
    }

    if (lines >= j * 2)
    {
        System.out.print(spaces);
    }

    for(k = lines; k >= j * 2; k--)
    {
        System.out.print(character);
    }

    if (lines >= j * 2)
    {
        System.out.println();
    }
}

for (i = 2; i <= lines; i++)
{
    StringBuilder spaces = new StringBuilder();
    System.out.print(" ");

    for (j = 2; j < i; j++)
    {
        spaces.append(" ");
    }

    if(lines >= j * 2)
    {
        System.out.print(spaces);
    }

    for (k = lines ; k >= j * 2; k--)
    {
        System.out.print(character);
    }

    if (lines >= j * 2)
    {
        System.out.println();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM